Apache ANT với Selenium: Hướng dẫn hoàn chỉnh
Kiến Apache là gì?
Trong khi tạo một sản phẩm phần mềm hoàn chỉnh, người ta cần quan tâm đến API của bên thứ ba khác, đường dẫn lớp của chúng, dọn dẹp các tệp nhị phân thực thi trước đó, biên dịch mã nguồn của chúng ta, thực thi mã nguồn, tạo báo cáo và cơ sở mã triển khai, v.v. Nếu những tác vụ này được thực hiện được thực hiện từng bước một theo cách thủ công, sẽ mất rất nhiều thời gian và quá trình này sẽ dễ xảy ra lỗi.
Ở đây có tầm quan trọng của một công cụ xây dựng như Ant. Nó lưu trữ, thực thi và tự động hóa tất cả quy trình theo thứ tự tuần tự được đề cập trong tệp cấu hình của Ant (thường là build.xml).
Lợi ích của Ant build
- Ant tạo ra vòng đời ứng dụng, tức là dọn dẹp, biên dịch, thiết lập phần phụ thuộc, thực thi, báo cáo, v.v.
- Sự phụ thuộc API của bên thứ ba có thể được thiết lập bởi Ant, tức là đường dẫn lớp của tệp Jar khác được thiết lập bởi tệp xây dựng Ant.
- Một ứng dụng hoàn chỉnh được tạo để phân phối và triển khai End to End.
- Nó là một công cụ xây dựng đơn giản trong đó tất cả các cấu hình có thể được thực hiện bằng tệp XML và có thể được thực thi từ dòng lệnh.
- Nó làm cho mã của bạn rõ ràng vì cấu hình tách biệt với logic ứng dụng thực tế.
Cách cài đặt Ant
Các bước cài đặt Ant trong Windows là như sau
Bước 1) Đến phần https://ant.apache.org/bindownload.cgi Hoặc Tải xuống tệp .zip từ apache-ant-1.9.4-bin.zip
Bước 2) Giải nén thư mục rồi vào copy đường dẫn vào thư mục gốc của thư mục vừa giải nén
Bước 3) Đi tới Bắt đầu -> Máy tính -> nhấp chuột phải vào đây và chọn 'Thuộc tính', sau đó nhấp vào Cài đặt hệ thống nâng cao
Bước 4) Một cửa sổ mới mở ra. Nhấp vào nút 'Biến môi trường…'.
Bước 5) Nhấp vào nút 'Mới…' và đặt tên biến là 'ANT_HOME' và giá trị biến làm đường dẫn gốc đến thư mục đã giải nén và nhấp OK.
Bước 6) bây giờ hãy chọn biến 'Đường dẫn' từ danh sách và nhấp vào 'Chỉnh sửa' và nối thêm; %ANT_HOME%\bin.
Khởi động lại hệ thống một lần và bạn đã sẵn sàng sử dụng công cụ xây dựng Ant ngay bây giờ.
Bước 7) Để kiểm tra phiên bản Ant của bạn bằng dòng lệnh:
Ant –version
Hiểu Build.xml
Build.xml là thành phần quan trọng nhất của công cụ Ant build. Cho một Java dự án, tất cả các nhiệm vụ liên quan đến dọn dẹp, thiết lập, biên dịch và triển khai đều được đề cập trong tệp này ở định dạng XML. Khi chúng tôi thực thi tệp XML này bằng dòng lệnh hoặc bất kỳ plugin IDE nào, tất cả các hướng dẫn được ghi vào tệp này sẽ được thực thi theo cách tuần tự.
Hãy hiểu mã trong một bản dựng mẫu.XML
-
Thẻ dự án được sử dụng để đề cập đến tên dự án và thuộc tính dựa trên nó. Dựa trên là thư mục gốc của ứng dụng
<project name="YTMonetize" basedir=".">
- Thẻ thuộc tính được sử dụng làm biến trong tệp build.XML để sử dụng trong các bước tiếp theo
<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 thẻ được sử dụng như các bước sẽ thực hiện theo thứ tự tuần tự. Thuộc tính tên là tên của mục tiêu. Bạn có thể có nhiều mục tiêu trong một build.xml
<target name="setClassPath">
- thẻ đường dẫn được sử dụng để gói tất cả các tệp một cách hợp lý ở vị trí chung
<path id="classpath_jars">
- thẻ pathelement sẽ đặt đường dẫn đến thư mục gốc của vị trí chung nơi lưu trữ tất cả các tệp
<pathelement path="${basedir}/"/>
- thẻ pathconvert được sử dụng để chuyển đổi đường dẫn của tất cả các tệp chung bên trong thẻ đường dẫn sang định dạng đường dẫn lớp của hệ thống
<pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
- thẻ tập tin được sử dụng để đặt đường dẫn lớp cho jar bên thứ ba khác trong dự án của chúng tôi
<fileset dir="${ytoperation.dir}" includes="*.jar"/>
- Thẻ Echo được sử dụng để in văn bản trên bảng điều khiển
<echo message="deleting existing build directory"/>
- Xóa thẻ sẽ xóa dữ liệu khỏi thư mục đã cho
<delete dir="${build.dir}"/>
- Thẻ mkdir sẽ tạo một thư mục mới
<mkdir dir="${build.dir}"/>
- Thẻ javac được sử dụng để biên dịch mã nguồn java và di chuyển các tệp .class sang thư mục mới
<javac destdir="${build.dir}" srcdir="${src.dir}"> <classpath refid="classpath_jars"/> </javac>
- thẻ jar sẽ tạo tệp jar từ tệp .class
<jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}">
- thẻ kê khai sẽ đặt lớp chính của bạn để thực thi
<manifest> <attribute name="Main-Class" value="test.Main"/> </manifest>
- Thuộc tính 'phụ thuộc' được sử dụng để làm cho một mục tiêu phụ thuộc vào mục tiêu khác
<target name="run" depends="compile">
- Thẻ java sẽ thực thi chức năng chính từ jar được tạo trong phần mục tiêu biên dịch
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>
Chạy Ant bằng cách sử dụng Eclipse Plugin
Để chạy Ant từ eclipse, hãy vào tệp build.xml -> nhấp chuột phải vào tệp -> Chạy dưới dạng… -> nhấp vào tệp Build
Ví dụ
Chúng ta sẽ lấy một chương trình mẫu nhỏ để giải thích rất rõ ràng về chức năng của Ant. Cấu trúc dự án của chúng tôi sẽ trông giống như –
Ở đây trong ví dụ này chúng ta có 4 mục tiêu
- Đặt đường dẫn lớp cho các lọ bên ngoài,
- Làm sạch mã đã tuân thủ trước đó
- Biên dịch mã java hiện có
- Chạy mã
Guru99AntClass.class
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(); } }
Bản dựng.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>
Cách thực hiện TestNG mã bằng Ant
Ở đây chúng ta sẽ tạo một lớp với Kiểm tra phương thức và thiết lập đường dẫn lớp cho Kiểm tra trong build.xml.
Bây giờ để thực thi phương thức testng, chúng ta sẽ tạo một tệp testng.xml khác và gọi tệp này từ tệp build.xml.
Bước 1) Chúng tôi tạo ra một “Guru99AntClass.class” trong gói kiểm tra kiến
Guru99AntClass.class
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(); } }
Bước 2) Tạo mục tiêu để tải lớp này trong 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>
Bước 3) Tạo testng.xml
testng.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>
Bước 4) Tạo Target trong Build.xml để chạy cái này TestNG mã
<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>
Bước 5) Build.xml hoàn chỉnh
<?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>
Bước 6) Đầu ra
Kiến với Selenium webdriver
Cho đến nay, chúng tôi đã biết rằng bằng cách sử dụng ANT, chúng tôi có thể đặt tất cả các lọ của bên thứ ba vào một vị trí cụ thể trong hệ thống và đặt đường dẫn cho dự án của chúng tôi. Bằng cách sử dụng phương pháp này, chúng tôi đang đặt tất cả các phần phụ thuộc của dự án ở một nơi duy nhất và làm cho dự án trở nên đáng tin cậy hơn trong quá trình biên dịch, thực thi và triển khai.
Tương tự, đối với các dự án thử nghiệm sử dụng selen, chúng ta có thể dễ dàng đề cập đến sự phụ thuộc của selen trong build.xml và chúng ta không cần thêm đường dẫn lớp của nó theo cách thủ công vào ứng dụng của mình.
Vì vậy, bây giờ bạn có thể bỏ qua cách truyền thống được đề cập dưới đây để thiết lập đường dẫn lớp cho dự án.
Ví dụ:
Chúng tôi sẽ sửa đổi ví dụ trước
Bước 1) Đặt thuộc tính Selenium.jars thành jar liên quan đến Selenium trong thư mục tài nguyên
<property name="selenium.jars" value=".\selenium"/>
Bước 2) Trong setClassPath đích, hãy thêm các tệp selen
<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>
Bước 3) Hoàn thành 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>
Bước 4) Bây giờ hãy thay đổi lớp Guru99AntClass.java đã tạo trước đó bằng mã mới.
Ở đây trong ví dụ này các bước của chúng tôi sử dụng Selenium là:
- Đến phần https://demo.guru99.com/test/guru99home/
- Đọc tất cả các liên kết khóa học từng cái một
- In siêu liên kết tất cả các khóa học trên bảng điều khiển.
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")); } } }
Bước 5) Sau khi thực hiện thành công, kết quả sẽ như sau:
Tổng kết
Ant là một công cụ xây dựng cho Java.
Ant dùng để biên dịch, triển khai, thực thi mã.
Ant có thể được tải xuống từ Apache trang web.
Tệp Build.xml dùng để định cấu hình mục tiêu thực thi bằng Ant.
Ant có thể được chạy từ dòng lệnh hoặc plugin IDE phù hợp như Eclipse.