There is a situation where I have 150 test cases and I have set up Test NG parallel test execution. Unfortunately, I'm using the same web driver which initialized at the test base class on all the tests. Because of that, I believe my tests are getting failed. So is there any way to avoid this? Execute parallel test by using the same WebDriver? And I'm on IntelliJ Java.
Below is the sample code:
public class AppTest {
// driver;
String baseURL, nodeURL;
WebDriver driver;
@AfterTest
public void afterTest() {
}
@Test
public void sampleTest() throws MalformedURLException {
System.setProperty("webdriver.chrome.driver", "C:\\SeleniumGrid\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.phptravels.net/");
String Email = "cjay" + Math.random() + "[email protected]";
String UserName = "Chathura";
// driver.findElement(By.xpath("/html/body/header/div[2]/div/div/div[2]/div/div/a[2]"));
driver.findElement(By.xpath("//*[@class=\"theme-btn theme-btn-small waves-effect\"]")).click();
driver.findElement(By.name("first_name")).sendKeys(UserName);
driver.findElement(By.name("last_name")).sendKeys("Jayasinghe");
driver.findElement(By.name("phone")).sendKeys("12321231");
driver.findElement(By.name("email")).sendKeys(Email);
driver.findElement(By.name("password")).sendKeys("Email");
driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();
driver.findElement(By.name("email")).sendKeys(Email);
driver.findElement(By.name("password")).sendKeys("Email");
driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();
String name = driver.findElement(By.className("author__title")).getText();
System.out.println(name);
Assert.assertEquals(name, UserName);
driver.quit();
}
@Test
public void sampleTest2() throws MalformedURLException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\SeleniumGrid\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
Thread.sleep(4000);
driver.get("https://www.phptravels.net/");
String Email = "cjay" + Math.random() + "[email protected]";
String UserName = "Chathura";
// driver.findElement(By.xpath("/html/body/header/div[2]/div/div/div[2]/div/div/a[2]"));
driver.findElement(By.xpath("//*[@class=\"theme-btn theme-btn-small waves-effect\"]")).click();
driver.findElement(By.name("first_name")).sendKeys(UserName);
driver.findElement(By.name("last_name")).sendKeys("Jayasinghe");
driver.findElement(By.name("phone")).sendKeys("12321231");
driver.findElement(By.name("email")).sendKeys(Email);
driver.findElement(By.name("password")).sendKeys("Email");
driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();
driver.findElement(By.name("email")).sendKeys(Email);
driver.findElement(By.name("password")).sendKeys("Email");
Thread.sleep(4000);
driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();
String name = driver.findElement(By.className("author__title")).getText();
System.out.println(name);
Assert.assertEquals(name, UserName);
driver.quit();
}