<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 帶有 Selenium 的 Apache ANT:完整教程 > 原文: [https://www.guru99.com/using-apache-ant-with-selenium.html](https://www.guru99.com/using-apache-ant-with-selenium.html) ## **什么是 Apache Ant?** 在創建完整的軟件產品時,需要注意不同的第三方 API,它們的類路徑,清理先前的可執行二進制文件,編譯我們的源代碼,執行源代碼,創建報告和部署代碼庫等。如果完成了這些任務 一個接一個地手動操作,將花費大量時間,并且該過程容易出錯。 這就是像 Ant 這樣的構建工具的重要性。 它按 Ant 配置文件(通常為 build.xml)中提到的順序存儲,執行和自動化所有過程。 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/9b/0a/9b0ac198a0538783bb40f55974f43869_830x353.png "Using Apache Ant with Selenium") ## 螞蟻建造的好處 1. Ant 會創建應用程序生命周期,即清理,編譯,設置依賴項,執行,報告等。 2. 第三方 API 依賴性可以由 Ant 設置,即其他 Jar 文件的類路徑由 Ant 構建文件設置。 3. 為端到端交付和部署創建了一個完整的應用程序。 4. 這是一個簡單的構建工具,其中所有配置都可以使用 XML 文件完成,并且可以從命令行執行。 5. 由于配置與實際的應用程序邏輯分開,因此可以使您的代碼清晰。 ## 如何安裝 Ant 在 Windows 中安裝 Ant 的步驟如下 **步驟 1)**轉到 [http://ant.apache.org/bindownload.cgi](http://ant.apache.org/bindownload.cgi) 從 [apache-ant-1.9.4-bin.zip 下載.zip 文件](http://apache.bytenet.in/ant/binaries/apache-ant-1.9.4-bin.zip) [](http://apache.bytenet.in/ant/binaries/apache-ant-1.9.4-bin.zip)![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/87/3a/873a8a0d5e1cecf79f87e774d476f925_615x159.png "Using Apache Ant with Selenium") **步驟 2)**解壓縮該文件夾,然后轉到路徑并將其復制到未壓縮文件夾的根目錄 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/cb/31/cb3198dbace0fb9f6f754c8c9f233f98_626x181.png "Using Apache Ant with Selenium") **步驟 3)**轉到開始->計算機->右鍵單擊此處并選擇“屬性”,然后單擊高級系統設置 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/67/94/6794d8c9da41b9cb33609b00905a157b_552x454.png "Using Apache Ant with Selenium") **步驟 4)**將打開一個新窗口。 單擊“環境變量...”按鈕。 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/50/3e/503ee0524cd45b5aed2458b1c7157224_436x216.png "Using Apache Ant with Selenium") **步驟 5)**單擊“新建…”按鈕,并將變量名稱設置為“ ANT_HOME”,并將變量值設置為根 壓縮文件夾的路徑,然后單擊確定。 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/1b/2b/1b2bc9c42c3a5013f990cf64c5e57b11_775x415.png "Using Apache Ant with Selenium") **步驟 6)**現在從列表中選擇“路徑”變量,然后單擊“編輯”并附加; %ANT_HOME%\ bin。 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/1c/24/1c24329fbc3c45d656f7a4f21dd0ac8b_588x507.png "Using Apache Ant with Selenium") 一次重新啟動系統,您現在就可以使用 Ant 構建工具。 **步驟 7)**要使用命令行檢查 Ant 的版本: Ant –version ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/7e/ce/7ece6d6a9d23a40440a6ebfb8c6e8982_589x116.png "Using Apache Ant with Selenium") ## 了解 Build.xml Build.xml 是 Ant 構建工具最重要的組件。 對于 [Java](/java-tutorial.html) 項目,此文件中以 XML 格式提到了所有與清理,設置,編譯和部署相關的任務。 當我們使用命令行或任何 IDE 插件執行此 XML 文件時,寫入該文件的所有指令都將按順序執行。 讓我們了解示例構建中的代碼。XML * Project 標簽用于提及項目名稱和 basedir 屬性。 basedir 是應用程序的根目錄 ``` <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"/> ``` * 目標標簽用作將按順序執行的步驟。 Name 屬性是目標的名稱。 您可以在一個 build.xml 中包含多個目標。 ``` <target name="setClassPath"> ``` * 路徑標記用于邏輯上捆綁位于公共位置 ``` <path id="classpath_jars"> ``` * pathelement 標記會將路徑設置為存儲所有文件的公共位置的根。 ``` <pathelement path="${basedir}/"/> ``` * pathconvert 標記,用于轉換路徑內所有公共文件的路徑 標記為系統的類路徑格式 ``` <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/> ``` * 文件集標記,用于為我們的項目中的其他第三方 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 文件移動到 ew 文件夾 ``` <javac destdir="${build.dir}" srcdir="${src.dir}"> <classpath refid="classpath_jars"/> </javac> ``` * jar 標簽將從.class 文件創建 jar 文件。 ``` <jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}"> ``` * 清單標簽將設置您的 執行的主要類別 ``` <manifest> <attribute name="Main-Class" value="test.Main"/> </manifest> ``` * 'depends'屬性,用于使一個目標依賴于另一個目標 ``` <target name="run" depends="compile"> ``` * java 標記將從編譯目標部分 ``` <java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/> ``` 中創建的 jar 中執行主要功能 ## 使用 Eclipse 插件運行 Ant 要從 eclipse 運行 Ant,請轉到 build.xml 文件->右鍵單擊文件->運行為…->單擊生成文件 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/fa/a3/faa35f66f08262faf86944123b0bd25a_823x471.png "Using Apache Ant with Selenium") ## 例: 我們將使用一個小示例程序,該程序將非常清楚地解釋 Ant 功能。 我們的項目結構看起來像– ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/52/6a/526af3c281e95c28ffd602f7320f0860_420x237.png "Using Apache Ant with Selenium") 在此示例中,我們有 4 個目標 1. 設置外部罐子的類路徑, 2. 清理以前編譯的代碼 3. 編譯現有的 Java 代碼 4. 運行代碼 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> ``` [![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/b4/fd/b4fd0ccb6172c1801d2abbd8626a8ef5_836x637.png "Using Apache Ant with Selenium") ](/images/1-2015/031015_1057_UsingApache11.png) ## 如何使用 Ant 執行 TestNG 代碼 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/42/bf/42bf5fd8ecd90453ac41e32b52a172d8_597x271.png "Using Apache Ant with Selenium") 在這里,我們將使用 [Testng](/all-about-testng-and-selenium.html) 方法創建一個類,并在 build.xml 中設置用于[測試](/software-testing.html)的類路徑。 現在執行 testng 方法,我們將創建另一個 testng.xml 文件,并從 build.xml 文件中調用該文件。 **步驟 1)**我們在程序包 **testAnt** 中創建了一個**“** **Guru99AntClass.class”** 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(); } } ``` **步驟 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 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> ``` **步驟 4)**在 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)**輸出 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/6f/9d/6f9d2b41712b27a00c6a738bd7d78644_885x440.png "Using Apache Ant with Selenium") [下載上述文件](https://drive.google.com/uc?export=download&id=0B_vqvT0ovzHceGJQbi12Q013VlE) ## 帶有 Selenium Webdriver 的 Ant: 到目前為止,我們已經了解到,使用 ANT,我們可以將所有第三方 jar 放在系統中的特定位置,并為我們的項目設置它們的路徑。 使用這種方法,我們可以在一個地方設置項目的所有依賴項,并使它對編譯,執行和部署更加可靠。 同樣,對于我們使用硒的測試項目,我們可以在 build.xml 中輕松提及硒依賴性,并且不需要在應用程序中手動添加它的類路徑。 因此,現在您可以忽略下面提到的為項目設置類路徑的傳統方法。 ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/cc/9d/cc9d3fda4f407f7df17e7070c36e67c7_730x548.png "Using Apache Ant with Selenium") 例: 我們將修改前面的示例 **步驟 1)**將屬性 selenium.jars 設置為資源文件夾中與硒相關的 jar ``` <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。 在此示例中,我們使用硒的步驟為: 1. 轉到 [http://demo.guru99.com/test/guru99home/](http://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("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")); } } } ``` **步驟 5)**成功執行后,輸出將如下所示: ![Apache ANT with Selenium: Complete Tutorial](https://img.kancloud.cn/f7/07/f707ea75f0636fa5a3e031ec4f628400_994x476.png "Using Apache Ant with Selenium") [下載以上示例文件](https://drive.google.com/uc?export=download&id=0B_vqvT0ovzHcOW9WZlprdy1nNWs) **摘要:** Ant 是 Java 的構建工具。 Ant 用于代碼編譯,部署,執行過程。 可以從 [Apache](/apache.html) 網站下載 Ant。 Build.xml 文件,用于使用 Ant 配置執行目標。 可以從命令行或合適的 IDE 插件(如 eclipse)運行 Ant。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看