Apache ANT と Selenium: 完全なチュートリアル

アパッチアントとは?

完全なソフトウェア製品を作成する際には、さまざまなサードパーティ API、それらのクラスパス、以前の実行可能バイナリ ファイルのクリーニング、ソース コードのコンパイル、ソース コードの実行、レポートの作成、コード ベースの配置などを処理する必要があります。これらのタスクがXNUMX つずつ手動で行うと、膨大な時間がかかり、プロセスでエラーが発生しやすくなります。

Ant のようなビルド ツールの重要性はここにあります。 Ant の構成ファイル (通常は build.xml) に記載されている順序で、すべてのプロセスを保存、実行、および自動化します。

アパッチアント

Ant ビルドの利点

  1. Ant は、クリーン、コンパイル、依存関係の設定、実行、レポートなど、アプリケーションのライフサイクルを作成します。
  2. サード パーティ API の依存関係は Ant によって設定できます。つまり、他の Jar ファイルのクラス パスは Ant ビルド ファイルによって設定されます。
  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) [スタート] -> [コンピューター] -> ここを右クリックして [プロパティ] を選択し、[システムの詳細設定] をクリックします。

Antをインストールする

ステップ4) 新しいウィンドウが開きます。 「環境変数…」ボタンをクリックします。

Antをインストールする

ステップ5) 「新規…」ボタンをクリックし、変数名を「ANT_HOME」に、変数値を解凍したフォルダーへのルートパスとして設定し、「OK」をクリックします。

Antをインストールする

ステップ6) リストから「パス」変数を​​選択し、「編集」をクリックして追加します。 %ANT_HOME%\bin.

Antをインストールする

システムを一度再起動すると、Ant ビルド ツールを使用する準備が整います。

ステップ7) コマンドラインを使用して Ant のバージョンを確認するには:

Ant – バージョン

Antをインストールする

Build.xml について

Build.xml は、Ant ビルド ツールの最も重要なコンポーネントです。 のために Java プロジェクト、すべてのクリーニング、セットアップ、コンパイル、および展開に関連するタスクは、このファイルに XML 形式で記載されています。 コマンド ラインまたは任意の IDE プラグインを使用してこの XML ファイルを実行すると、このファイルに書き込まれたすべての命令が順次実行されます。

サンプル build.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"/>
  • Target 順番に実行されるステップとして使用されるタグ。Name 属性はターゲットの名前です。1 つの build.xml に複数のターゲットを含めることができます。
    <target name="setClassPath">
  • パスタグは、共通の場所にあるすべてのファイルを論理的にバンドルするために使用されます
    <path id="classpath_jars">
  • pathelement タグは、すべてのファイルが保存されている共通の場所のルートへのパスを設定します
    <pathelement path="${basedir}/"/>
  • path タグ内のすべての共通ファイルのパスをシステムのクラスパス形式に変換するために使用される pathconvert タグ
    <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
  • プロジェクト内のさまざまなサードパーティ jar のクラスパスを設定するために使用される fileset タグ
    <fileset dir="${ytoperation.dir}" includes="*.jar"/>
  • エコータグは、コンソールにテキストを出力するために使用されます
<echo message="deleting existing build directory"/>
  • タグを削除すると、指定されたフォルダーからデータが削除されます
<delete dir="${build.dir}"/>
  • mkdir タグは新しいディレクトリを作成します
	<mkdir dir="${build.dir}"/>
  • Java ソース コードをコンパイルし、.class ファイルを新しいフォルダに移動するために使用される javac タグ
        <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 タグは、コンパイル ターゲット セクションで作成された jar から main 関数を実行します。
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>

次を使用して Ant を実行します Eclipse プラグイン

EclipseからAntを実行するには、build.xmlファイルに移動し、ファイルを右クリック→実行…→ファイルのビルドをクリックします。

次を使用して Ant を実行します Eclipse プラグイン

Ant の機能を非常に明確に説明する小さなサンプル プログラムを取り上げます。 私たちのプロジェクト構造は次のようになります -

次を使用して Ant を実行します Eclipse プラグイン

この例では、4 つのターゲットがあります。

  1. 外部jarのクラスパスを設定し、
  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>				

次を使用して Ant を実行します Eclipse プラグイン

実行方法 TestNG Antを使用したコード

実行する TestNG Antを使用したコード

ここでは、クラスを作成します テスト メソッドとクラスパスの設定 テスト build.xml で。

ここで、testng メソッドを実行するために、別の testng.xml ファイルを作成し、build.xml ファイルからこのファイルを呼び出します。

ステップ1) 作成します 「Guru99AntClass.クラス」 パッケージで テストアント

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 を作成する

テスト

<?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 をシステム内の特定の場所に配置し、プロジェクトのパスを設定できることを学びました。 この方法を使用して、プロジェクトのすべての依存関係を XNUMX か所で設定し、コンパイル、実行、および展開の信頼性を高めます。

同様に、Selenium を使用したテスト プロジェクトでは、build.xml で Selenium の依存関係を簡単に記述でき、アプリケーションに手動でクラス パスを追加する必要はありません。

したがって、プロジェクトのクラスパスを設定する以下の従来の方法を無視できます。

アリと Selenium ウェブドライバー

例:

前の例を変更します

ステップ1) リソースフォルダ内のSelenium関連のjarにプロパティselenium.jarsを設定します。

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

ステップ2) ターゲットsetClassPathにSeleniumファイルを追加します

<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. に行く http://demo.guru99.com/test/guru99home/
  2. すべてのコースのリンクを XNUMX つずつ読む
  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) 実行が成功すると、出力は次のようになります。

アリと Selenium ウェブドライバー

上記のサンプル ファイルをダウンロードします。

まとめ

Antはビルドツールです Java.

コードのコンパイル、展開、実行プロセスに使用される Ant。

Ant は以下からダウンロードできます。 アパッチ ウェブサイトをご覧ください。

Ant を使用して実行ターゲットを構成するために使用される Build.xml ファイル。

Ant は、コマンド ラインまたは Eclipse などの適切な IDE プラグインから実行できます。