Serenity/WebDriver: cannot access localhost - java

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.

Related

Selenium "can't kill an exited process" [duplicate]

Server: Raspberry Pi 3
OS: Dietpi - version 159
Geckodriver version: 0.22 for arm
Firefox version: 52.9.0
Python version: 3.5
Selenium version: 3.14.1
Gecko is executable, and is located in /usr/local/bin/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_options=options)
print('Need your login credential')
username = input('What is your username?:\n')
password = input('What is your password?:\n')
...
...
Output:
root#RPi3:~# python3.5 ITE-bot.py
Traceback (most recent call last):
File "ITE-bot.py", line 12, in <module>
driver = webdriver.Firefox(firefox_options=options)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
Any idea what is wrong? I've tried google without luck.
If you are running Firefox on a system with no display, make sure you use headless mode.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
Also, make sure you have compatible versions of Firefox, Selenium, and Geckodriver:
https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
Thumb rule
A common cause for Browsers to crash during startup is running WebDriver initiated Browsers as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Browser as a regular user instead.
This error message...
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowsing Session i.e. Firefox Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
Your GeckoDriver version is 0.22.0.
Release Notes of GeckoDriver v0.21.0 (2018-06-15) clearly mentions the following:
Firefox 57 (and greater)
Selenium 3.11 (and greater)
Your Firefox version is 52.9.0.
So there is a clear mismatch between GeckoDriver v0.22.0 and the Firefox Browser v57
Solution
Upgrade GeckoDriver to GeckoDriver v0.22.0 level.
GeckoDriver is present in the specified location.
GeckoDriver is having executable permission for non-root users.
Upgrade Firefox version to Firefox v62.0.2 levels.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Execute your Selenium Test as a non-root user.
GeckoDriver, Selenium and Firefox Browser compatibility chart
I was on headless mode, using correct versions of everything, and the only way to get out of this error message was not to execute the selenium test as root
Yes checked Start Xvfb before the build can fix the problem, but if you have a job like a pipeline or multibranch pipeline this option is not visible. In the node of your Selenium grid that you go to execute the test you need:
1- Install Xvfb: apt install xvfb
2- Execute Xvfb: /usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 & export DISPLAY=":99"
3- Rerun your node, for example: java -jar selenium.jar -role node -hub http://#.#.#.#:4444/grid/register -capabilities browserName=firefox,plataform=linux -host #.#.#.# -port 1991
This solution worked for me
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
As there can be many different underlying causes for this error it is best to find the root cause setting selenium to use debug level logging. In my case, for Ruby with capybara I needed to set: Selenium::WebDriver.logger.level = :debug. And voilà, running the same spec I could see in the logs that a dependency was missing, in my case:
libdbus-glib-1.so.2: cannot open shared object file: No such file or directory
Couldn't load XPCOM.
After installing it all worked fine.
I used:
VS Code
Linunx/Ubuntu:18.10
Nightwatch.js
My problem was that I tried to run Nightwatch (which automatically starts GeckoDriver) from the VS Code terminal.
I had the same problem, and realized that the real problem was some firefox dependencies not being installed inside the docker container I was testing in.
Try to initiate firefox and check if it returns an error.
As Nico and jay have stated you need to check the logs to see the details of the error. As you might use different systems, you can specify the path where the log is stored (i.e. "/tmp/geckodriver.log").
from selenium import webdriver
firefox_options = webdriver.firefox.webdriver.Options()
driver = webdriver.Firefox(log_path="/tmp/geckodriver.log",
options=firefox_options)
In my particular case, what the log said was:
Error: no DISPLAY environment variable specified
That was resolved adding in the options the headless mode before starting the driver. With the line:
firefox_options.set_headless()
I was able to fix this by running my tests with Xvfb. I was running them on a remote server.
I was using Jenkins so I checked the box that looked like this:
Credit to https://www.obeythetestinggoat.com/book/chapter_CI.html
in my case, I was running test cases as root
geckodriver.log
1576076416677 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilenCbl2e"
Running Firefox as root in a regular user's session is not supported. ($HOME is /home/seluser which is owned by seluser.)
1576077143004 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile7wpSQ7"
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: telemetry
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1576077143689 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: about:reader*
1576077145372 Marionette INFO Listening on port 35571
1576077145423 Marionette WARN TLS certificate errors will be ignored for this session
1576077200207 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilenhoHlr"
Running Firefox as root in a regular user's session is not supported. ($HOME is /home/seluser which is owned by seluser.)
i could get around by
cd /home
chown -R root seluser
i woundnt say its correct but it got my job done

Can't build selenium image in docker

I am quite new to docker, and I am trying to get a really small selenium framework with just 1 test in the container. The test works all fine locally, but when I try to build it in the container it fails at the last stap when it tries to execute the tests via the mvn test command.
I get the following error: "could not start a new session. possible causes are invalid address of the remote server or browser start-up failure".
And this is my test I'm trying to get in the container:
I quess it is because I'm not doing something right when it comes to the browser. Any feedback to get me a step further would be highly appreciated.
Sharing my codes, How I manage docker and selenium. Hope it can help you
First create selenium HUB
docker run -d -p 4444:4444 --name selenium-hub selenium/hub:3.141.0-actinium
Then connect nodes with hub
docker run -d -P -p 5900:5900 --link selenium-hub:hub -v /dev/shm:/dev/shm selenium/node-chrome-debug:3.141.0-actinium
And Add the codes in #BeforeMethod
#BeforeMethod
public void Openbrowser() throws MalformedURLException {
nodeUrl = "http://172.17.0.3:5555/wd/hub";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.getCurrent());
driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
driver.manage().window().maximize();
driver.get("https://www.google.com");
}
You will get more details about docker here https://github.com/SeleniumHQ/docker-selenium
Also using chrome nodes debug you can view browser using vnc viewer.
Hope it will help you.

Is selenium 3.12 and geckodrvier 0.21.0 are compatible and stable, all tests gets skipped on linux machine

Platform details:
geckodriver 0.21.0 , Firefox: 60, Selenium: 3.12, cent Os 7
When i ran it using mvn it starts successfully:
geckodriver INFO Listening on 127.0.0.1:14185
Marionette INFO Listening on port 284135
Tests run successfully on windows machine however when running the same on CentOs 7, tests gets skipped.
I observed All tests get skipped as the GUI of Firefox gets closed after some time with below info and error on cmd console:
INFO: org.openqa.selenium.WebDriverException: java.io.IOException:
unexpected end of stream on Connection{localhost:33365, proxy=DIRECT
hostAddress=localhost/12 6.10.0.1:258107
[ERROR] java.net.ConnectException: Failed to connect to
localhost/127.0.0.1:2285
/bin/sh: line 1: 8780 Killed
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64/jre/bin/java
if(platform.equalsIgnoreCase("linux")) {
FirefoxOptions options = new FirefoxOptions();
DesiredCapabilities desiredCap = DesiredCapabilities.firefox();
profile.setPreference("browser.download.dir",System.getProperty("user.dir")+ File.separator + "target");
System.setProperty("webdriver.gecko.driver", "/path/geckodriver/geckodriver");
System.setProperty("webdriver.firefox.bin","/usr/bin/firefox/firefox");
desiredCap.setCapability(CapabilityType.PLATFORM_NAME,Platform.LINUX);
desiredCap.setCapability("webdriver.firefox.profile",DesiredCapabilities.firefox());
driver = new FirefoxDriver();
}
I have spent so much time on this but unable to find the root cause of this.
Using maven surefire plugin 2.19.1.
Kindly help me on this i am really stuck here.
As per the documentation below the combination of the binaries which you have mentioned in your question (Selenium v3.12 / GeckoDriver v0.21.0 / Firefox v60) are compatible and stable as follows:
This error message...
INFO: org.openqa.selenium.WebDriverException: java.io.IOException: unexpected end of stream on Connection{localhost:33365, proxy=DIRECT hostAddress=localhost/12 6.10.0.1:258107
[ERROR] java.net.ConnectException: Failed to connect to localhost/127.0.0.1:2285
...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowser i.e. Firefox Browser session.
As you have mentioned about using GeckoDriver v0.21.0 there is no need to mention the setProperty with webdriver.firefox.bin. You need to ensure that Mozilla Firefox is installed at the default location for each system.
Solution
As per your code trials though you have created and configured the FirefoxOptions Class and DesiredCapabilities Class objects, you havn't passed them during initializing the WebDriver.
If your usecase requires the FirefoxOptions Class and DesiredCapabilities Class objects you need to pass them during initializing the WebDriver and Web Browser.
If your usecase does not requires the FirefoxOptions Class and DesiredCapabilities Class objects you need to remove them.
Your code looks fine to me.
Check for all the processes being used in your automation, make sure multiple processes are not running. Most importantly for following:
ps -ef|grep firefox
ps -ef|grep geckodriver
ps -ef|grep java
Close if multiple process are running
Check for any error logs:
sudo vi /var/log/messages
Find for Kill or ERROR.This should help where it is breaking.

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON while registering Selenium Grid Node to Hub

i installed selenium-server-standalone-3.4.0.jar and run following command(as hube) on my machine A:
java -jar selenium-server-standalone-3.4.0.jar -role hub
it runs successfuly.
then, on my machine B(as node) run following command:
java -jar selenium-server-standalone-3.4.0.jar -role node -hub http://192.168.1.27:4444/grid/register
following logs produced. As you can see see, first it registered then it indicates it is not registered. How could be possible?
> 11:35:29.561 INFO - Selenium build info: version: '3.4.0', revision: 'unknown'
> 11:35:29.562 INFO - Launching a Selenium Grid node
> 11:35:30.353 WARN - error getting the parameters from the hub. The node may end up with wrong timeouts.com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
> 2017-07-23 11:35:30.417:INFO::main: Logging initialized #1343ms to org.seleniumhq.jetty9.util.log.StdErrLog
> 11:35:30.568 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform LINUX
> 11:35:30.568 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform LINUX
> 11:35:30.572 INFO - Driver class not found: com.opera.core.systems.OperaDriver
> 11:35:30.572 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
> 11:35:30.573 INFO - Driver class not found: com.opera.core.systems.OperaDriver
> 11:35:30.575 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
> 11:35:30.585 INFO - Driver provider org.openqa.selenium.safari.SafariDriver registration is skipped:
registration capabilities Capabilities [{browserName=safari, version=, platform=MAC}] does not match the current platform LINUX
> 2017-07-23 11:35:30.729:INFO:osjs.Server:main: jetty-9.4.3.v20170317
> 2017-07-23 11:35:30.828:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler#70be0a2b{/,null,AVAILABLE}
> 2017-07-23 11:35:30.899:INFO:osjs.AbstractConnector:main: Started ServerConnector#29176cc1{HTTP/1.1,[http/1.1]}{0.0.0.0:5555}
> 2017-07-23 11:35:30.900:INFO:osjs.Server:main: Started #1827ms
> 11:35:30.900 INFO - Selenium Grid node is up and ready to register to the hub
> 11:35:31.017 INFO - Starting auto registration thread. Will try to register every 5000 ms.
> 11:35:31.017 INFO - Registering the node to the hub: http://192.168.1.27:4444/grid/register
> 11:35:31.062 INFO - The node is registered to the hub and ready to use
> 11:35:36.095 INFO - Couldn't register this node: The hub is down or not responding: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
> 11:35:41.123 INFO - Couldn't register this node: The hub is down or not responding: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
> 11:35:46.135 INFO - Couldn't register this node: The hub is down or not responding: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
Here is the Answer to your Question:
Assuming that your requirement is to run the Selenium Grid Hub on default port 4444 and Selenium Grid Node on default port 5555 we need to configure and start them as follows:
Start the Selenium Grid Hub:
java -jar selenium-server-standalone-3.4.0.jar -role hub
The following logs confirms your Selenium Grid Hub is running properly:
2017-07-24 15:31:46.139:INFO:osjs.Server:main: Started #2757ms
15:31:46.140 INFO - Nodes should register to http://192.168.0.107:4444/grid/register/
15:31:46.140 INFO - Selenium Grid hub is up and running
Access the Selenium Grid Console through the URL:
http://localhost:4444/grid/console
Start the Selenium Grid Node:
java -jar selenium-server-standalone-3.4.0.jar -role node -hub http://<IP_GRID_HUB>:4444/grid/register
I have used localhost so I used:
java -jar selenium-server-standalone-3.4.0.jar -role node -hub http://localhost:4444/grid/register
The following logs confirms your Selenium Grid Node is running properly:
15:35:44.939 INFO - Selenium Grid node is up and ready to register to the hub
15:35:44.958 INFO - Starting auto registration thread. Will try to register every 5000 ms.
15:35:44.958 INFO - Registering the node to the hub: http://localhost:4444/grid/register
15:35:45.231 INFO - The node is registered to the hub and ready to use
Access the Selenium Grid Console through the console URL to see the registered Node:
http://localhost:4444/grid/console
Let me know if this Answers your Question.
I was able to solve this by updating the version of chrome to latest (60) and the version of the chromedriver to latest.
It finally started working when I updated the version of the chromedriver on the hub side as well.
When I have some time I will be switching to seleniumGridExtra which is supposed to manage the versions automatically.
My work log:
8/3/2017, 10:11:03 AM
Still trying to get selenium running on the hub/node setup. It stopped 17 hours ago.
Updating all the files involved.
Latest chrome firefox and IE drivers.
Latest selenium version
Update chrome
rebooted machine
Updated chromeDriver on the hub..which seems silly.
Suddenly working fine.
Lessons learned:
Switch to selenium grid extras. They handle the version dependencies.
I am new to this but here what works for me. I run hub and node from same machine, I run hub and node from the window batch file. I run the hub first and make sure to leave the window opened then I run the grid. I didn't the connection issue since I started doing that

Unable to create WebDriver instance on Selenium 3.0 Grid / Node

Trying to run some Selenium tests locally using Selenium 3.0 but get the following error:
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{marionette=true, screen-resolution=1680x1050, commandTimeout=300, record-video=true, version=50.1.0, platform=MAC, tags=[Ljava.lang.String;#1ac4da8f, build=jgilmore-12291406, idleTimeout=120, name=homePageSmokeTest, browserName=firefox, seleniumVersion=null, maxDuration=360}], required capabilities = Capabilities [{}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700
Currently running the Selenium Hub as follows:
java -jar /Users/jgilmore/Selenium/selenium-server-standalone-3.0.1.jar -role hub -timeout 15
And the Node (trying to run Firefox 50.1.0 with Geckodriver first):
java -Dwebdriver.gecko.driver=/Users/jgilmore/Selenium/geckodriver -jar selenium-server-standalone-3.0.1.jar -port 5557 -role node -hub http://localhost:4444/grid/register -browser "browserName=firefox, browserVersion=50.1.0, maxInstances=10"
The Grid and Node startup just fine (see them in the Grid Console), but I can't create a WebDriver instance when trying to run my test(s). This used to work for me with Selenium 2.x, so I am assuming I'm missing something here. Any help is appreciated!
So with the help of gasalis, I think we figured out my issue:
Change browserVersion to version on both the Node and my local configuration.
For whatever reason, I have to set the browser version for Firefox and Chrome to ANY -- I can't explicitly state the browser version such as "50.1.0" for Firefox or "55" for Chrome. Probably something I'm missing, but that's an issue for another question.
By changing those two things, I can now successfully create the WebDriver(s) necessary to run my local Selenium scripts.
Thanks for your help gsaslis!

Categories