Pre-Requisite:
Read these article before proceeding with this post:
Configuring Selenium Webdriver in Eclipse with TestNG plugin installation.
I suppose that you are familiar with how to add jar files to your project and How to add any class, folder and XML.
Most of the time we find it hard to do compatibility testing of certain web-page on various Browsers and it takes a big chunk of time for its execution on various set of Browser and also on various instance of Browser.
TestNG has provided us with an option through its Parameter feature, through which we could run the same test script written in Selenium WebDriver parallel to all installed Browser on our system?
Scripts and Steps to execute Script
1- Go to your Eclipse Project –> Right Click on Project –> Click On New–> others–> Pop up would appear–> Click on XML –> Select XML File –> Click on Next –> New XML File pop up would appear–> Enter the name of XML and click on Finish
2- Your XML file would appear like this
<?xml version=“1.0” encoding=“UTF-8”?>
<suite name=“Suite_2” parallel=“tests”>
<test name=“Funaction Test Cases “>
<parameter name=“browser” value=“Chrome” />
<classes>
<class name=“com.testng.Test” />
</classes>
</test> <!– Test –>
<test name=“Funaction Test Cases_Firefox “>
<parameter name=“browser” value=“Firefox” />
<classes>
<class name=“com.testng.Test” />
</classes>
</test>
<test name=“Funaction Test Cases_IE “>
<parameter name=“browser” value=“IE” />
<classes>
<class name=“com.testng.Test” />
</classes>
</test>
</suite> <!– Suite –>
Download XML file from here
Now understand the tags of XML that I have marked Bold in XML file
Parallel: This is being used to run tests parallelly in different browser
suite name: This is the name of your test suit
test name : kind of test cases (you may give a name like Regression, Sanity,Smoke, or anything that make you to make better test execution ) like I have given name to this test case as Generic test
Classes: name of class that you want to add in this test execution
Most important one is
<parameter name="browser" value="FF"></parameter>
Here browser has been taken as a parameter name(you can give a name as per your choice) and FF for Firefox, IE for Internet Explorer, and Chrome for Chrome Browser are the various values given to this browser parameter would be called in our code.
3- Now its time to understand how you have to use the parameter name in the Java program. Since parameter is defined for Browsers. Since you are trying to use the TestNG framework we would write two function first one to launch Browser and Second one to close Browser
Create new java class in your Java Project
Steps to create a new java class:: right-click on project ||New|| Click on Class||Enter the name of the class and Hit on the finish.
The name of my java class is Browser.java so just create one class with the name Browser and copy and paste the following code in eclipse.
But before moving ahead i would suggest to go through following two post
1- Launching Chrome Browser
2- CHALLENGES TO RUN SELENIUM WEBDRIVER SCRIPTS IN IE BROWSER
package com.testng; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.Parameters; //import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeMethod; public class Browser { WebDriver driver; @BeforeMethod /* The annotated method will be run before all tests in this suite have run */ //browser is the name of parameter that we have used in xml @Parameters("browser") /* this annotation is used to insert parameter in test*/ public void openBroswer(String browser){ /*Comparing the value of parameter name if this is FF then It would launch Firefox and script that would run is as follows */ if(b.equalsIgnoreCase("Chrome")) { System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe"); driver = new ChromeDriver(); } else if (b.equalsIgnoreCase("FF")) { driver = new FirefoxDriver(); } else if (b.equalsIgnoreCase("IE")) { System.setProperty("webdriver.ie.driver", "path of IEDriver.exe"); driver = new InternetExplorerDriver(); } } @AfterMethod /* this annotation would run once test script execution would complete*/ public void closeBrowser() { try{ driver.wait(15000); } catch(Exception e) {} driver.close(); driver.quit(); } }
4- Since we have created Browser class in which we have used the parameter defined in XML, Now we should create one more class that is our Test class, This class would inherit Browser class for launching Browser before executing scripts under @Test annotation and for Closing Browser once execution of scipt under @Test annotation get completed.
here I have given the name of this class is Test
package com.testng; import org.openqa.selenium.By; import org.testng.Reporter; import org.testng.annotations.Test; @Test public class Test extends Browser{ public void test(){ /* in this test we are searching selenium in Google and clicking on first link in Google Result*/ driver.get("http://www.google.com"); driver.findElement(By.id("lst-ib")).sendKeys("selenium"); driver.findElement(By.id("lst-ib").sendKeys(Keys.ENTER); //driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.findElement(By.xpath("//*[@id='rso']/div[1]/div/div/h3/a")).click(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
5- Now its time to execute Test-Case
Steps:
Right Click on Test.java file and –> Run As –> Run Confinguration –> In pop up Go Testng –> Right Click on Testng and Click on New –> Give name to your Run Configuration, Browser Class and select your Xml just by Browsing in front of Suit –> hit on Finish and this would run your test
Hope this post would help you to run your selenium test case parallel.
Friends I have tried these scripts on my system and are running well so if you find any missing bracket or braces missing then please do the correction on your own but Script is correct.
Leave a Reply