How to run selenium test in ubuntu? - selenium

I am trying to running selenium test cases that invoke the chromedrivers with ubuntu.
I am facing below issue
[1;31mERROR[m] [1;31m Run 1: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'ip-10-1-8-153', ip: '10.1.8.153', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-1045-aws', java.version: '11.0.16'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x55cc6f1882c3 <unknown>
Please help me to resolve this issue , I am using below test configurations
public WebDriver beforeScenario() {
if (cr.valueOnTheKey("Env").equals("PROD")) {
WebDriverManager.chromedriver().setup();
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedrive");
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
// options.addArguments("headless");
options.addArguments("no-sandbox");
options.addArguments("disable-dev-shm-usage");
options.addArguments("disable-gpu");
driver = new ChromeDriver(options);
// driver.get("https://www.google.com");
driver.manage().window().maximize();
return driver;
}
I am using below steps before running mvn clean test
sh 'sudo apt-get update -y'
sh 'sudo apt-get upgrade -y'
sh 'sudo apt install xvfb -y'
sh 'sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb'
sh 'sudo dpkg -i google-chrome-stable_current_amd64.deb || true'
sh 'sudo apt-get install -f -y'
sh 'sudo google-chrome -version'
sh 'sudo chmod 777 /usr/bin/google-chrome'
sh 'sudo nohup /usr/bin/google-chrome &'
sh 'sudo wget https://chromedriver.storage.googleapis.com/105.0.5195.19/chromedriver_linux64.zip'
sh 'sudo unzip chromedriver_linux64.zip'
sh 'sudo mv chromedriver /usr/bin/chromedrive'
sh 'sudo chmod 777 /usr/bin/chromedrive'
sh 'sudo nohup Xvfb -ac :99 -screen 0 1280x1024x16 & DISPLAY=:99 &'

Related

Chrome driver version 92 not running tests

I am having issues to run the version 92 with the browser open, in headless mode is working fine.
I am currently running my tests on a docker container. The installation of the chrome-driver follows:
RUN curl -s https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \
apt-get update && \
apt-get install -y \
xvfb \
google-chrome-stable=92.\* \
unzip
RUN curl -s -o /tmp/chromedriver.zip "https://chromedriver.storage.googleapis.com/$(curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE_92)/chromedriver_linux64.zip" && \
unzip /tmp/chromedriver.zip chromedriver -d /usr/bin/ && \
chmod +x /usr/bin/chromedriver
after executing my command to run the tests, the browser open but nothing happens.
I get stuck on data; on the address bar, and page keeps loading forever.
I'm currently using the chrome options:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
Looks like it was related to an old Nvidia driver running on Linux:
You may need to run with --disable-gpu on Linux with NVIDIA driver older than 295.20
Source: chromium documentation
So, adding chrome_options.add_argument("--disable-gpu") solved my problem.

How to Fix " org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed" in docker container

I am trying to build a docker image for my selenium tests. However i keep getting error message " org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed" .
Please do not mark this as Duplicate ,though I have referred to a lot of answers provided in the links below. I am still not able to get through this. I have tried all the answers that are provided but no luck .
Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed
WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser
Please find the docker file code and my selenium code.
Docker file code looks like this :
FROM selenium/standalone-chrome
FROM gradle
RUN gradle wrapper
USER root
RUN apt-get update; apt-get -y install wget gnupg2
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub |
apt-key add -
RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable
main" >> /etc/apt/sources.list.d/google-chrome.list
RUN apt-get update; apt-get -y install google-chrome-stable
COPY . /project
RUN chown -R gradle:gradle /project
RUN wget -N
http://chromedriver.storage.googleapis.com/76.0.3809.25/chromedriver_linux64.zip -P ~/
RUN unzip ~/chromedriver_linux64.zip -d ~/
RUN rm ~/chromedriver_linux64.zip
RUN mv -f ~/chromedriver /project/executables/chromedriver
RUN chown gradle:gradle /project/executables/chromedriver
RUN chmod 0755 /project/executables/chromedriver
USER gradle
WORKDIR /project
ENV GRADLE_USER_HOME /project/.gradle_home
CMD gradle build --info
Selenium code :
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized"); // open Browser in maximized mode
chromeOptions.addArguments("disable-infobars"); // disabling infobars
chromeOptions.addArguments("--disable-extensions"); // disabling extensions
chromeOptions.addArguments("--disable-gpu"); // applicable to windows os only
chromeOptions.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
chromeOptions.addArguments("--no-sandbox"); // Bypass OS security model
System.setProperty("webdriver.chrome.driver","executables/chromedriver");
Webdriver driver = new ChromeDriver(chromeOptions);
driver.get("http://google.com");
As you can see from the error message the chrome is starting at default location(usr/bin/google-chrome) but it is crashing .
Starting ChromeDriver 76.0.3809.25 (a0c95f440512e06df1c9c206f2d79cc20be18bb1-refs/branch-heads/3809#{#271}) on port 30275
Only local connections are allowed.
" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed" .
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
System info: host: 'd2e61fa0170d', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.125-linuxkit', java.version: '1.8.0_212'
Driver info: driver.version: ChromeDriver
I am using latest chrome driver 76.0.3809.25. I am assuming that latest google chrome is fetched and installed
Any help is appreciated
Seems like you are having issues with installing google chrome and its driver. Sharing you my Dockerfile and Docker-compose.yml. I achieved this using python. It also has the example for Firefox and PhantomJS.
FROM ubuntu:bionic
RUN apt-get update && apt-get install -y \
python3 python3-pip \
fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 \
libnspr4 libnss3 lsb-release xdg-utils libxss1 libdbus-glib-1-2 \
curl unzip wget \
xvfb
# install geckodriver and firefox
RUN GECKODRIVER_VERSION=`curl https://github.com/mozilla/geckodriver/releases/latest | grep -Po 'v[0-9]+.[0-9]+.[0-9]+'` && \
wget https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz && \
tar -zxf geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/local/bin && \
chmod +x /usr/local/bin/geckodriver && \
rm geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz
RUN FIREFOX_SETUP=firefox-setup.tar.bz2 && \
apt-get purge firefox && \
wget -O $FIREFOX_SETUP "https://download.mozilla.org/?product=firefox-latest&os=linux64" && \
tar xjf $FIREFOX_SETUP -C /opt/ && \
ln -s /opt/firefox/firefox /usr/bin/firefox && \
rm $FIREFOX_SETUP
# install chromedriver and google-chrome
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
wget https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
unzip chromedriver_linux64.zip -d /usr/bin && \
chmod +x /usr/bin/chromedriver && \
rm chromedriver_linux64.zip
RUN CHROME_SETUP=google-chrome.deb && \
wget -O $CHROME_SETUP "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb" && \
dpkg -i $CHROME_SETUP && \
apt-get install -y -f && \
rm $CHROME_SETUP
# install phantomjs
RUN wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 && \
tar -jxf phantomjs-2.1.1-linux-x86_64.tar.bz2 && \
cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs && \
rm phantomjs-2.1.1-linux-x86_64.tar.bz2
RUN pip3 install selenium
RUN pip3 install pyvirtualdisplay
RUN pip3 install Selenium-Screenshot
RUN pip3 install requests
RUN pip3 install pytest
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONUNBUFFERED=1
ENV APP_HOME /usr/src/app
WORKDIR /$APP_HOME
COPY . $APP_HOME/
CMD tail -f /dev/null
CMD python3 example.py
Docker-compose.yml
selenium:
build: .
ports:
- 4000:4000
- 443:443
volumes:
- ./data/:/data/
privileged: true

Selenium Only local connections allowed and NoSuchSessionException

I have the following docker-container
FROM ubuntu:xenial
MAINTAINER Hasan Kara
RUN set -xe \
\
# Install Java, Chrome, Xvfb, and unzip
&& apt-get update \
&& apt-get install -y \
openjdk-8-jre \
chromium-browser \
xvfb \
curl \
wget \
unzip \
&& rm -rf /var/lib/apt/lists/* \
&& ln -s /usr/lib/chromium-browser/chromium-browser /usr/bin/google-chrome \
\
# Download and install chrome drive and selenium server standalone
&& wget -q "https://chromedriver.storage.googleapis.com/2.27/chromedriver_linux64.zip" \
&& wget -q "http://selenium-release.storage.googleapis.com/3.0/selenium-server-standalone-3.0.1.jar" \
&& unzip chromedriver_linux64.zip \
&& mv chromedriver /usr/local/bin \
&& mv selenium-server-standalone-3.0.1.jar /usr/local/bin
VOLUME /downloads
ENV DISPLAY :10
CMD export DISPLAY=:10 && Xvfb :10 -screen 0 1366x768x24 -ac & google-chrome --no-sandbox -remote-debugging-port=9222 & java -jar /usr/local/bin/selenium-server-standalone-3.0.1.jar &
EXPOSE 4444 9222
Which I run with:
docker run --rm -it --shm-size=512m --name chromium -p 4444:4444 -p 9222:9222 hasankarafhnw/seleniumchromium /bin/bash
And inside the bash I run by hand, because for some reason the CMD doesn't work...
export DISPLAY=:10 && Xvfb :10 -screen 0 1366x768x24 -ac & google-chrome --no-sandbox -remote-debugging-port=9222 & java -jar /usr/local/bin/selenium-server-standalone-3.0.1.jar &
Now I can perfectly connect to the selenium hub at "http://192.168.99.100:4444/wd/hub/static/resource/hub.html", but if try:
Create a Session through the hub
Create a Session with this code in the container.
Create a Session though a RemoteWebDriver running on the host-os.
I get the following error:
Only local connections are allowed.
INFO - Attempting bi-dialect
session, assuming Postel's Law holds true on the remote end
INFO - Executing: [take screenshot]) WARN - Exception thrown
org.openqa.selenium.NoSuchSessionException: no such session
Chromedriver: 2.27.440175
Chromium: 55.0.2883.87
Selenium-server-standalone: 3.0.1
Docker: 1.13
Host-OS: Win 7
RemoteDriveCode:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("./extension_0_2_0_10.crx"));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL( "http://192.168.99.100:4444/wd/hub"), capabilities);
driver = new Augmenter().augment(driver);
driver.get("http://google.com");
You need to setup whitelisted-ips argument for chromedriver executable (not chrome!). You can achive it by set system property webdriver.chrome.whitelistedIps when node starts, like in your command:
CMD export DISPLAY=:10 && Xvfb :10 -screen 0 1366x768x24 -ac & google-chrome --no-sandbox -remote-debugging-port=9222 & java -Dwebdriver.chrome.whitelistedIps= -jar /usr/local/bin/selenium-server-standalone-3.0.1.jar &

Chrome driver throwing "org.openqa.selenium.remote.SessionNotFoundException" when running selenium tests heedlessly in linux?

Environment: Centos7, Chromedriver2.24, chrome-browser-stable version 2.53, selenium version 2.53.1 and xvfb
When running tests parallel with 8 concurrent threads giving session not found exception..
Chrome-setup:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList(
"--disable-logging", "--silent", "--log-level 3"));
capabilities.setCapability("chrome.logfile", "NUL");
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver",
"servers/chromedriver-local");
driver = new ChromeDriver(capabilities);
Any help much appreciated!!
This is what fixed my issue:
Simpy mount -v /dev/shm:/dev/shm
Or, longer, create a big shm
Started in privileged mode: docker run --privileged
Fix small /dev/shm size
docker exec $id sudo umount /dev/shm
docker exec $id sudo mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=512M tmpfs /dev/shm
However it would be nice to avoid privileged mode.
(Ref:github.com/elgalu/docker-selenium/issues/20)

How to implement chromedriver in selenium in Linux platform

Can somebody tell me how to use the Chrome driver in Selenium for Linux platform?
I have my chrome driver location at username/home/chromedriver.
My code is:
System.setProperty("webdriver.chrome.driver", "/home/username/ChromeDriver/chrome‌​driver");
driver = new ChromeDriver();
driver.get("facebook.com");
The error I am getting is:
org.openqa.selenium.WebDriverException: Unable to either launch or
connect to Chrome. Please check that ChromeDriver is up-to-date.
Using Chrome binary at: /opt/google/chrome/google-chrome
(WARNING: The server did not provide any stacktrace information)
From [the official documentation](https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver:
Requirements
The ChromeDriver controls the browser using Chrome's automation proxy
framework.
The server expects you to have Chrome installed in the default
location for each system:
OS Expected Location of Chrome
-------------------------------------
Linux /usr/bin/google-chrome
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary. See also the section on overriding the Chrome binary location.
Getting Started
To get set up, first
download the
appropriate prebuilt server. Make sure the server can be located on
your PATH or specify its location via the webdriver.chrome.driver
system property. Finally, all you need to do is create a new
ChromeDriver instance:
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
Therefore, download the version of chromedriver you need, unzip it somewhere on your PATH (or specify the path to it via a system property), then run the driver.
We have installed Successfully
sudo apt-get install unzip
wget -N http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip -P ~/Downloads
unzip ~/Downloads/chromedriver_linux64.zip -d ~/Downloads
chmod +x ~/Downloads/chromedriver
sudo mv -f ~/Downloads/chromedriver /usr/local/share/chromedriver
Change the directory to /usr/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
Now run the script and add the following in the environment file.
Capybara.register_driver :chrome do |app| client = Selenium::WebDriver::Remote::Http::Default.new Capybara::Selenium::Driver.new(app, :browser => :chrome, :http_client => client) end
Capybara.javascript_driver = :chrome
Note : Change the chrome driver version according to your operating system type like 32 bit or 64 bit.
Here is a complete script for Linux 18.04 to install Google Chrome and the chrome driver. It should automatically adjust to collect the correct chrome driver for the browser.
#!/usr/bin/env bash
# install the latest version of Chrome and the Chrome Driver
apt-get update && apt-get install -y libnss3-dev
version=$(curl http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
wget -N http://chromedriver.storage.googleapis.com/${version}/chromedriver_linux64.zip
unzip chromedriver_linux64.zip -d /usr/local/bin
chmod +x /usr/local/bin/chromedriver
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
For me worked with these commands:
Unziped the file -> unzip -q chromedriver_linux64.zip
Force the copy to the directory 'usr/bin' -> sudo mv -f chromedriver /usr/bin
The selenium code was something like that.
System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://mvnrepository.com");
driver.close();
You can see small example from this example
For linux, i downlaod chrome driver and keep as system path variable(or put in exist path folder). And from code i use following ways (add property and initiate with path of chrome driver)
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/usr/local/bin/chromedriver"))
.usingAnyFreePort()
.build();
try {
service.start();
} catch (IOException e) {
e.printStackTrace();
}
return new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());