The driver instance initializes as Threadsafe. Till the launching of the browser and pass the URL everything seems to be working. But the credentials passing on anyone's browser, instead of all instances.
Below is the code for set and get the driver.
public static ThreadLocal<WebDriver> tldriver = new ThreadLocal<WebDriver>();
/**
* This method is used to initialize on the basis of browser name
*
* @param browserName
* @return
*/
public WebDriver init_driver(String browserName) {
if (browserName.equals("chrome")) {
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
chromeOptions.addArguments("--incognito");
tldriver.set(new ChromeDriver(chromeOptions));
} else if (browserName.equals("ff")) {
WebDriverManager.firefoxdriver();
tldriver.set(new FirefoxDriver());
} else {
System.out.println(browserName + "Browser value wrong, provide correct browser");
}
getDriver().manage().deleteAllCookies();
getDriver().manage().window().maximize();
return getDriver();
}
public static synchronized WebDriver getDriver() {
return tldriver.get();
}
Below code I am using in my test class
@BeforeMethod
public void setUp() {
basePage = new BasePage();
prop = basePage.init_proprties();
String browser = prop.getProperty("browser");
driver = basePage.init_driver(browser);
loginPage = new LoginPage(driver);
driver.get(prop.getProperty("url"));
homePage = loginPage.doLogin(prop.getProperty("username"), prop.getProperty("password"));
}
TestNG - XML
<listeners>
<listener
class-name="com.qa.zoho.listeners.ExtentReportListener" />
<!-- <listener class-name="com.qa.zoho.listeners.TestAllureListener" /> -->
</listeners>
<test name="CRM Sanity Test">
<classes>
<class name="com.qa.zoho.tests.HomePageTest" />
</classes>
</test>