selenide: Can't use SelenideElement if it was declared before setWebDriver() method

The problem

As per issue title, declaring a SelenideElement before calling WebDriverRunner.setWebDriver(driver) method leads to an IllegalStateException whenever user tries to interact with the element.

Details

If we do not use setWebDriver method, then everything works regardless of where you declare SelenideElement. It can be a test class field for instance and still works. Exception stacktrace

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:65)
	at com.codeborne.selenide.impl.ElementFinder.getSearchContext(ElementFinder.java:86)
	at com.codeborne.selenide.impl.ElementFinder.getWebElement(ElementFinder.java:74)
	at com.codeborne.selenide.impl.WebElementSource.checkCondition(WebElementSource.java:47)
	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.$Proxy5.shouldBe(Unknown Source)

Tell us about your environment

  • Selenide Version: 5.0.1
  • Chrome\Firefox\IE Version: irrelevant
  • Browser Driver Version: irrelevant
  • Selenium Version: 3.x
  • OS Version: irrelevant

Code To Reproduce Issue [ Good To Have ]

Easily reproducible with:

        SelenideElement a=$("abc");
        WebDriverRunner.setWebDriver(new ChromeDriver());
        open("http://google.com");
        a.shouldNot(exist);

And this way it works

        WebDriverRunner.setWebDriver(new ChromeDriver());
        SelenideElement a=$("abc");
        open("http://google.com");
        a.shouldNot(exist);

Also this works

public class SimpleTest {

    public SelenideElement search = $(By.name("q"));

    @Test
    public void testSimple() {
        open("https://www.google.com");
        search.shouldBe(Condition.visible);
    }

}

And this does not

public class SimpleTest {

    public SelenideElement search = $(By.name("q"));

    @Test
    public void testSimple() {
        WebDriverRunner.setWebDriver(new ChromeDriver(new ChromeOptions()));
        open("https://www.google.com");
        search.shouldBe(Condition.visible);
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 29 (23 by maintainers)

Most upvoted comments

I still see it as a bug. I will propose a fix later this week.

Actually, I’ve looked into code and it looks like bug. I hope to fix it until Sunday.

Viele Grüße

Alexei Vinogradov

On 6. December 2018 at 11:41:40, pavelpp (notifications@github.com) wrote:

@jkromski https://github.com/jkromski that would have been true if it didn’t work without using setWebDriver() method, letting Selenide setup driver, but it’s not the case. The issue is only happening when setWebDriver method is used. See my example above without usage of setWebDriver, where element is declared as a field - this still works.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/selenide/selenide/issues/873#issuecomment-444828429, or mute the thread https://github.com/notifications/unsubscribe-auth/AGQ4YGlQdWzwKCbtrMcn9vkhsFYxj1qVks5u2PRkgaJpZM4ZCdXD .

$() is not supposed to set up any particular driver, it just making use of a drivers configured at other places.

So I guess it is a bug)

Viele Grüße

Alexei Vinogradov

On 6. December 2018 at 11:22:58, Jacek (notifications@github.com) wrote:

From what I understand, by using $() you setting up a default driver and $ uses it to declare SelenideElement, so if you’re setting driver manually after $ then Selenide closes previous one and that is why you get the error.

SelenideElement a=$(“abc”); // default driver (created for you automatically)WebDriverRunner.setWebDriver(new ChromeDriver()); // your driver, default driver being closed open(“http://google.com”); // using your driver a.shouldNot(exist); // as this is proxy it remembers default driver from line 1

I think this is not an bug, you should set driver before using it. if you need to declare element before then declare selector only as String and after setting up the driver use $()

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/selenide/selenide/issues/873#issuecomment-444822975, or mute the thread https://github.com/notifications/unsubscribe-auth/AGQ4YI_i4aYdKRKwlwNdTCwLL41QblZZks5u2PACgaJpZM4ZCdXD .