Codeception auto start and auto shutdown selenium server? - selenium

I'm using Codeception and I want to do Selenium tests.
Obviously, I need Selenium server running and I can spin it up with:
java -jar ./path/to/selenium/binary
However, I've used another testing framework in past which allowed me to specify this path in config file. Then, whenever I did something like codecept run, the framework automatically started selenium server and also shutted it down after all tests were completed.
Can I do this with Codeception? I tried to put exec() in _bootstrap file but it didn't work..

I'm using phantomjs - headless alternative to selenium. This extension starts phantomjs automatically with codecept run. Also phantomjs is faster than selenium.
You can write your own extension for running selenium - have a look at this file.

I start my tests with a bash script and then start and stop selenium via screen:
#!/usr/bin/env bash
# Start selenium in detached screen
screen -d -m -S "selenium" java -Dwebdriver.chrome.driver=./path/to/chromedriver -jar ./path/to/selenium/binary || true
./vendor/bin/codecept run -c codeception.yml --fail-fast --coverage --steps --coverage-html --html || error=true
# quit selenium
screen -S selenium -X quit || true
This might not fit the use-case but I can execute my code in my bash on Windows and Ubuntu as well as via my jenkins build process.

Related

Selenium side runner + chromedriver tests with docker not running

I am trying to get selenium side runner to run some tests using docker, to include in our CI.
I am able to run the tests locally in my machine by running:
selenium-side-runner C:\path-to-tests\tests-selenium.side
This is windows host.
I am trying to do the same using docker locally, so afterwards I will migrate this to our Teamcity.
First I am running the selenium server container:
docker run -d -p 4444:4444 --name chromedriver selenium/standalone-chrome:3.4.0
Afterwards I run the selenium side runner container:
docker run -v C:\path-to-tests:/sides --link chromedriver:chromedriver nixel2007/docker-selenium-side-runner
I have to link the containers otherwise I get an error saying that the container can't connect to chromedriver:4444
I also have to mount the volume where my tests are.
When I do this and run, I get the following error:
Test suite failed to run
WebDriverError: Unable to parse new session response
What am I missing here?
UPDATE:
I also tried different versions of the selenium/standalone-chrome container, selenium/standalone-chrome:3.4.0, selenium/standalone-chrome:3.141.59-xenon and selenium/standalone-chrome:latest
All fail with different errors.
SECOND UPDATE:
I have been able to get the tests to run, both locally and in teamcity. One of the issues that I am facing right now is that docker-compose seems to hang. Not sure if this is container related, or docker-compose related.
When I run the tests, the selenium side runner container exits with code 1 and I do not get back to the host console prompt, it stays forever waiting for something to happen.
The error is this:
selenium_selenium-side-runner_1 exited with code 1
I have gotten the docker-compose file from here:
https://github.com/nixel2007/docker-selenium-side-runner/blob/master/docker-compose.yml
Any clues on what I might be missing?

Using GOCD with Selenium BDD framework

I have developed BDD cucumber test framework using Selenium and Java. Our dev ops uses GOCD as CI CD tool so I need to integrate framework with GOCD.
I have integrated selenium framework with Jenkins numerous times but its my first time with GOCD. I have tried to look for plugin but didnt find any and neither any assistance with blogs or any support.
Is there any way that I can integrate selenium framework with GOCD so that it can be used in pipeline?
Basically you need to have program you can run at the command line that GoCD can call for you as part of a test task/job/stage.
Then you need a GoCD agent with all the necessary software installed, and run that command. (Or have a docker image with all the necessary software, and run the tests in that command).
One common problem is that selenium automates browsers, and browsers tend to want to have a display environment, and GoCD tends to run on servers without any GUI.
On Linux, there's a tool called Xvfb that provides a virtual X server which tricks the browser into thinking there is a GUI.
This is a small bash script that we run before starting the Selenium tests:
#!/bin/bash
DISPLAY=":99"
# The last one wins
killall -q Xvfb
# Start virtual x-server.
Xvfb -ac :99 -screen 0 1280x1024x16 > x11.log 2>&1 &
XSERVER_PID=$!
sleep 5
kill -0 $XSERVER_PID
if [ $? -gt 0 ]; then
echo "cannot start Xvfb"
exit 1
fi
# Start VNC server for watching the test runs and debugging problems.
x11vnc --listen 0.0.0.0 -rfbport 5900 -display :99 -forever >> x11.log 2>&1 &
export DISPLAY=:99
# Window manager for enabling resize of browser window.
fvwm >> x11.log 2>&1 &
Note that this also starts a lightweight window manager (fvwm) to enable tests that resize browser windows.

Getting Protractor.js to run on Jenkins build failing -- Error forwarding the new session empty pool of VM for setup capabilities

I am trying to get Protactor.js unit testing up and running with Jenkins on a windows server but I am having trouble getting it to run.
Currently this is the steps I have so far:
#install protractor via package.json
cd ./src/My\ Editor/WebApp/Scripts/protractor && D:Jenkins/nodesjs/npm install
[other build steps here]
#start webdriver-manager
./src/My\ Editor/WebApp/Scripts/protractor/node_modules/protractor/bin/webdriver-manager start > /dev/null 2>&1 &
#wait until selenium up
while ! curl http://localhost:4444/wd/hub/status &>/dev/null
do
true
done
#run protractor
cd ./src/My\ Editor/WebApp/Scripts/protractor/node_modules/protractor/bin/
./protractor ../../../conf.js
#stop selenium
curl -s -L http://localhost:4444/selenium-server/driver?cmd-shutDownSeleniumServer > /dev/null 2>&1
everything works well until the command 'protractor conf.js'. I get this output:
[21:12:34] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[21:12:34] I/launcher - Running 1 instances of WebDriver
[21:12:34] E/launcher - Error forwarding the new session empty pool of VM for setup capabilities [{count=1, browserName=chrome}]
Any help for what I am doing wrong is appreciated, thank you.
Is there a specific need to start a Selenium Server/hub?
If you dont have a plan to setup a Selenium GRID and you just need to run protractor test scripts stand-alone you can skip the part where you are starting server.
Can you leave the remove seleniumAddress option in Protractor.conf.js. This will enable Protractor to start its own selenium server & wait till server is completely up and execute and will shutdown the server once execution is done
More information here
/**
* The address of a running Selenium Server. If specified, Protractor will
* connect to an already running instance of Selenium. This usually looks like
* seleniumAddress: 'http://localhost:4444/wd/hub'
*/
seleniumAddress?: string;
just have the step #run protractor and rest is taken care by Protractor

Running headless firefox Xvfb with Jenkins to run selenium tests

I face with Error: no display specified error when running play framework tests in Jenkins at FreeBSD server.
So every time I face with timeout
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox
Jenkins has:
1) Xvfb plugin installed
2) Play Framework installed
Tests are written using selenide library and selenide module for play framework.
Xvfb configured and enabled in job configuration.
Job console output is:
Checking out Revision 3f485bd2e3dbcfa058fc19f89ab18020e36707d8 (origin/trunk)
...
Xvfb starting$ /usr/local/bin//Xvfb :1 -screen 0 -fbdir /usr/local/jenkins/xvfb-9-786185694297443042.fbdir
...
Command detected: clean
Command detected: deps --sync
Command detected: precompile
Command detected: auto-test
[YalsTests] $ /srv/java/play/play clean
...
~ using java version "1.8.0_72"
[YalsTests] $ /srv/java/play/play auto-test
~ 14 tests to run:
~
~ selenium/front/CorrectInput... org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: no display specified
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:113)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
Job configuration:
[X] Start Xvfb before the build, and shut it down after.
Xvfb specific display name 1
Xvfb display name offset 0
Invoke Play Framework
Command set Play 1.x
Goals
Clean project [clean]
Custom parameter
Custom command deps --sync
Precompile all Java sources and templates [precompile]
Automatically run all application tests [auto-test]
The selenium tasks needs to know the DISPLAY that it shall connect to.
You can set it e.g. as an environment variable (don't forget to export it, if you do that in .profile)
export DISPLAY=:10
This is for bash, other shells might need a 2 step process:
DISPLAY=:10
export DISPLAY
You can also specify the variable at the command line before the command:
DISPLAY=:10 java -jar mySelenium.jar
You could avoid all these issues by using Selenoid project which starts headless browsers in parallel in Docker containers. Container images are created by considering compatible version of webdriver and browser. They also include fonts, flashplayer and so on. Just choose one of already existing images and run your tests. No need to install Java to run Selenium tests.
I tend to supply this if I'm running my tests via Jenkins:
xvfb-run --server-args="-screen 0, 1920x1080x16" mvn clean install...
One thing that has tripped me up in the past is that while xvfb-run will create a virtual display, it can really screw up your screenshots and web interactions if it isn't sized correctly, so it's usually advisable to supply a resolution size which will suitably display your browser.

Selenium Server on startup

I'm using Selenium RC in a Ubuntu system.
I want to automate the tests, and I need to start Selenium-server.jar on startup of the machine.
I created seleniumServer.conf in /ect/init/ with:
start on startup
start on runlevel 3
respawn
exec xvfb-run java -jar /home/condde/selenium-server-1.0.3/selenium-server.jar -port 4444
When I reboot the machine, it works fine, the process is running.
But when I execute a test, the result is:
PHPUnit_Framework_Exception: Could not connect to the Selenium RC server.
Any ideas?
Thanks!
I have the same problem, my process can not connect the selenium server sometimes. After dig into debug log and selenium source code, I found that's because java's SecureRandom hangs if /dev/random hangs when selenium try generate random number. So I replace /dev/random with /dev/urandom, then selenium server works fine:
sudo mv /dev/random /dev/random.real
sudo ln -s /dev/urandom /dev/random
Or you can modify $JAVA_HOME/jre/lib/security/java.security file and changing the property:
securerandom.source=file:/dev/random
to:
securerandom.source=file:/dev/urandom
Maybe it works, but not for me.
Use -debug to start Selenium with debug log to see if any error.
java -jar selenium-server.jar -debug > /var/log/selenium-server.log 2>&1
I did this on ubuntu 14 using npm.
First, install the selenium-standalone via npm.
sudo npm install selenium-standalone -g
sudo selenium-standalone install
Then create a symbolic link in /etc/init.d, and configure it to run.
sudo ln -s /usr/local/bin/selenium-standalone /etc/init.d/
sudo update-rc.d selenium-standalone defaults
Another very simple and good solution is to install selenium via docker. I have used the chrome image and it's easy as:
sudo docker run -d -p 4444:4444 selenium/standalone-chrome
The -d option makes is a daemon that will be restarted every time you start your computer. The -p option forwards the webdriver port (4444) from the docker instance to the host.
Well, it's not phantomjs, but I like chrome better anyway. There is also a firefox image! Checkout https://github.com/SeleniumHQ/docker-selenium for more info.
I would start the selenium server process with -log parameter to get info from the process first and all and see if it actually get any kind of connections, errors etc..
A few ideas to troubleshoot:
Do you get any response if you enter http://localhost:4444
It should render a 403 error by the Jetty engine.
If this does not work I would try with your actual IP:4444, that might indicate problem with localhost variable, proxy settings etc..
Could the firewall settings be blocking the the 4444 port? Maybe the Selenium Server process is not allowed to start the browser.