1
votes

I am facing a problem of Selenium Grid and TestNG. My test scripts will NOT quite the browser until all the test class are executed.

I've configed 5 Chrome instance running on my Selenium Grid, then below is my xml file

   <suite name="BrowserTest">            
   <test name="Test1" >
        <parameter name="browserName" value="chrome"/>
        <classes>
            <class name= "Pagetest" />
        </classes>      
    </test>   

    <test name="Test2" >
        <parameter name="browserName" value="firefox"/>
        <classes>
            <class name= "Pagetest" />
        </classes>      
    </test>   

  <test name="Overview Page Test on ie, English" >
        <parameter name="browserName" value="ie"/>
        <classes>
            <class name= "Pagetest" />
        </classes>      
    </test> 
   </suite>

Here is my test scripts public class Pagetest { private WebDriver browser;

    public PateTest( String browser){
        // create a browser instance.
    }

   @AfterClass (or @afterTest)   
   public void afterTest() throws Exception {
        System.out.println("this test is done");
        this.browser.quit();
    }

   @Test
   public void test1(){
       this.browser.get("www.google.com");
       // doing some login stuff
   }

}

Now when I am running my test, testNG will create 3 chrome webdirver instance one by one (with running the test). but after the 1st test is done, the browser instance of 1st test is not quited until all 3 tests are done, then all the 3 chrome instance are quited almost at the same time. What I want is after the 1st test is done, it's browser instance is quited, then the 2nd test case is started, did I do something wrong or how should I change my code?

1

1 Answers

0
votes

Try using threadlocal it would help when you are even running tests in parallel

public ThreadLocal<WebDriver> driver=null;

@beformethod()

public void setUp
{
driver = new ThreadLocal<WebDriver>();
driver.set(new ChromeDriver());
 return driver.get();
}

@AfterMethod
public void closeBrowser() {

{
 driver.get().quit();
}