I have added code snippet for further clarity. I am using java-selenium-testNG and trying to login to a website zoho.com with 3 accounts and verifying if success. I have looked through somewhat similar issues but haven't found a solution that works in my case.
I instantiate the ChromeDriver in @BeforeMethod. Then I login to the website with first account and close the browser in my @AfterMethod. I have to re-instantiate the browser with Driver = new ChromeDriver to login back with a new account as the instance is closed or atleast that's the feeling it gives error that the session id doesn't exists.
My issue is only the last instance of the driver is getting closed as I have driver.quit in the @AfterTest method. The other two instances remain in memory.
I have also tried using the same instance without closing the browser but in such cases the login option is not available on that particular website and my subsequent tests fail.
here is the code below for further clarification
package uk.dev.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.concurrent.TimeUnit;
public class zohoLogin {
WebDriver driver=null;
String winHandleBefore;
@DataProvider(name ="testData")
public Object[][] loginPasswordData()
{
return new Object[][]{
{"xxx@gmail.com",new String ("somepassword")} ,
{"yyy@gmail.com",new String ("somepassword")},
{"zzz@gmail.com",new String ("somepassword")},
};
}
@Test ( dataProvider = "testData")
public void enterUserDetails(String userEmail, String userPassword) throws InterruptedException {
driver.findElement(By.xpath("//input[@id='login_id']")).sendKeys(userEmail);
System.out.println(("Running for Test data " + userEmail));
Thread.sleep(2000);
driver.findElement(By.xpath("//button[@id='nextbtn']")).click();
Thread.sleep(2000);
System.out.println("clicked on Next");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys(userPassword); System.out.println("Entered the password"); driver.findElement(By.xpath("//button[@id='nextbtn']//span[contains(text(),'Sign in')]")).click(); System.out.println("Clicked on Signin"); Thread.sleep(2000);
}
@BeforeMethod
public void loginZoho() throws InterruptedException
{
driver = new ChromeDriver();
driver.get("https://www.zoho.com");
System.out.println("Open the browser");
driver.findElement(By.xpath("//a[@class='zh-login']")).click();
//driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
}
@AfterMethod
public void closeBrosers() throws InterruptedException {
Thread.sleep(2000);
driver.close();
System.out.println("Close the browser");
}
@BeforeTest
public void setupBrowser()
{ WebDriverManager.chromedriver().setup();
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}
Attached the run results where 2 chrome driver instance can be seen.Also note the AfterMethod is not getting executed. enter image description here