0

I have a test class that has multiple alternative tests running one after another. To make sure the tests are isolated and to not make it stressfull for the build-agent, I want to close and open a new browser instance between each test.

The issue I'm getting is the IllegalStateException when any test after the first opens a new browser instance.

Selenide Version: 5.2.3
Chrome Version: 75.0.3770.100
Browser Driver Version: 74.0.3729.6
Selenium Version: 3.141.59
OS Version: Windows 10

So far I've tried removing the Selenide.close() method call from my tests, but it results in using the same window for each test. New tests pick up from where the last test finished and that just doesn't sit well with me - I want my tests to be isolated. I also tried instantiating and closing the browser window using different means, but I was not successful using the latests Selenide version.

private static final String CHROMEDRIVER_V = "74.0.3729.6"
private static final String DEFAULT_TITLE = "Title";
private static final String TITLE_1 = "New alphabetic title";
private static final String TITLE_2 = "123456";

@BeforeEach
void beforeEach() throws Exception {
    setup();
    loginToService();
}

@Test
public void case_one() {
    HomePage.title.shouldHave(Condition.text(DEFAULT_TITLE));
    HomePageActions.changeTitle(TITLE_1));
    HomePage.title.shouldHave(Condition.text(TITLE_1));
}

@Test
public void case_two() {
    HomePage.title.shouldHave(Condition.text(DEFAULT_TITLE));
    HomePageActions.changeTitle(TITLE_2));
    HomePage.title.shouldHave(Condition.text(TITLE_2));
}

@AfterEach
void afterEach() throws Exception {
    teardown();
}

private void setup() {
    String url = "http://localhost:8080/login";
    WebDriverManager.chromedriver()
        .version(CHROMEDRIVER_V)
        .timeout(20000)
        .forceDownload()
        .setup();
    Selenide.open(url);
}

private void teardown() {
    Selenide.close();
}

private void loginToService() {
    LoginPage.loginField.setValue("login");
    LoginPage.passField.setValue("pass");
    LoginPage.loginButton.click();
    HomePage.page.shouldBe(Condition.visible);

So with the code above, I expect it to, when launching the whole test class: 1. Do the beforeEach() method, setting up a new browser instance and logging in to our service. 2. do the case_one() method, testing the required things 3. do the afterEach() method, closing our browser instance and freeing up resources 4. do the beforeEach() again 5. do the case_two() now 6. do the afterEach() again, the test suite is finished

What I get in fact is 1-3 running as expected with no issues, and visibly step 4 starts fine as a new browser window appears, but the run returns the exception when trying to execute login code from step 4.

июл 15, 2019 6:22:52 PM com.codeborne.selenide.drivercommands.LazyDriver getAndCheckWebDriver
INFO: No webdriver is bound to current thread: 1 - let's create a new webdriver
Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 7568
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1563204173.792][WARNING]: This version of ChromeDriver has not been tested with Chrome version 75.
июл 15, 2019 6:22:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
июл 15, 2019 6:22:54 PM com.codeborne.selenide.webdriver.BrowserResizer adjustBrowserSize
INFO: Set browser size to 1366x768
июл 15, 2019 6:22:55 PM com.codeborne.selenide.webdriver.WebDriverFactory logBrowserVersion
INFO: BrowserName=chrome Version=75.0.3770.100 Platform=XP
июл 15, 2019 6:22:55 PM com.codeborne.selenide.webdriver.WebDriverFactory createWebDriver
INFO: Selenide v. 5.2.3
июл 15, 2019 6:22:55 PM com.codeborne.selenide.webdriver.WebDriverFactory logSeleniumInfo
INFO: Selenium WebDriver v. 3.141.59 build time: 2018-11-14T08:17:03
июл 15, 2019 6:22:55 PM com.codeborne.selenide.drivercommands.CreateDriverCommand createDriver
INFO: Create webdriver in current thread 1: ChromeDriver -> ChromeDriver: chrome on XP (c531a2374a250d21a63eedec3d35c942)
июл 15, 2019 6:22:59 PM com.codeborne.selenide.drivercommands.CloseDriverCommand run
INFO: Close webdriver: 1 -> ChromeDriver: chrome on XP (c531a2374a250d21a63eedec3d35c942)
июл 15, 2019 6:22:59 PM com.codeborne.selenide.drivercommands.CloseBrowser run
INFO: Trying to close the browser ChromeDriver ...
июл 15, 2019 6:22:59 PM com.codeborne.selenide.drivercommands.CloseDriverCommand run
INFO: Closed webdriver 1 in 772 ms

java.lang.IllegalStateException: Webdriver has been closed. You need to call open(url) to open a browser again.

    at com.codeborne.selenide.drivercommands.LazyDriver.getWebDriver(LazyDriver.java:64)
    at com.codeborne.selenide.impl.ElementFinder.getSearchContext(ElementFinder.java:86)
    at com.codeborne.selenide.impl.ElementFinder.getWebElement(ElementFinder.java:74)
    at com.codeborne.selenide.commands.ToWebElement.execute(ToWebElement.java:11)
    at com.codeborne.selenide.commands.ToWebElement.execute(ToWebElement.java:8)
    at com.codeborne.selenide.commands.Commands.execute(Commands.java:144)
    at com.codeborne.selenide.impl.SelenideElementProxy.invoke(SelenideElementProxy.java:57)
    at com.sun.proxy.$Proxy16.toWebElement(Unknown Source)
    at com.codeborne.selenide.impl.ElementFinder.getSearchContext(ElementFinder.java:87)
    at com.codeborne.selenide.impl.ElementFinder.getWebElement(ElementFinder.java:74)
    at com.codeborne.selenide.impl.WebElementSource.checkCondition(WebElementSource.java:50)
    at com.codeborne.selenide.commands.Should.should(Should.java:35)
    at com.codeborne.selenide.commands.Should.execute(Should.java:29)
    at com.codeborne.selenide.commands.Should.execute(Should.java:12)
    at com.codeborne.selenide.commands.Commands.execute(Commands.java:144)
    at com.codeborne.selenide.impl.SelenideElementProxy.dispatchAndRetry(SelenideElementProxy.java:99)
    at com.codeborne.selenide.impl.SelenideElementProxy.invoke(SelenideElementProxy.java:65)
    at com.sun.proxy.$Proxy16.shouldBe(Unknown Source)
    at my.project.java.TestClass.loginToService(TestClass.java:)
    at my.project.java.TestClass.beforeEach(TestClass.java:)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:628)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeMethodInExtensionContext(ClassTestDescriptor.java:302)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$synthesizeBeforeEachMethodAdapter$12(ClassTestDescriptor.java:290)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachMethods$2(TestMethodTestDescriptor.java:135)
    at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:155)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachMethods(TestMethodTestDescriptor.java:134)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:109)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Any help would be greatly appreciated

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Lotrein
  • 1
  • 1
  • This is an issue with your browser since you use new browser version. Update your chrome driver, User another browser , or downgrade your chrome driver will solve your issue. – Devdun Jul 16 '19 at 10:34

2 Answers2

1

This is an issue with your browser since you use newer browser version. Update your chrome driver, User another browser , or downgrade your chrome Browser will solve your issue.

If I'm right currently you are downloading chrome driver and configure and then you are running it. In such a case chrome or Firefox or what ever will be open but tests cannot be performed if you upgraded your browser to latest versions.

But Use webdriver manager to avoid such situations. So you can use any driver and only what you have to do is update version number of webdriver manager.

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.4.0</version>
</dependency>

You can call it like this

      if(browser.equalsIgnoreCase("Chrome")){
            WebDriverManager.chromedriver().setup();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--no-sandbox");
            chromeOptions.addArguments("window-size=1400,2100");
            driver = new ChromeDriver(chromeOptions);
        }
Devdun
  • 747
  • 5
  • 14
  • Hello! Thanks for the suggestion. Initiating chromedriver of an explicit version was done when we had some compatibility issues. I removed the option and the tests automatically downloaded the 75.xx version, with no warnings about any possible incompatibility. However, the issue persists. The tests still try to open the browser but then fail with the same exception, so the version disparity is not the issue here... – Lotrein Jul 16 '19 at 12:53
1

This error message...

[WARNING]: This version of ChromeDriver has not been tested with Chrome version 75.

...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.

Your main issue is the incompatibility between the version of the binaries you are using as follows:

  • You are using chromedriver=74.0
  • Release Notes of chromedriver=74.0 clearly mentions the following :

Supports Chrome v74

  • You are using chrome=75.0
  • Release Notes of ChromeDriver v75.0 clearly mentions the following :

Supports Chrome version 75

So there is a clear mismatch between ChromeDriver v74.0 and the Chrome Browser v75.0


Solution


Outro

You can find a similar discussion in This version of ChromeDriver has not been tested with Chrome version 75 error with Selenium ChromeDriver and Chrome through facebook/php-webdriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352