Parallel Test Execution - Testng Listeners, Selenium Webdriver and Selenium Grid

571 views
Skip to first unread message

Luka Popovic

unread,
Jan 13, 2015, 8:19:27 AM1/13/15
to seleniu...@googlegroups.com
I have a specific situation. I have a web app tests written in java (selenium webdriver) that are organized and executed with testng in parallel.
Everything is run on Ubuntu Linux 14.04 and Firefox 31.0

The process looks like this:

- Every testng test (not test method) is run in parallel.
- Every testng test contains one class and the class has more then one test method
- Every testng test has the same listener (ItestListener) that starts the webdriver that is later shared between all tests methods in this particular test. This listener also closes the webdriver at the end of this test.

I am trying now to run this suite on Selenium Grid. Problem that I am facing is that after finishing one test webdriver instance stays in Selenium Grid active and blocks execution of next test until the timeout kicks in and kills the test. I am guessing that the problem is this handling of webdriver through the test listener and sharing one driver between test methods.

I am doing this simply because I want to save some time on opening and closing the webdriver before and after every test method.

My question is: Does anyone have an idea if this behavior is possible when using Selenium Grid and if yes, how this should be handled? So does anyone have this situation that he is using Grid and he has the test methods that are sharing webdriver and everything is working correctly? If so, I would like to hear how did you achieve it.

Code of TestListener:

public class WebDriverTestListener extends TestListenerAdapter {
   
   
@Override
   
public void onStart(ITestContext testContext) {
       
super.onStart(testContext);
       
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
       
WebDriver driver = null;
       
try {
            driver
= new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
       
} catch (MalformedURLException e) {
            e
.printStackTrace();
       
}
        testContext
.setAttribute("webdriver", driver);
       
System.out.println("Webdriver instantiated!");
       
   
}
   
   
@Override
   
public void onFinish(ITestContext testContext) {
       
super.onFinish(testContext);
       
WebDriver driver = (WebDriver) testContext.getAttribute("webdriver");
        driver
.close();
   
}
}

And code of one of the test classes:
public class App
{
   
private WebDriver driver;
   
   
@BeforeTest
   
public void Before(ITestContext testContext){        
        driver
= (WebDriver) testContext.getAttribute("webdriver");
   
}
   
   
@Test
   
public void test1(ITestContext testContext){
        driver
.get("http://gogole.com");
       
System.out.println("Thread id = " + Thread.currentThread().getId());
   
}
   
   
@Test
   
public void test2(ITestContext testContext){
       
WebElement searchbox = driver.findElement(By.name("q"));
       
WebElement searchbutton = driver.findElement(By.name("btnG"));
        searchbox
.sendKeys("One");
        searchbutton
.click();
       
WebDriverWait wait = new WebDriverWait(driver, 10);
        wait
.until(ExpectedConditions.presenceOfElementLocated(By.id("taw")));
       
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

       
try {
           
FileUtils.copyFile(scrFile, new File("test-output/screenshot" + Thread.currentThread().getId() + ".png"));
       
} catch (IOException e) {
            e
.printStackTrace();
       
}
       
System.out.println("Test2, Thread id = " + Thread.currentThread().getId());
   
}
   
}

This is just an example.

Thanks everyone in advance!
Luka

Krishnan Mahadevan

unread,
Jan 14, 2015, 3:47:33 AM1/14/15
to Selenium Users
Can you please try replacing driver.close() with driver.quit() and see if that helps ?

I think driver.quit() is basically what sends out a call to clean up the browser instances, sessions etc.,

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6cbd65c4-d0be-4ca4-9c69-eb15e04e8741%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luka Popovic

unread,
Jan 14, 2015, 4:26:43 AM1/14/15
to seleniu...@googlegroups.com
@Krishnan

Yes, you were right! I expected some big implementation problem and I've overseen such a trivial mistake. Anyway, the difference between driver.close() and driver.quit() is that driver.close() closes currently focused window or popup and the browser window itself if there is nothing else, BUT the driver stays active. driver.quit() closes all windows associated with particular driver and removes the driver instance. I hope this will be helpful for someone in the future. Thanks Krishnan!
Reply all
Reply to author
Forward
0 new messages