I've done a few searches already but I'm still encountering this same issue. I believe it might be caused by my webdriver being static? I'm not too sure...
In my main class, I have included @BeforeTest
and @AfterTest
.
@BeforeTest
includes launching a new browser depending on my XML file
@AfterTest
includes driver.quit()
, which should kill the session/driver so the second test can have a clean driver from @BeforeTest
, no?
Here's my browser declaration:
public class Browser {
public static WebDriver driver;
//Variable initialization
public static String title;
public static String url;
public static String currentBrowser;
public static boolean jseWorkAround;
public Browser(){
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
//Browser launch
public static void launch(String browser){
if(browser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
currentBrowser = "Firefox/";
jseWorkAround = false;
System.out.println("Firefox Selected");
} else if (browser.equalsIgnoreCase("chrome")){
driver = new ChromeDriver();
currentBrowser = "Chrome/";
jseWorkAround = false;
System.out.println("Chrome Selected");
} else if (browser.equalsIgnoreCase("edge")){
driver = new EdgeDriver();
currentBrowser = "Edge/";
jseWorkAround = true;
System.out.println("Edge selected");
} else if (browser.equalsIgnoreCase("ie")){
driver = new InternetExplorerDriver();
currentBrowser = "IE/";
jseWorkAround = true;
System.out.println("IE Driver Selected");
} else if (browser.equalsIgnoreCase("background")){
driver = new PhantomJSDriver();
currentBrowser = "Background/";
jseWorkAround = false;
System.out.println("Background selected");
} else {
throw new IllegalArgumentException("Invalid Browser");
}
}
public static void quit(){
driver.quit();
}
public static void goToPage(String pageurl){
driver.get(pageurl);
}
Here's a random sample test:
@Parameters({"browser"})
@BeforeTest
public void browserSelection(String browser){
Browser.launch(browser);
}
@AfterTest
public void cleanupAfterTest(){
Print.line("Test complete. Cleaning up...");
Browser.quit();
}
@Test
public void Test1() {
Browser.goToPage("http://www.google.com");
Screenshot.page("Goes to google");
}
@Test
public void Test2() {
Browser.goToPage("http://www.yahoo.com");
Screenshot.page("Goes to yahoo");
}
I noticed that it is always the end of second test when things start failing with the error "Calling webdriver after calling quit".
Tests between browsers happen sequentially based on the order mentioned on my testng.xml
file.
If I run firefox first then chrome, chrome test would fail on the last @Test
.
Whereas, if I run chrome first then firefox, firefox would fail on the last @Test
.
Below is the error message...
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'DESKTOP-5ED0H7O', ip: '10.0.9.239', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:130)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
at utility.Scroll.toText(Scroll.java:43)
at tests.TestCases.TCID20(TestCases.java:390)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
This random failing is severely harming my progress. If I enable only one browser at a time on my XML file with no other browser in line, nothing fails.
As I see your problem is @test run before load browser (you have to increase wait time). Another thing is you haven't added any priority levels. Before all, the way you get browser. you have to use geckodriver, chromedriver etc.
Try to get browser like below
if (browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "/Path/ToChromeDriver/chromedriver.exe");
driver = new ChromeDriver();
System.out.println("Chrome Selected");
}
Add priorities like below
Browser browser;
@Parameters({"browser"})
@BeforeTest
public void browserSelection(String browser){
browser.launch(browser);
thread.sleep(5000);//Not recommended but timeout also may be an issue if so increase implicitly wait time
}
@AfterTest
public void cleanupAfterTest(){
Print.line("Test complete. Cleaning up...");
browser.quit();
}
@Test( priority = 1, description = "Test 1")
public void setUserName(){
Browser.goToPage("http://www.google.com");
Screenshot.page("Goes to google");
}
@Test(priority = 2,description = "Test 2")
public void setPassword(){
Browser.goToPage("http://www.yahoo.com");
Screenshot.page("Goes to yahoo");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With