अपाचे एएनटी के साथ Selenium: पूरा ट्यूटोरियल

अपाचे एंट क्या है?

एक पूर्ण सॉफ्टवेयर उत्पाद बनाते समय, हमें विभिन्न तृतीय पक्ष API, उनके क्लासपाथ, पिछली निष्पादन योग्य बाइनरी फ़ाइलों की सफाई, हमारे स्रोत कोड को संकलित करना, स्रोत कोड का निष्पादन, रिपोर्ट का निर्माण और परिनियोजन कोड बेस आदि का ध्यान रखना पड़ता है। यदि ये कार्य एक-एक करके मैन्युअल रूप से किए जाते हैं, तो इसमें बहुत समय लगेगा, और प्रक्रिया में त्रुटियां होने की संभावना रहेगी।

यहाँ एंट जैसे बिल्ड टूल का महत्व आता है। यह एंट की कॉन्फ़िगरेशन फ़ाइल (आमतौर पर build.xml) में उल्लिखित अनुक्रमिक क्रम में सभी प्रक्रियाओं को संग्रहीत, निष्पादित और स्वचालित करता है।

अपाचे चींटी

चींटी निर्माण का लाभ

  1. चींटी एप्लिकेशन जीवन चक्र बनाती है, अर्थात साफ करना, संकलित करना, निर्भरता निर्धारित करना, निष्पादित करना, रिपोर्ट करना आदि।
  2. तृतीय पक्ष API निर्भरता को Ant द्वारा सेट किया जा सकता है, अर्थात अन्य Jar फ़ाइल का क्लास पथ Ant बिल्ड फ़ाइल द्वारा सेट किया जाता है।
  3. संपूर्ण अनुप्रयोग को एंड टू एंड वितरण और परिनियोजन के लिए बनाया गया है।
  4. यह एक सरल बिल्ड टूल है जहां सभी कॉन्फ़िगरेशन XML फ़ाइल का उपयोग करके किए जा सकते हैं और जिसे कमांड लाइन से निष्पादित किया जा सकता है।
  5. यह आपके कोड को साफ़ बनाता है क्योंकि कॉन्फ़िगरेशन वास्तविक एप्लिकेशन लॉजिक से अलग होता है।

एंट कैसे स्थापित करें

एंट को स्थापित करने के चरण Windows इस प्रकार है

चरण 1) https://ant.apache.org/bindownload.cgi या .zip फ़ाइल यहाँ से डाउनलोड करें अपाचे-ant-1.9.4-bin.zip

चींटी स्थापित करें

चरण 2) फ़ोल्डर को अनज़िप करें और अनज़िप किए गए फ़ोल्डर के रूट पर जाकर पथ को कॉपी करें

चींटी स्थापित करें

चरण 3) स्टार्ट -> कंप्यूटर -> पर जाएं यहां राइट क्लिक करें और 'प्रॉपर्टीज' चुनें फिर एडवांस्ड सिस्टम सेटिंग्स पर क्लिक करें

चींटी स्थापित करें

चरण 4) एक नई विंडो खुलेगी। 'पर्यावरण चर...' बटन पर क्लिक करें।

चींटी स्थापित करें

चरण 5) 'नया...' बटन पर क्लिक करें और चर नाम को 'ANT_HOME' के रूप में सेट करें और चर मान को अनज़िप किए गए फ़ोल्डर के रूट पथ के रूप में सेट करें और ओके पर क्लिक करें।

चींटी स्थापित करें

चरण 6) अब सूची से 'पथ' चर का चयन करें और 'संपादन' पर क्लिक करें और संलग्न करें; %ANT_HOME%\bin.

चींटी स्थापित करें

सिस्टम को एक बार पुनः आरंभ करें और अब आप एंट बिल्ड टूल का उपयोग करने के लिए तैयार हैं।

चरण 7) कमांड लाइन का उपयोग करके अपने Ant का संस्करण जांचने के लिए:

चींटी-संस्करण

चींटी स्थापित करें

Build.xml को समझना

Build.xml एंट बिल्ड टूल का सबसे महत्वपूर्ण घटक है। Java प्रोजेक्ट, सभी सफाई, सेटअप, संकलन और परिनियोजन से संबंधित कार्य इस फ़ाइल में XML प्रारूप में उल्लिखित हैं। जब हम कमांड लाइन या किसी IDE प्लगइन का उपयोग करके इस XML फ़ाइल को निष्पादित करते हैं, तो इस फ़ाइल में लिखे गए सभी निर्देश अनुक्रमिक तरीके से निष्पादित होंगे।

आइए नमूना build.XML के कोड को समझें

  • प्रोजेक्ट टैग का उपयोग प्रोजेक्ट नाम और 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 टैग का उपयोग चरणों के रूप में किया जाता है जो क्रमिक क्रम में निष्पादित होंगे। नाम विशेषता लक्ष्य का नाम है। आप एक ही build.xml में कई लक्ष्य रख सकते हैं
    <target name="setClassPath">
  • पथ टैग का उपयोग सभी फ़ाइलों को तार्किक रूप से बंडल करने के लिए किया जाता है जो सामान्य स्थान पर हैं
    <path id="classpath_jars">
  • पथएलिमेंट टैग उस सामान्य स्थान के मूल पथ को सेट करेगा जहां सभी फ़ाइलें संग्रहीत हैं
    <pathelement path="${basedir}/"/>
  • पथकन्वर्ट टैग का उपयोग पथ टैग के अंदर सभी सामान्य फ़ाइलों के पथों को सिस्टम के क्लासपथ प्रारूप में परिवर्तित करने के लिए किया जाता है
    <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
  • फ़ाइलसेट टैग का उपयोग हमारे प्रोजेक्ट में विभिन्न तृतीय पक्ष जार के लिए क्लासपाथ सेट करने के लिए किया जाता है
    <fileset dir="${ytoperation.dir}" includes="*.jar"/>
  • इको टैग का उपयोग कंसोल पर टेक्स्ट प्रिंट करने के लिए किया जाता है
<echo message="deleting existing build directory"/>
  • टैग हटाने से दिए गए फ़ोल्डर से डेटा साफ़ हो जाएगा
<delete dir="${build.dir}"/>
  • mkdir टैग एक नई निर्देशिका बनाएगा
	<mkdir dir="${build.dir}"/>
  • javac टैग का उपयोग जावा स्रोत कोड को संकलित करने और .class फ़ाइलों को एक नए फ़ोल्डर में ले जाने के लिए किया जाता है
        <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>
  • 'निर्भर करता है' विशेषता का उपयोग एक लक्ष्य को दूसरे लक्ष्य पर निर्भर बनाने के लिए किया जाता है
<target name="run" depends="compile">
  • जावा टैग संकलन लक्ष्य अनुभाग में बनाए गए जार से मुख्य फ़ंक्शन निष्पादित करेगा
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>

Ant का उपयोग करके चलाएँ Eclipse लगाना

एक्लिप्स से एंट चलाने के लिए build.xml फ़ाइल पर जाएँ -> फ़ाइल पर राइट क्लिक करें -> Run as… -> बिल्ड फ़ाइल पर क्लिक करें

Ant का उपयोग करके चलाएँ Eclipse लगाना

उदाहरण

हम एक छोटा सा नमूना कार्यक्रम लेंगे जो एंट की कार्यक्षमता को बहुत स्पष्ट रूप से समझाएगा। हमारी परियोजना संरचना इस प्रकार होगी -

Ant का उपयोग करके चलाएँ Eclipse लगाना

इस उदाहरण में हमारे पास 4 लक्ष्य हैं

  1. बाहरी जार के लिए क्लास पथ सेट करें,
  2. पहले से संकलित कोड को साफ़ करें
  3. मौजूदा जावा कोड संकलित करें
  4. कोड चलाएँ

गुरु99AntClass.क्लास

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();					  
	}		
}

बिल्ड.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 चींटी का उपयोग कर कोड

निष्पादित करना TestNG चींटी का उपयोग कर कोड

यहाँ हम एक क्लास बनाएंगे टेस्टिंग विधियाँ और के लिए वर्ग पथ सेट करें परीक्षण build.xml में.

अब testng विधि को निष्पादित करने के लिए हम एक और testng.xml फ़ाइल बनाएंगे और build.xml फ़ाइल से इस फ़ाइल को कॉल करेंगे।

चरण 1) हम एक बनाते हैं “गुरु99एंटक्लास.क्लास” पैकेज में टेस्टएंट

गुरु99AntClass.क्लास

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) बनाएं 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 चींटी का उपयोग कर कोड

उपरोक्त फ़ाइल डाउनलोड करें

चींटी के साथ Selenium वेबड्राइवर

अब तक, हमने सीखा है कि ANT का उपयोग करके हम सभी थर्ड पार्टी जार को सिस्टम में एक विशेष स्थान पर रख सकते हैं और अपने प्रोजेक्ट के लिए उनका पथ निर्धारित कर सकते हैं। इस विधि का उपयोग करके हम अपने प्रोजेक्ट की सभी निर्भरताओं को एक ही स्थान पर सेट कर रहे हैं और इसे संकलन, निष्पादन और परिनियोजन के लिए अधिक विश्वसनीय बना रहे हैं।

इसी प्रकार, सेलेनियम का उपयोग करने वाली हमारी परीक्षण परियोजनाओं के लिए, हम build.xml में सेलेनियम निर्भरता का आसानी से उल्लेख कर सकते हैं और हमें अपने अनुप्रयोग में मैन्युअल रूप से इसका क्लास पथ जोड़ने की आवश्यकता नहीं है।

तो अब आप प्रोजेक्ट के लिए क्लासपाथ सेट करने के लिए नीचे उल्लिखित पारंपरिक तरीके को अनदेखा कर सकते हैं।

चींटी के साथ Selenium वेबड्राइवर

उदाहरण:

हम पिछले उदाहरण को संशोधित करने जा रहे हैं

चरण 1) संसाधन फ़ोल्डर में selenium.jars प्रॉपर्टी को selenium से संबंधित 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 को नए कोड से बदलें।

इस उदाहरण में हमारे चरण इस प्रकार हैं Selenium यह है:

  1. https://demo.guru99.com/test/guru99home/
  2. सभी पाठ्यक्रमों के लिंक एक-एक करके पढ़ें
  3. कंसोल पर सभी पाठ्यक्रम हाइपरलिंक प्रिंट करें।

गुरु99AntClass.जावा:

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 वेबड्राइवर

उपरोक्त उदाहरण फ़ाइल डाउनलोड करें

सारांश

चींटी एक निर्माण उपकरण है Java.

चींटी का उपयोग कोड संकलन, परिनियोजन, निष्पादन प्रक्रिया के लिए किया जाता है।

Ant को यहां से डाउनलोड किया जा सकता है अपाचे वेबसाइट।

Build.xml फ़ाइल का उपयोग Ant का उपयोग करके निष्पादन लक्ष्यों को कॉन्फ़िगर करने के लिए किया जाता है।

एंट को कमांड लाइन या एक्लिप्स जैसे उपयुक्त आईडीई प्लगइन से चलाया जा सकता है।