简体   繁体   中英

Serenity/WebDriver: cannot access localhost

I have problems with Serenity/WebDriver. Suddenly one day, my code doesn't work and I got the error "Unknown host localhost".

I can reproduced with the code from http://thucydides.info/docs/serenity-staging/#_serenity_with_cucumber :

@RunWith(SerenityRunner.class)
public class WhenSearchingOnGoogle {

    @Managed                                                                
    WebDriver driver;

    @Test
    public void shouldInstantiateAWebDriverInstanceForAWebTest() {
        driver.get("http://www.google.com");                                

        driver.findElement(By.name("q")).sendKeys("firefly", Keys.ENTER);

        new WebDriverWait(driver,5).until(titleContains("Google Search"));

        assertThat(driver.getTitle()).isEqualTo("firefly - Google Search");
    }
}

I got this error:

TEST STARTED: shouldInstantiateAWebDriverInstanceForAWebTest

18:06:11.599 [main] INFO net.serenitybdd.core.Serenity - TEST NUMBER: 1 sept. 22, 2018 6:06:11 PM org.openqa.selenium.remote.DesiredCapabilities chrome INFOS: Using new ChromeOptions() is preferred to DesiredCapabilities.chrome()

net.serenitybdd.core.exceptions.SerenityManagedException: localhost could not be reached

at net.serenitybdd.core.webdriver.driverproviders.RemoteDriverBuilder.newRemoteDriver(RemoteDriverBuilder.java:66)
at net.serenitybdd.core.webdriver.driverproviders.DefaultRemoteDriver.buildWithOptions(DefaultRemoteDriver.java:24)
at net.serenitybdd.core.webdriver.driverproviders.RemoteDriverProvider.newInstance(RemoteDriverProvider.java:53)
at net.thucydides.core.webdriver.WebDriverFactory.newWebdriverInstance(WebDriverFactory.java:127)
at net.thucydides.core.webdriver.WebDriverFacade.newDriverInstance(WebDriverFacade.java:149)

I do not understand the "localhost could not be reached" when I give another URL ?!

I tried others drivers with no success. How can I make it work ?

Thks, Cédric

It seems that a remote webdriver was used, with localhost:4444, certainly by this parameter in serenity.properties: webdriver.remote.url= http://localhost:4444/wd/hub

i solved this with https://github.com/SeleniumHQ/docker-selenium/blob/master/README.md#selenium-grid-hub-and-nodes : creating a docker-compose.yml, then starting with docker compose up -d stopping with docker-compose down

If you run the selenium server locally its simple

1. Please download the binary from 

Selenium downloads page

Now navigate to the folder contains the jar and open command prompt there and run the commands in step 2 and 3

2 . Start hub :

    java -jar selenium-server-standalone-3.14.0.jar -port 4444 -role hub

3. Register:

java -jar selenium-server-standalone-3.14.0.jar -role node -hub http://localhost:4444/grid/register 

After that you have to find the matching drivers for the version of chrome. for example v69 using chromdriver version 2.42.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM