Can WebDriver be declared as static?
Yes, the declaration of a static WebDriver is valid.
Why is it not recommended to declare WebDriver as static?
When WebDriver is declared as static, its instance will be shared. This can result in unexpected errors when the tests are executed in parallel, as the WebDriver instance is intended for use in a single test case only.
Example:
package TestAutomationCentral; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class StaticWebDriver { //TestAutomationCentral.com public static WebDriver driver; @Test public void setUp(){ System.setProperty("webdriver.chrome.driver", "C:\\temp\\drivers\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://testautomationcentral.com"); } }
In this tutorial, we discussed the static WebDriver and why it is not recommended to use in Test Automation. Hope you could be able to answer if an interviewer asked this question in an interview. The interview question might phrase in this way – What’s the impact of declaring the WebDriver as static and its implication while running tests in parallel?