Search code examples
seleniumwebdrivertestngdataprovider

Running Selenium script for IE, firefox, chrome using @DataProvider but it is invoking only one browser thrice parallely; Using TestNG


I am learning Selenium Grid and started HUB and Node in my machine.

My machine configurations are:
OS: Windows8.1
FF version: 38.0.5
Chrome version: 43.0
IE version: 11

HUB:

java -jar selenium-server-standalone-2.45.0.jar -role hub

NODE:

java -Dwebdriver.chrome.driver=C:\Third_Party_Browser_Drivers\chromedriver.exe -Dwebdriver.ie.driver=C:\Third_Party_Browser_Drivers\IEDriverServer.exe -jar selenium-server-standalone-2.45.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox,maxInstances=4 -browser browserName=iexplore,maxInstances=3 -browser browserName=chrome,maxInstances=2 -maxSession 4

I am tryin to execute my test parallely in all the browser with the help of @DataProvider in TestNG. Below is my code:

public class TestSample2 {

    public static WebDriver driver;
    public static DesiredCapabilities capabilities = null;


    @Test(dataProvider = "getData")
    public void testLogin(String username, String password, String browser) throws MalformedURLException, InterruptedException{

        System.out.println(browser);

        if (browser.equals("chrome")){
        capabilities = DesiredCapabilities.chrome();
        capabilities.setBrowserName("chrome");
        capabilities.setPlatform(Platform.ANY); 
        //driver = new RemoteWebDriver(new URL("http://10.0.0.11:5566/wd/hub"), capabilities);

        } else if(browser.equals("firefox")){
            capabilities = DesiredCapabilities.firefox();
            capabilities.setBrowserName("firefox");
            capabilities.setPlatform(Platform.ANY); 

        } else if(browser.equals("iexplore")){
            capabilities = DesiredCapabilities.internetExplorer();
            capabilities.setBrowserName("iexplore");
            capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
            capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

            capabilities.setPlatform(Platform.WINDOWS);


        }

        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
        driver.get("http://facebook.com");
        Thread.sleep(3000);
        driver.findElement(By.xpath("//input[@id='email']")).sendKeys(username);
        driver.findElement(By.xpath("//input[@id='pass']")).sendKeys(password);
        driver.quit();


    }

    @DataProvider(parallel = true)
    public Object[][] getData(){

        Object data[][] = new Object[3][3];

        //first data
        data[0][0] = "meganbody";
        data[0][1] = "meganlht";
        data[0][2] = "firefox";

        //second data
        data[1][0] = "megan";
        data[1][1] = "megaight";
        data[1][2] = "chrome";


        //third data
         data[2][0] = "megdy";
         data[2][1] = "anlight";
         data[2][2] = "iexplore";

         return data;
    }

}

My testng.xml file is:

  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
  <suite name="TestNG Learning" parallel="tests" thread-count="3" >

     <test name="test_FF">
        <classes>
              <class name="Grid.TestSample2" ></class>  
        </classes>
      </test>
   </suite>

When I run the xml file as TestNG , I am receiving the output as:

chrome
iexplore
firefox

and the test case is success. But when I am seeing the Grid/console, it is showing me only "one browser" getting invoked; be it IE, FF or chrome(since i ran it several times) with 3 instances. For eg: Invoking IE browser thrice pararllely.

I don't understand where I am going wrong. Any help!!


Solution

  • You have a static webdriver object. If you run parallely, the same object is being overwritten. Dataprovider is correctly passing data but the driver object is being reused in all your tests. I would expect your tests to fail randomly because of the object sharing.

    Either put the driver object within the @Test scope or consider using threadlocal to avoid sharing driver object between threads.