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.
Related
I am trying to run an application which runs Selenium in order to take some screenshots.
When I am running the application in a docker compose file it all works fine, however, when I try to run in in a kubernetes cluster in the cloud I keep on getting the following message:Only local connections are allowed, and no connections seem to be established. To my mind the issue is due to networking and selenium does not allow connections which are not coming from localhost, as is the case in kubernetes.
I am using image: selenium/standalone-chrome image (selenium/standalone-chrome:3.141 in my chart), where apparently the chrome driver is: 2.43.600233
I have been trying to counter this with the --whitelisted-ips option, but to no avail. I have tried:
chromeOptions.addArguments("--whitelisted-ips");
chromeOptions.addArguments("--whitelisted-ips=");
chromeOptions.addArguments("--whitelisted-ips=''");
Here is some of my java code.
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.TakesScreenshot;
chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--verbose");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--whitelisted-ips=");
chromeOptions.addArguments("--disable-gpu");
Here is what I seen in the logs.
you need to setup whitelisted-ips argument for chromedriver executable. You can achive it by set env JAVA_OPTS for docker chrome-node image:
chrome:
image: selenium/node-chrome:3.141.59
container_name: chrome
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
- JAVA_OPTS=-Dwebdriver.chrome.whitelistedIps=
fyi.
chromeOptions.addArguments("--whitelisted-ips="); pass argument into chrome not chromedriver!
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.
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.
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!
I am running selenium-server-standalone-2.17.0 (for IE and Firefox) and ChromeDriver 18.0.1022.0 (standalone) on a test box (Windows 7 64bit) which I use to run Java selenium tests against.
For some reason, when running my tests against ChromeDriver, the first time it encounters an unexpected Alert box, it blocks idefinitely and the ChromeDriver log says
WARNING: Executing: executeScript
I configured ChromeDriver using the guide http://code.google.com/p/selenium/wiki/ChromeDriver and set the timeout of all the drivers with
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Update:
I figured out how to initialize the remote ChromeDriver in a clean way with
URL url = new URL("http://192.168.1.15:4444/wd/hub");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Webdriver chromeDriver = new RemoteWebDriver(url, capabilities);
this ran best with the URL pointing to a selenium-server running chromedriver in a child process. You can make selenium-server run the ChromeDriver by starting it like this:
java -jar C:\selenium-server.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe
I still have the same problem with Chrome getting stuck at the unexpected Alert box, but the selenium log gave me at bit more info:
INFO - Done: /session/1328623219287/element/253/click
INFO - Executing: [execute script: return !!document['readyState'];, []] at URL: /session/1328623219287/execute)
Still have no idea what is causing this... can anyone help?
This is how I initialize ChromeDriver:
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY,
"PathToWhereChromeDriverIsAvailable");
ChromeDriverService service = ChromeDriverService.createDefaultService();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
ChromeDriver cd = new ChromeDriver(service, options);
With the Alert() i have just plain guess - probably it hangs out while executing the script - so basically you are not waiting for page to load, but for script to end executing. However, I do not have solution for this...