Reputation: 523
i am running 4 parallel using testNG
and Selenium. My Java file looks like this:
public class SecondNGTest {
public static WebDriver driver;
public static ChromeOptions chromeOptions;
public static final String USERNAME = PRIVATE";
public static final String AUTOMATE_KEY = "PRIVATE";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static DesiredCapabilities caps;
@Test
public void executSessionOne() throws MalformedURLException{
//First session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://google.com/");
}
@Test
public void executeSessionTwo() throws MalformedURLException{
//Second session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://youtube.com/");
}
@Test
public void executSessionThree() throws MalformedURLException{
//Third session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://slack.com/");
}
@Test
public void executSessionFour() throws MalformedURLException{
//Third session of WebDriver
caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0 beta");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.selenium_version", "3.5.2");
caps.setCapability("name", "selenium test");
driver = new RemoteWebDriver(new URL(URL), caps);
chromeOptions = new ChromeOptions();
String chromeDriverPath = "resources/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//Goto guru99 site
driver.get("http://yahoo.com/");
}
@AfterTest
public void browserclose (){
driver.quit();
System.out.println("TestCase : Browser was closed");
}
}
It works as it should by opening up 4 browser windows at once and running the tests but the problem is they never end. The @AfterTest
block is only being triggered once, so one of the windows does close but the rest are left open and I have to manually close them. How can I close/quit each driver when each test is done?
EDIT: here is my testng.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="TestSuite" >
<test name="testGuru" thread-count="4" parallel="methods">
<classes>
<class name="selerixautomation.SecondNGTest">
</class>
</classes>
</test>
</suite>
Upvotes: 1
Views: 1615
Reputation: 8676
You should completely re-design your class.. First of all avoid using static fields when design your code to be running in parallel. Static field is a property of a class, not of an object. So being instantiated it is shared between all parallel threads and thus that can lead to unexpected results unless synchronized.
So you need to remove static
modifier and create your driver objects in @BeforeTest
(not in test code) and then quit your drivers in @AfterTest
.
Upvotes: 0
Reputation: 56
Since you are doing your parallel execution inside one java class but different methods (e.g. one @Test annotation one method) you need to call @AfterMethod hook to close all drivers (after every method / @Test annotation method).
Upvotes: 2
Reputation: 2326
You can try to kill your driver with annotation @AfterClass instead of @AfterTest.
@AfterClass
public void browserclose (){
driver.quit();
System.out.println("TestCase : Browser was closed");
}
Upvotes: 0