Apache ANT sa Selenium: Potpuni vodič
Što je Apache Ant?
Prilikom izrade cjelovitog softverskog proizvoda potrebno je voditi računa o različitim API-jima trećih strana, njihovoj stazi klasa, čišćenju prethodnih izvršnih binarnih datoteka, kompajliranju našeg izvornog koda, izvršavanju izvornog koda, stvaranju izvješća i baze koda za implementaciju itd. Ako su ovi zadaci obavljati jedan po jedan ručno, to će oduzeti ogromno vrijeme, a proces će biti sklon pogreškama.
Ovdje dolazi do važnosti alata za izgradnju kao što je Ant. Pohranjuje, izvršava i automatizira sve procese redoslijedom navedenim u Antovoj konfiguracijskoj datoteci (obično build.xml).
Prednost Ant builda
- Ant stvara životni ciklus aplikacije, tj. čisti, kompilira, postavlja ovisnost, izvršava, izvješćuje itd.
- Ovisnost o API-ju treće strane može postaviti Ant, tj. putanju klase druge Jar datoteke postavlja Ant build datoteka.
- Izrađena je potpuna aplikacija za isporuku i implementaciju od kraja do kraja.
- To je jednostavan alat za izradu u kojem se sve konfiguracije mogu izvršiti pomoću XML datoteke i koji se može izvršiti iz naredbenog retka.
- Čini vaš kod čistim jer je konfiguracija odvojena od stvarne logike aplikacije.
Kako instalirati Ant
Koraci za instaliranje Anta Windows je kako slijedi
Korak 1) Idi na https://ant.apache.org/bindownload.cgi Ili preuzmite .zip datoteku s apache-ant-1.9.4-bin.zip
Korak 2) Raspakirajte mapu i idite na i kopirajte putanju do korijena raspakirane mape
Korak 3) Idite na Start -> Računalo -> kliknite ovdje desnom tipkom i odaberite 'Svojstva' zatim kliknite na Napredne postavke sustava
Korak 4) Otvara se novi prozor. Kliknite gumb "Varijabla okoline...".
Korak 5) Pritisnite gumb "Novo..." i postavite naziv varijable kao "ANT_HOME" i vrijednost varijable kao korijenski put do raspakovane mape i kliknite OK.
Korak 6) sada odaberite varijablu 'Path' s popisa i kliknite 'Edit' i dodajte; %ANT_HOME%\bin.
Ponovno pokrenite sustav jednom i sada ste spremni za korištenje Ant build alata.
Korak 7) Za provjeru verzije Anta pomoću naredbenog retka:
Mrav – verzija
Razumijevanje Build.xml
Build.xml je najvažnija komponenta Ant alata za izgradnju. Za Java projekta, svi zadaci povezani s čišćenjem, postavljanjem, kompilacijom i implementacijom spominju se u ovoj datoteci u XML formatu. Kada izvršimo ovu XML datoteku pomoću naredbenog retka ili bilo kojeg IDE dodatka, sve upute zapisane u ovu datoteku izvršit će se u sekvencijalnom načinu.
Hajdemo razumjeti kod unutar uzorka build.XML
-
Oznaka projekta koristi se za spominjanje naziva projekta i atributa basedir. Basedir je korijenski direktorij aplikacije
<project name="YTMonetize" basedir=".">
- Oznake svojstava koriste se kao varijable u build.XML datoteci koje se koriste u daljnjim koracima
<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 oznake koje se koriste kao koraci koji će se izvršavati uzastopnim redoslijedom. Naziv atributa je naziv cilja. Možete imati više ciljeva u jednom build.xml
<target name="setClassPath">
- oznaka staze koristi se za logično grupiranje svih datoteka koje se nalaze na zajedničkoj lokaciji
<path id="classpath_jars">
- Oznaka pathelement će postaviti put do korijena zajedničke lokacije gdje su pohranjene sve datoteke
<pathelement path="${basedir}/"/>
- pathconvert oznaka koja se koristi za pretvaranje staza svih uobičajenih datoteka unutar oznake staze u sistemski format classpath
<pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
- Oznaka skupa datoteka koja se koristi za postavljanje staze klasa za različite jar treće strane u našem projektu
<fileset dir="${ytoperation.dir}" includes="*.jar"/>
- Echo tag se koristi za ispis teksta na konzoli
<echo message="deleting existing build directory"/>
- Brisanje oznake će očistiti podatke iz dane mape
<delete dir="${build.dir}"/>
- mkdir oznaka će stvoriti novi direktorij
<mkdir dir="${build.dir}"/>
- javac oznaka koja se koristi za kompajliranje java izvornog koda i premještanje .class datoteka u novu mapu
<javac destdir="${build.dir}" srcdir="${src.dir}"> <classpath refid="classpath_jars"/> </javac>
- jar oznaka će stvoriti jar datoteku iz .class datoteka
<jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}">
- manifest oznaka će postaviti vašu glavnu klasu za izvršenje
<manifest> <attribute name="Main-Class" value="test.Main"/> </manifest>
- 'ovisi' atribut koji se koristi da bi jedan cilj bio ovisan o drugom cilju
<target name="run" depends="compile">
- java oznaka izvršit će glavnu funkciju iz jar stvorene u ciljnom odjeljku kompajliranja
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>
Pokreni Ant pomoću Eclipse uključiti
Za pokretanje Anta iz eclipse idite na datoteku build.xml -> desni klik na datoteku -> Pokreni kao… -> kliknite datoteku Build.
Primjer
Uzet ćemo mali ogledni program koji će vrlo jasno objasniti Ant funkcionalnost. Struktura našeg projekta izgledat će ovako –
Ovdje u ovom primjeru imamo 4 cilja
- Postavite put klase za vanjske posude,
- Očistite prethodno usklađeni kod
- Kompajlirajte postojeći java kod
- Pokrenite kod
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(); } }
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>
Kako izvršiti TestNG kod pomoću Ant
Ovdje ćemo stvoriti klasu sa Testng metode i postaviti put klase za Ispitivanje u build.xml.
Da bismo sada izvršili metodu testng, stvorit ćemo drugu datoteku testng.xml i pozvati ovu datoteku iz datoteke build.xml.
Korak 1) Mi stvaramo a “Guru99AntClass.class” u paketu testAnt
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(); } }
Korak 2) Stvorite cilj za učitavanje ove klase u 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>
Korak 3) Stvorite 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>
Korak 4) stvoriti Target u Build.xml za pokretanje ovog TestNG kod
<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>
Korak 5) Kompletan 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>
Korak 6) Izlaz
Mrav sa Selenium Webdriver
Do sada smo naučili da korištenjem ANT-a možemo staviti sve posude trećih strana na određeno mjesto u sustavu i postaviti njihov put za naš projekt. Pomoću ove metode postavljamo sve ovisnosti našeg projekta na jednom mjestu i činimo ga pouzdanijim za kompilaciju, izvođenje i implementaciju.
Slično tome, za naše projekte testiranja koji koriste selenium, možemo lako spomenuti ovisnost o selenu u build.xml i ne moramo ručno dodavati stazu klase za to u našoj aplikaciji.
Dakle, sada možete zanemariti dolje spomenuti tradicionalni način postavljanja staza klasa za projekt.
Primjer:
Modificirat ćemo prethodni primjer
Korak 1) Postavite svojstvo selenium.jars na jar povezan sa selenom u mapi resursa
<property name="selenium.jars" value=".\selenium"/>
Korak 2) U ciljni setClassPath dodajte datoteke selena
<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>
Korak 3) Kompletan 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>
Korak 4) Sada promijenite prethodno stvorenu klasu Guru99AntClass.java novim kodom.
Ovdje u ovom primjeru naši koraci koriste Selenium je:
- Idi na
http://demo.guru99.com/test/guru99home/
- Pročitajte sve veze na tečajeve jednu po jednu
- Ispis hiperveze svih tečajeva na konzoli.
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("http://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")); } } }
Korak 5) Nakon uspješnog izvođenja izlaz će izgledati ovako:
Preuzmite gornju datoteku primjera
rezime
Ant je alat za izgradnju Java.
Ant koji se koristi za kompilaciju koda, implementaciju, proces izvršenja.
Ant se može preuzeti sa apaš web stranicu.
Datoteka Build.xml koja se koristi za konfiguriranje ciljeva izvršenja pomoću Ant.
Ant se može pokrenuti iz naredbenog retka ili odgovarajućeg IDE dodatka kao što je eclipse.