Sunday, September 2, 2018

Selenium Grid Ii - Using @Dataprovider To Last Parallel Webdriver Tests

| Sunday, September 2, 2018
In my previous post, We learnt how to execute selenium WebDriver unmarried software automation attempt out inwards multiple browsers inwards parallel using Selenium Grid 2. We had used "parameter" tag inwards testng.xml file to feed browser advert inwards which your software automation attempt out needs to run inwards parallel. Instead of using @Parameters, We tin purpose @DataProvider notation method to feed browser names in addition to run selenium WebDriver attempt out inwards parallel using selenium Grid 2.

If y'all purpose @Parameters, You postulate to write same attempt out multiple times(Depends on position out of browsers)  in testng.xml file. Main create goodness of using @DataProvider notation method is y'all no postulate to write same software automation attempt out representative advert multiple times inwards testng.xml file. You guide maintain to write it alone i time inwards testng.xml file. @DataProvider notation method volition provide browser details to @Test method. To run attempt out inwards parallel, y'all postulate to ready (parallel=true) amongst @DataProvider annotation. Let's travail it amongst elementary example.

My requirement is : I wants to run my selenium software automation attempt out parallel inwards 3 browsers using selenium grid 2 in addition to testng notation @DataProvider.

Prerequisites :
  • Selenium Grid 2 hub should last inwards running vogue every bit described inwards THIS POST.
  • Selenium Grid 2 node should last registered amongst hub in addition to inwards running vogue every bit described inwards THIS POST.
  • Also IE browser's protected modes should last enabled for all zones every bit described in THIS POST and zoom aeroplane should last ready to 100% every bit described in THIS POST if y'all are running your attempt out inwards IE browser too.
I am assuming your grid 2 hub in addition to node are working fine in addition to right away this is fourth dimension to launch software automation test. Write bellow given attempt out inwards eclipse.

Note : Please take network explorer related code materials from bellow given script if facial expression upward whatsoever related mistake every bit it is non real stable amongst selenium grid.

fillingForm.java
package Grid2;  import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Platform; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;  populace degree fillingForm {   //Used dataProvider parameter to acquire information from @DataProvider notation method.  //Can guide maintain object array data(browser name, First Name in addition to Last Name) from getNames method.  @Test(dataProvider = "getNames")  populace void gmailLogin(String browser, String fName, String lName) throws MalformedURLException, InterruptedException {      //Initialize DesiredCapabilities null.   DesiredCapabilities cap = null;    //Initialize browser driver every bit per information received from getNames().   if (browser.equals("firefox")) {    //Set firefox browser capabilities for windows platform.    cap = DesiredCapabilities.firefox();    cap.setBrowserName("firefox");    cap.setPlatform(Platform.WINDOWS);   } else if (browser.equals("chrome")) {    //Set chrome browser capabilities for windows platform.    cap = DesiredCapabilities.chrome();    cap.setBrowserName("chrome");    cap.setPlatform(Platform.WINDOWS);   } else if (browser.equals("iexplore")) {    //Set IE browser capabilities for windows platform.    cap = DesiredCapabilities.internetExplorer();    cap.setBrowserName("internet explorer");    cap.setPlatform(Platform.WINDOWS);   }    //Initialize RemoteWebDriver on grid 2 node amongst browser capability.   RemoteWebDriver driver = novel RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   //Open URL inwards requested browsers of node in addition to execute attempt out steps.   driver.get("aa/search?q=selenium-grid-2-configuration-setup-hub");   driver.findElement(By.name("FirstName")).sendKeys(fName);   driver.findElement(By.name("LastName")).sendKeys(lName);    driver.quit();  }    //Created @DataProvider notation method to render data(browser name, First Name in addition to Last Name) for test  @DataProvider(parallel=true)  populace Object[][] getNames(){   Object data[][] = novel Object[3][3];   data[0][0] = "firefox";   data[0][1] = "FirstName1";   data[0][2] = "LastName1";      data[1][0] = "chrome";   data[1][1] = "FirstName2";   data[1][2] = "LastName2";      data[2][0] = "iexplore";   data[2][1] = "FirstName3";   data[2][2] = "LastName3";        render data;  }  }

You tin run into inwards in a higher house test. We guide maintain used @DataProvider notation method amongst (parallel=true) to render information to @Test method.  So no postulate to set @Parameter inwards testng.xml file. Use bellow given testng.xml file to run inwards a higher house test.

testng.xml
<suite name="My Test Suite" verbose="2" parallel="tests" thread-count="5">  <test name="Selenium Grid Test">   <classes>    <class name="Grid2.fillingForm" />   </classes>  </test> </suite>

Execute inwards a higher house attempt out in addition to verify execution. It volition run attempt out inwards parallel inwards dissimilar 3 browsers. This agency y'all tin purpose @DataProvider notation method amongst (parallel=true) at house of @Parameter to execute attempt out inwards dissimilar browser parallel using selenium grid 2.

Related Posts