Reputation: 69
I have a java class that opens up two Chrome browsers, searches for "test 1" and "test 2", respectively. However, once both browsers open, only one browser with the google page will search for "test 1 test 2".
I believe this issue may be because I am calling the driver = new WebDriver from a parent class. However, I am not sure how to resolve the issue.
Here are my two methods that I am trying to run in parallel.
package webDrivertests;
public class googleTestClass extends Methods{
@Test
public void test1() throws InterruptedException {
googleTestClass object1;
object1 = new googleTestClass();
object1.launchBrowser();
object1.goToURL("https://www.google.com");
object1.enterValue("name","q","google test 1");
driver.quit();
}
@Test
public void test2() throws InterruptedException {
googleTestClass object2;
object2 = new googleTestClass();
object2.launchBrowser();
object2.goToURL("https://www.google.com");
object2.enterValue("name","q","google test 2");
driver.quit();
}
}
This is my xml file I use to call them.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="2" name="Test" parallel="methods">
<classes>
<class name="webDrivertests.googleTestClass"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
The parent method that includes the driver
package webDrivertests;
// import statements
public class Methods {
public static WebDriver driver;
public void launchBrowser() {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver_win32\\chromedriver.exe");
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
}
public void goToURL(String url) {
driver.get(url);
}
public void enterValue(String htmltype, String identifier, String value) throws InterruptedException {
if (htmltype == "id") {
WebElement element = driver.findElement(By.id(identifier));
element.clear();
element.sendKeys(value);
element.submit();
}
if (htmltype =="name") {
WebElement element = driver.findElement(By.name(identifier));
element.clear();
element.sendKeys(value);
element.submit();
}
Thread.sleep(3000);
}
}
Current Result: Two browsers are opened and each go to google.com. However only one browser will search for "test 1 test 2". Any help is appreciated! If possible, I would still like to use my parent class "Methods" as it contains a lot of methods I am using for my other real test cases.
Thanks in advance.
Upvotes: 1
Views: 2297
Reputation: 5908
Because of static
declaration of the driver object in the class, it get's overridden parallel execution when second test called launchBrowser().
Obviously removing static
will fix this issue but still you will fall in different issues of managing driver while your test bed and methods increase.
I would recommend to use any of TestNG extension that takes care of such requirement. We are using QAF which is built upon TestNG and provides driver management, resource management and many more features which are crucial for web/mobile/webservices testing.
Upvotes: 0
Reputation: 14736
The problem lies in your test code. WebDriver object is being declared as a static object.
So this causes every test method to share the same instance.
To fix the problem remove the static
keyword from the WebDriver declaration in your Methods
class and try again.
Upvotes: 1