Difficulty Level: HIGH
Pre-requisites:
- JDK (JAVA version "1.6.021") should be installed and working
- Ant (Apache Ant version 1.7.1) should be working
- selenium-server-standalone-2.12.0.jar
- testng-5.14.1.jar
Steps involved in implementation:
- Go to C drive and create a folder structure like below
---------------------grid2
---------------------------------config
---------------------------------lib
---------------------------------src\com\grid2\tests
---------------------------------build.properties
---------------------------------build.xml
- In this step we create testNG configuration file to map the created test cases. Create a griddemo.xml file into the config folder having the below code lines:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Same TestCases on Different Browser" verbose="3" parallel="tests" thread-count="2">
<test name="Run on Internet Explorer">
<parameter name="browser" value="iexplore"/>
<classes>
<class name="com.grid2.tests.GridWithWebdriver"/>
</classes>
</test>
<test name="Run on firefox">
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.grid2.tests.GridWithWebdriver1"/>
</classes>
</test>
</suite>
- Put the two downloaded jar files (selenium-server-standalone-2.12.0.jar and testng-5.14.1.jar) into the lib folder.
Add the code for the selenium scripts under the src\com\grid2\test folder, create two java files having below code lines:
package com.grid2.tests;Save the below codes with "GridWithWebdriver1.java"
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class GridWithWebdriver {
public WebDriver driver;
@Parameters({"browser"})
@BeforeClass
public void setup(String browser) throws MalformedURLException {
DesiredCapabilities capability=null;
if(browser.equalsIgnoreCase("firefox")){
System.out.println("firefox");
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
//capability.setVersion("");
}
if(browser.equalsIgnoreCase("iexplore")){
System.out.println("iexplore");
capability= DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
//capability.setVersion("");
}
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to("http://google.com");
}
@Test
public void test_first() throws InterruptedException{
Thread.sleep(3000);
WebElement search_editbox = driver.findElement(By.name("q"));
WebElement search_button = driver.findElement(By.name("btnG"));
search_editbox.clear();
search_editbox.sendKeys("first");
search_button.click();
}
@Test
public void test_second(){
WebElement search_editbox = driver.findElement(By.name("q"));
WebElement search_button = driver.findElement(By.name("btnG"));
search_editbox.clear();
search_editbox.sendKeys("second");
search_button.click();
}
@AfterClass
public void tearDown(){
driver.quit();
}
}
package com.grid2.tests;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class GridWithWebdriver1 {
public WebDriver driver;
@Parameters({"browser"})
@BeforeClass
public void setup(String browser) throws MalformedURLException, InterruptedException {
DesiredCapabilities capability=null;
if(browser.equalsIgnoreCase("firefox")){
System.out.println("firefox");
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
//capability.setVersion("");
}
if(browser.equalsIgnoreCase("iexplore")){
System.out.println("iexplore");
capability= DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
//capability.setVersion("");
}
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to("http://google.com");
}
@Test
public void test_third() throws InterruptedException{
Thread.sleep(3000);
WebElement search_editbox = driver.findElement(By.name("q"));
WebElement search_button = driver.findElement(By.name("btnG"));
search_editbox.clear();
search_editbox.sendKeys("third");
search_button.click();
}
@Test
public void test_fourth(){
WebElement search_editbox = driver.findElement(By.name("q"));
WebElement search_button = driver.findElement(By.name("btnG"));
search_editbox.clear();
search_editbox.sendKeys("foruth");
search_button.click();
}
@AfterClass
public void tearDown(){
//Close the browser
// driver.quit();
}
}
- Open the build.properties file and put the below code lines into same, we do it for keeping the build properties:
ws.home=C:/selenium/grid2
test.dest=${ws.home}/build
test.src=${ws.home}/src/com/grid2
- Open the build.xml file and write the below code, this is because we are running our code through ant:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>
<project name="S4" default="all" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property file="./build.properties"/>
<!--<property name="webSite" value="http://www.manta.com" />-->
<property name="seleniumHost" value="localhost" />
<property name="seleniumPort" value="4444" />
<property name="browser" value="*firefox" />
<property name="hubPollerIntervalInSeconds" value="30" />
<!--
<property name="ws.home" value="C:/selenium/testws"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/test"/>
-->
<target name="start-server">
<java jar="${ws.home}/lib/selenium-server.jar" fork="true" spawn="true" >
<arg line="-timeout 30"/>
<arg line="-port ${seleniumPort}"/>
</java>
<echo taskname="waitfor" message="Wait for proxy server launch" />
<waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
<http url="http://localhost:${seleniumPort}/selenium-server/driver/?cmd=testComplete"/>
</waitfor>
<echo>Selenium Server started</echo>
</target>
<target name="stop-server">
<get taskname="selenium-shutdown"
src="http://localhost:${seleniumPort}/selenium-server/driver/?cmd=shutDownSeleniumServer"
dest="result.txt" ignoreerrors="true" />
<echo taskname="selenium-shutdown" message="Selenium Server Shutdowned." />
</target>
<target name="start-selenium-server">
<java jar="${ws.home}/lib/selenium-server.jar" fork="true"/>
</target>
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.home}/lib" includes="*.jar"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />
</target>
<!-- all -->
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<!-- <echo message="classpath: ${test.classpath}"/> -->
<echo message="compiling..."/>
<javac
debug="true"
destdir="${test.dest}"
includes="*/*.java"
srcdir="${test.src}"
target="1.5"
classpath="${test.classpath}"
></javac>
</target>
<!-- bin -->
<target name="build" depends="init">
</target>
<!-- run -->
<target name="run1" depends="compile,init">
<testng classpath="${test.classpath}:${test.dest}" suitename="Same TestCases on Different Browser">
<xmlfileset dir="${ws.home}/config" includes="GridDemo.xml"/>
</testng>
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<!-- ****************** targets not used ****************** -->
</project>
- Run the project
java -jar selenium-server-standalone-2.12.0.jar -role hub
b. open another cmd and run the below command for launch server instance for IE:
java -jar selenium-server-standalone-2.12.0.jar-role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS -port 5556
c. open another cmd and run the below command for launch server instance for firefox:
java -jar selenium-server-standalone-2.12.0.jar-role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS -port 5555
d. After running the above commands open the URL http://localhost:4444/console on any of the browser, purpose is that using same way you can check if both server instances are launched successfully
e.Open another command terminal and run the ant target, run the below command in path C:\selenium\grid2
ant run1
f.for viewing the result go to "C:\selenium\grid2\test-output" and open index.html file, result would be shown like in below screen,
Green color represent that script ran successfully and it it would be in red then it means your script did not run successfully.

Helpful Video
No comments:
Post a Comment