Apache ANT ด้วย Selenium: บทช่วยสอนที่สมบูรณ์

Apache Ant คืออะไร?

ในขณะที่สร้างผลิตภัณฑ์ซอฟต์แวร์ที่สมบูรณ์ เราจำเป็นต้องดูแล API ของบุคคลที่สามที่แตกต่างกัน คลาสพาธของพวกเขา การล้างไฟล์ไบนารีที่ปฏิบัติการได้ก่อนหน้า การคอมไพล์ซอร์สโค้ดของเรา การดำเนินการซอร์สโค้ด การสร้างรายงาน และฐานโค้ดการปรับใช้ ฯลฯ หากงานเหล่านี้ ทำทีละรายการด้วยตนเองจะใช้เวลานานมากและกระบวนการนี้มีแนวโน้มที่จะเกิดข้อผิดพลาดได้

มาถึงความสำคัญของเครื่องมือสร้างเช่น Ant มันจัดเก็บ ดำเนินการ และดำเนินการกระบวนการทั้งหมดโดยอัตโนมัติตามลำดับที่กล่าวถึงในไฟล์กำหนดค่าของ Ant (โดยปกติคือ build.xml)

มดอาปาเช่

ประโยชน์ของการสร้างมด

  1. Ant สร้างวงจรชีวิตของแอปพลิเคชัน เช่น ล้าง คอมไพล์ ตั้งค่าการพึ่งพา ดำเนินการ รายงาน ฯลฯ
  2. Ant สามารถตั้งค่าการพึ่งพา API ของบริษัทอื่นได้ เช่น พาธคลาสของไฟล์ Jar อื่นตั้งค่าโดยไฟล์ Ant build
  3. แอปพลิเคชันที่สมบูรณ์ถูกสร้างขึ้นสำหรับการจัดส่งและการปรับใช้แบบครบวงจร
  4. มันเป็นเครื่องมือสร้างอย่างง่ายที่การกำหนดค่าทั้งหมดสามารถทำได้โดยใช้ไฟล์ XML และสามารถดำเนินการได้จากบรรทัดคำสั่ง
  5. ทำให้โค้ดของคุณสะอาดเนื่องจากการกำหนดค่าแยกจากตรรกะของแอปพลิเคชันจริง

วิธีการติดตั้ง Ant

ขั้นตอนการติดตั้ง Ant Windows มีดังนี้

ขั้นตอน 1) ไปที่ https://ant.apache.org/bindownload.cgi หรือดาวน์โหลดไฟล์ .zip จาก apache-ant-1.9.4-bin.zip

ติดตั้ง Ant

ขั้นตอน 2) คลายซิปโฟลเดอร์แล้วไปที่และคัดลอกเส้นทางไปยังรูทของโฟลเดอร์ที่คลายซิป

ติดตั้ง Ant

ขั้นตอน 3) ไปที่ Start -> Computer -> คลิกขวาที่นี่และเลือก 'Properties' จากนั้นคลิกที่ Advanced System Settings

ติดตั้ง Ant

ขั้นตอน 4) หน้าต่างใหม่จะเปิดขึ้น คลิกที่ปุ่ม 'ตัวแปรสภาพแวดล้อม…'

ติดตั้ง Ant

ขั้นตอน 5) คลิกปุ่ม 'ใหม่…' และตั้งชื่อตัวแปรเป็น 'ANT_HOME' และค่าตัวแปรเป็นเส้นทางรูทไปยังโฟลเดอร์ที่คลายซิปแล้วคลิกตกลง

ติดตั้ง Ant

ขั้นตอน 6) ตอนนี้เลือกตัวแปร 'เส้นทาง' จากรายการแล้วคลิก 'แก้ไข' และต่อท้าย %ANT_HOME%\bin.

ติดตั้ง Ant

รีสตาร์ทระบบหนึ่งครั้ง และคุณพร้อมที่จะใช้เครื่องมือ Ant build ทันที

ขั้นตอน 7) วิธีตรวจสอบเวอร์ชันของ Ant ของคุณโดยใช้บรรทัดคำสั่ง:

มด –เวอร์ชั่น

ติดตั้ง Ant

ทำความเข้าใจกับ Build.xml

Build.xml เป็นองค์ประกอบที่สำคัญที่สุดของเครื่องมือ Ant build สำหรับ Java โครงการ งานที่เกี่ยวข้องกับการทำความสะอาด การตั้งค่า การคอมไพล์ และการปรับใช้ทั้งหมดจะกล่าวถึงในไฟล์นี้ในรูปแบบ XML เมื่อเรารันไฟล์ XML นี้โดยใช้บรรทัดคำสั่งหรือปลั๊กอิน IDE คำสั่งทั้งหมดที่เขียนลงในไฟล์นี้จะถูกดำเนินการตามลำดับ

มาทำความเข้าใจโค้ดภายใน build.XML ตัวอย่างกัน

  • แท็กโครงการใช้เพื่อกล่าวถึงชื่อโครงการและแอตทริบิวต์ baseir baseir คือไดเร็กทอรีรากของแอปพลิเคชัน
    <project name="YTMonetize" basedir=".">
  • แท็กคุณสมบัติถูกใช้เป็นตัวแปรในไฟล์ build.XML เพื่อใช้ในขั้นตอนถัดไป
<property name="build.dir" value="${basedir}/build"/>
		<property name="external.jars" value=".\resources"/>
	<property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
<property name="src.dir"value="${basedir}/src"/>
  • Target แท็กที่ใช้เป็นขั้นตอนที่จะดำเนินการตามลำดับ แอตทริบิวต์ Name คือชื่อของเป้าหมาย คุณสามารถมีเป้าหมายหลายรายการใน build.xml เดียว
    <target name="setClassPath">
  • แท็ก path ใช้เพื่อรวมไฟล์ทั้งหมดแบบลอจิคัลซึ่งอยู่ในตำแหน่งทั่วไป
    <path id="classpath_jars">
  • แท็ก pathelement จะกำหนดเส้นทางไปยังรูทของตำแหน่งทั่วไปที่เก็บไฟล์ทั้งหมด
    <pathelement path="${basedir}/"/>
  • แท็ก pathconvert ใช้ในการแปลงพาธของไฟล์ทั่วไปทั้งหมดภายในแท็กพาธเป็นรูปแบบคลาสพาธของระบบ
    <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
  • แท็ก fileset ใช้เพื่อตั้งค่า classpath สำหรับ jar บุคคลที่สามที่แตกต่างกันในโปรเจ็กต์ของเรา
    <fileset dir="${ytoperation.dir}" includes="*.jar"/>
  • แท็ก Echo ใช้สำหรับพิมพ์ข้อความบนคอนโซล
<echo message="deleting existing build directory"/>
  • แท็กลบจะล้างข้อมูลจากโฟลเดอร์ที่กำหนด
<delete dir="${build.dir}"/>
  • แท็ก mkdir จะสร้างไดเร็กทอรีใหม่
	<mkdir dir="${build.dir}"/>
  • แท็ก javac ใช้เพื่อรวบรวมซอร์สโค้ด Java และย้ายไฟล์ .class ไปยังโฟลเดอร์ใหม่
        <javac destdir="${build.dir}" srcdir="${src.dir}">
	<classpath refid="classpath_jars"/>
</javac>
  • แท็ก jar จะสร้างไฟล์ jar จากไฟล์ .class
	<jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}">
  • แท็กรายการจะตั้งค่าคลาสหลักของคุณสำหรับการดำเนินการ
<manifest>
		<attribute name="Main-Class" value="test.Main"/>
</manifest>
  • แอตทริบิวต์ 'ขึ้นอยู่กับ' ที่ใช้เพื่อสร้างเป้าหมายหนึ่งให้ขึ้นอยู่กับเป้าหมายอื่น
<target name="run" depends="compile">
  • แท็ก java จะดำเนินการฟังก์ชันหลักจาก jar ที่สร้างขึ้นในส่วนเป้าหมายการคอมไพล์
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>

เรียกใช้ Ant โดยใช้ Eclipse เสียบเข้าไป

หากต้องการเรียกใช้ Ant จาก Eclipse ให้ไปที่ไฟล์ build.xml -> คลิกขวาที่ไฟล์ -> เรียกใช้ในฐานะ… -> คลิกสร้างไฟล์

เรียกใช้ Ant โดยใช้ Eclipse เสียบเข้าไป

ตัวอย่าง

เราจะนำโปรแกรมตัวอย่างเล็กๆ ที่จะอธิบายการทำงานของ Ant ไว้อย่างชัดเจน โครงสร้างโครงการของเราจะมีลักษณะดังนี้ –

เรียกใช้ Ant โดยใช้ Eclipse เสียบเข้าไป

ในตัวอย่างนี้ เรามี 4 เป้าหมาย

  1. กำหนดเส้นทางคลาสสำหรับขวดภายนอก
  2. ทำความสะอาดโค้ดที่ปฏิบัติตามก่อนหน้านี้
  3. รวบรวมโค้ดจาวาที่มีอยู่
  4. เรียกใช้รหัส

Guru99AntClass.คลาส

package testAnt;		
import java.util.Date;		

public class Guru99AntClass {				
   public static void main(String...s){									       
		System.out.println("HELLO GURU99 ANT PROGRAM");					        
		System.out.println("TODAY's DATE IS->"+ currentDate() );					  
}		    		   
public static String currentDate(){					        
	return new Date().toString();					  
	}		
}

Build.xml

 
<?xml version="1.0" encoding="UTF-8"	standalone="no"?>									
<!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->	

<project name="YTMonetize" basedir=".">								
     <!--Property tags will be used as variables in build.xml file to use in further steps-->		

	<property name="build.dir" value="${basedir}/build"/>								
    <property name="external.jars" value=".\resources"/>								
		<property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
<property name="src.dir"value="${basedir}/src"/>
<!--Target tags used as steps that will execute in sequential order. name attribute will be the name  of the target and < a name=OLE_LINK1 >'depends' attribute used to make one target to depend on another target -->		
	       <target name="setClassPath">					
			<path id="classpath_jars">						
				<pathelement	path="${basedir}/"/>					
			</path>				         
<pathconvert	pathsep=";"property="test.classpath" refid="classpath_jars"/>	
</target>				
	<target name="clean">						
		<!--echo tag will use to print text on console-->		
		<echo message="deleting existing build directory"/>						
		<!--delete tag will clean data from given folder-->		
		<delete dir="${build.dir}"/>						
	</target>				
<target name="compile" depends="clean,setClassPath">								
	<echo message="classpath:${test.classpath}"/>					
			<echo message="compiling.........."/>						
	<!--mkdir tag will create new director-->							
	<mkdir dir="${build.dir}"/>						
		<echo message="classpath:${test.classpath}"/>						
		<echo message="compiling.........."/>						
	<!--javac tag used to compile java source code and move .class files to a new folder-->		
	<javac destdir="${build.dir}" srcdir="${src.dir}">								
			<classpath refid="classpath_jars"/>						
	</javac>				
	<!--jar tag will create jar file from .class files-->		
	<jar	destfile="${ytoperation.dir}/YTOperation.jar"basedir="${build.dir}">								
	            <!--manifest tag will set your main class for execution-->		
						<manifest>				
							<attribute name="Main-Class" value="testAnt.Guru99AntClass"/>  
</manifest>		
</jar>				
    </target>				
	<target name="run" depends="compile">								
		<!--java tag will execute main function from the jar created in compile target section-->	
<java jar="${ytoperation.dir}/YTOperation.jar"fork="true"/>			
</target>				
	</project>				

เรียกใช้ Ant โดยใช้ Eclipse เสียบเข้าไป

วิธีดำเนินการ TestNG รหัสโดยใช้ Ant

ดำเนินงาน TestNG รหัสโดยใช้ Ant

ที่นี่เราจะสร้างชั้นเรียนด้วย ทดสอบ วิธีการและกำหนดเส้นทางคลาสสำหรับ การทดสอบ ใน build.xml

ตอนนี้เพื่อดำเนินการวิธี testng เราจะสร้างไฟล์ testng.xml อีกไฟล์และเรียกไฟล์นี้จากไฟล์ build.xml

ขั้นตอน 1) เราสร้างไฟล์ “Guru99AntClass.คลาส” ในแพ็คเกจ ทดสอบAnt

Guru99AntClass.คลาส

package testAnt;
import java.util.Date;
import org.testng.annotations.Test;		
public class Guru99AntClass {				
    @Test		  
	public void Guru99AntTestNGMethod(){					     
		System.out.println("HELLO GURU99 ANT PROGRAM");					
		System.out.println("TODAY's DATE IS->"+ currentDate() );					
	}		
	public static String currentDate(){					
		return new Date().toString();					
	}		
}		

ขั้นตอนที่ 2) สร้างเป้าหมายเพื่อโหลดคลาสนี้ใน Build.xml

<!-- Load testNG and add to the class path of application -->
	<target name="loadTestNG" depends="setClassPath">
<!—using taskdef  tag we can add a task to run on the current project. In below line, we are adding testing task in this project. Using testing task here now we can run testing code using the ant script -->
		<taskdef resource="testngtasks" classpath="${test.classpath}"/>
</target>

ขั้นตอน 3) สร้าง testng.xml

การทดสอบ ng.xml

<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="YT"thread-count="1">
			<test name="GURU99TestNGAnt">
			<classes>
			   <class name="testAnt.Guru99AntClass">
	</class>
</classes>
</test>
</suite>

ขั้นตอน 4) สร้างบัญชีตัวแทน Target ใน Build.xml เพื่อรันสิ่งนี้ TestNG รหัส

<target name="runGuru99TestNGAnt" depends="compile">
<!-- testng tag will be used to execute testng code using corresponding testng.xml file. Here classpath attribute is setting classpath for testng's jar to the project-->
	<testng classpath="${test.classpath};${build.dir}">
<!—xmlfileset tag is used here to run testng's code using testing.xml file. Using includes tag we are mentioning path to testing.xml file-->
	 <xmlfileset dir="${basedir}" includes="testng.xml"/>
</testng>				

ขั้นตอน 5) Build.xml ที่สมบูรณ์

<?xml version="1.0"encoding="UTF-8"standalone="no"?>
<!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
			<project name="YTMonetize" basedir=".">
		       <!--Property tags will be used as variables in build.xml file to use in further steps-->
			<property name="build.dir"value="${basedir}/build"/>
<!-- put  testng related jar in the resource  folder -->
	      <property name="external.jars" value=".\resource"/>
				<property name="src.dir" value="${basedir}/src"/>
<!--Target tags used as steps that will execute in  sequential order. name attribute will be the name
    of the target and 'depends' attribute used to make one target to depend on another target-->
<!-- Load testNG and add to the class path of application -->
         <target name="loadTestNG"depends="setClassPath">
				<taskdef resource="testngtasks"classpath="${test.classpath}"/>
		</target>
		<target name="setClassPath">
		       <path id="classpath_jars">
					<pathelement path="${basedir}/"/>
					<fileset dir="${external.jars}" includes="*.jar"/>
         </path>
        <pathconvert pathsep=";"property="test.classpath"refid="classpath_jars"/>
	</target>
	<target name="clean">
              <!--echo tag will use to print text on console-->
	               <echo message="deleting existing build directory"/>
               <!--delete tag will clean data from given folder-->
	               <delete				dir="${build.dir}"/>
			</target>
<target name="compile"depends="clean,setClassPath,loadTestNG">
	         <echo message="classpath:${test.classpath}"/>
	               <echo	message="compiling.........."/>
		       <!--mkdir tag will create new director-->
		        <mkdir dir="${build.dir}"/>
					<echo message="classpath:${test.classpath}"/>
			<echo message="compiling.........."/>
	<!--javac tag used to compile java source code and move .class files to a new folder-->
	        <javac destdir="${build.dir}"srcdir="${src.dir}">
	             <classpath refid="classpath_jars"/>
		</javac>
  </target>
<target name="runGuru99TestNGAnt"depends="compile">
		<!-- testng tag will be used to execute testng code using corresponding testng.xml file -->
			<testng classpath="${test.classpath};${build.dir}">
               <xmlfileset dir="${basedir}"includes="testng.xml"/>
	</testng>
</target>
</project>

ขั้นตอน 6) เอาท์พุต

ดำเนินงาน TestNG รหัสโดยใช้ Ant

ดาวน์โหลดไฟล์ด้านบน

มดด้วย Selenium ไดรเวอร์เว็บ

จนถึงตอนนี้ เราได้เรียนรู้ว่าการใช้ ANT ทำให้เราสามารถวาง jar ของบุคคลที่สามทั้งหมดไว้ในตำแหน่งเฉพาะในระบบและกำหนดเส้นทางสำหรับโปรเจ็กต์ของเราได้ เมื่อใช้วิธีนี้ เราจะตั้งค่าการขึ้นต่อกันทั้งหมดของโปรเจ็กต์ของเราไว้ในที่เดียว และทำให้มีความน่าเชื่อถือมากขึ้นสำหรับการรวบรวม การดำเนินการ และการใช้งาน

ในทำนองเดียวกัน สำหรับโครงการทดสอบของเราโดยใช้ selenium เราก็สามารถระบุการอ้างอิง selenium ใน build.xml ได้อย่างง่ายดาย และเราไม่จำเป็นต้องเพิ่มเส้นทางคลาสของมันด้วยตนเองในแอปพลิเคชันของเรา

ตอนนี้คุณสามารถเพิกเฉยต่อวิธีดั้งเดิมที่กล่าวถึงด้านล่างในการตั้งค่า classpaths สำหรับโปรเจ็กต์ได้

มดด้วย Selenium ไดรเวอร์เว็บ

ตัวอย่าง:

เราจะแก้ไขตัวอย่างก่อนหน้านี้

ขั้นตอน 1) ตั้งค่าคุณสมบัติ selenium.jars เป็น jar ที่เกี่ยวข้องกับ selenium ในโฟลเดอร์ทรัพยากร

		<property name="selenium.jars" value=".\selenium"/>

ขั้นตอน 2) ในเป้าหมาย setClassPath เพิ่มไฟล์เซเลเนียม

<target name="setClassPath">
	        <path id="classpath_jars">
				<pathelement path="${basedir}/"/>	
				<fileset dir="${external.jars}" includes="*.jar"/>
	            <!-- selenium jar added here -->
  		            <fileset dir="${selenium.jars}" includes="*.jar"/>
         </path>		

ขั้นตอน 3) สร้าง Build.xml ให้สมบูรณ์:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!--Project tag used to mention the project name, and basedir attribute will be the root directory of the application-->
			<project name="YTMonetize" basedir=".">
                  <!--Property tags will be used as variables in build.xml file to use in further steps-->
				<property name="build.dir" value="${basedir}/build"/>
      <!-- put  testng related jar in the resource  folder -->
	       <property name="external.jars" value=".\resource"/>
<!-- put  selenium related jar in resource  folder -->
     <property name="selenium.jars" value=".\selenium"/>
			<property name="src.dir" value="${basedir}/src"/>
				<!--Target tags used as steps that will execute in  sequential order. name attribute will be the name 
of the target and 'depends' attribute used to make one target to depend on another target-->
      <!-- Load testNG and add to the class path of application -->
       <target name="loadTestNG" depends="setClassPath">
				<taskdef resource="testngtasks" classpath="${test.classpath}"/>
		</target>
<target name="setClassPath">
	        <path id="classpath_jars">
				<pathelement path="${basedir}/"/>
					<fileset dir="${external.jars}" includes="*.jar"/>
			<!-- selenium jar added here -->
	            <fileset dir="${selenium.jars}"includes="*.jar"/>
        </path>
   <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
</target>
<target name="clean">
<!--echo tag will use to print text on console-->
               <echo message="deleting existing build directory"/>
	                <!--delete tag will clean data from given folder-->
		               <delete dir="${build.dir}"/>
				</target>
<target name="compile" depends="clean,setClassPath,loadTestNG">
         <echo message="classpath:${test.classpath}"/>
                <echo message="compiling.........."/>
        <!--mkdir tag will create new director-->
	        <mkdir dir="${build.dir}"/>
          			<echo message="classpath:${test.classpath}"/>
			<echo message="compiling.........."/>
	<!--javac tag used to compile java source code and move .class files to new folder-->
     <javac destdir="${build.dir}"srcdir="${src.dir}">
             <classpath refid="classpath_jars"/>
	</javac>
</target>
<target name="runGuru99TestNGAnt" depends="compile">
		<!-- testng tag will be used to execute testng code using corresponding testng.xml file -->
			<testng classpath="${test.classpath};${build.dir}">
               <xmlfileset dir="${basedir}" includes="testng.xml"/>
		</testng>
	</target>
</project>

ขั้นตอน 4) ตอนนี้เปลี่ยนคลาส Guru99AntClass.java ที่สร้างไว้ก่อนหน้านี้ด้วยรหัสใหม่

ในตัวอย่างนี้ขั้นตอนของเราที่ใช้ Selenium คือ:

  1. ไปที่ https://demo.guru99.com/test/guru99home/
  2. อ่านลิงค์หลักสูตรทั้งหมดทีละรายการ
  3. พิมพ์ไฮเปอร์ลิงก์หลักสูตรทั้งหมดบนคอนโซล

Guru99AntClass.java:

package testAnt;		
import java.util.List;		
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;	
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Guru99AntClass {

	@Test		
		public void Guru99AntTestNGMethod(){
	      WebDriver driver = new FirefoxDriver();	
		  driver.get("https://demo.guru99.com/test/guru99home/");
		  List<WebElement> listAllCourseLinks = driver.findElements(By.xpath("//div[@class='canvas-middle']//a"));							        
          for(WebElement webElement : listAllCourseLinks) {
			System.out.println(webElement.getAttribute("href"));
      	  }
		}
}		

ขั้นตอน 5) หลังจากดำเนินการสำเร็จผลลัพธ์จะมีลักษณะดังนี้:

มดด้วย Selenium ไดรเวอร์เว็บ

ดาวน์โหลดไฟล์ตัวอย่างข้างต้น

สรุป

Ant เป็นเครื่องมือสร้างสำหรับ Java.

Ant ใช้สำหรับการรวบรวมโค้ด การนำไปใช้งาน และกระบวนการดำเนินการ

สามารถดาวน์โหลด Ant ได้จาก อาปาเช่ เว็บไซต์.

ไฟล์ Build.xml ใช้เพื่อกำหนดค่าเป้าหมายการดำเนินการโดยใช้ Ant

สามารถรัน Ant จากบรรทัดคำสั่งหรือปลั๊กอิน IDE ที่เหมาะสม เช่น Eclipse

สรุปโพสต์นี้ด้วย: