I am running some tests in headless firefox using Xvfb. However, after my tests are finished I want to move back to normal display. But I am not able to do that .Here is What I am doing .
Open A Terminal
sudo Xvfb :10 -ac &
export DISPLAY=:10
Execute My Tests using RobotFramework+ Selenium
After step 4, I want to open the firefox in the same terminal but I am not able to see it as it is directed towards :10 display.
I wonder how can I shut this (xvfb :10) down so that I can open firefox and view it.
the & on the end of your 2nd command tells Linux to run it command in background. You can see the list of background commands running with $ jobs. Sample on Ubuntu16.04:
$ sudo Xvfb :10 -ac &
[1] 31294
$ jobs -l
[1]+ 31294 Running sudo Xvfb :10 -ac &
As you can see on the output above, jobs -l shows the background job and the second column of the output is the PID that can be used to kill it as $ sudo kill 31294.
Another option that may be "cleaner" is to start Xvfb just to run the command you want and quit automatic instead of keep it running in background. This way you would replace your lines 2,3 and 4 by:
xvfb-run python my_selenium_tests.py
Just replace python my_selenium_tests.py with whatever way to run your tests. It will open Xvfb just to it and close at the end.
The simple solution is to keep the old value of DISPLAY, change it to point to the xvfb, and then after the test is run you can change it back to the saved value.
This leaves the xvfb running, so you should also get the pid of that process and kill it. Otherwise, if you run this set of steps a dozen times, you'll have a dozen xvfb processes running in the background.
Related
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.
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.
updated: added the missing docker attach.
Hi am trying to run a docker container, with -dti. but I cannot access with a terminal set to dumb. is there a way to change this (it is currently set to xterm, even though my ssh client is dumb)
example:
create the container
docker run -dti --name test -v /my-folder alpine /bin/ash
docker attach test
apk --update add nodejs
cd /my-folder
npm install -g gulp
the last command always contains ascii escape chars to move the cursor.
I have tried "export TERM=dumb" inside the running container, but it does not work.
is there a way to "run" this using the dumb terminal?
I am running this from a script on another computer, via (dumb) ssh.
using the -t which sets this https://docs.docker.com/engine/reference/run/#env-environment-variables, however removing effects the command prompt (the prompt is not shown)
possible solution 1 remove the -t and keep the -i. To see if the command has completed echo out a known token (ENDENDEND). ie
docker run -di --name test -v /my-folder alpine /bin/ash
docker attach test
apk --update add nodejs;echo ENDENDEND
cd /my-folder;echo ENDENDEND
npm install -g gulp;echo ENDENDEND
not pretty, but it works (there is no ascii in the results)
Possible solution 2 use the journal, docker can log out to the linux journal, this can be gathered as commands are executed in the container. (I have yet to fully test this one out. however the log seems to be a nicer output of what happened)
update:
Yep -t is the problem.
However if you want to see the entire process when running a command, maybe this way is better:
docker run -di --name test -v/my-folder alpine /bin/ash
docker exec -it test /bin/ash
finally you need to kill the container after all jobs finished.
docker run -d means "Run container in background and print container ID"
not start the container as a daemon
I was hitting this issue on OSx running docker, i had to do 2 things to stop the terminal/ascii/ansi escape sequences.
remove the "t" option on the docker run command (from docker run -it ... to docker run -i...)
ensure to force bash or sh shells used on osx when running the command from a script file, not the default zsh
Also
the escape sequences were not always visible on the terminal
even so, they still usually caused content corruption, even with SED brought to bear
they always were shown in my editor
On our build server (bamboo launched) we are wanting to do selenium tests, to do this we are running xvfb-run, this works on our local servers which are all of the same type.
If I log on to the build server and run:
xvfb-run echo 'i'
I get the error:
xvfb-run: error: Xvfb failed to start
I have tried running like this:
xvfb-run -a echo 'i'
This time it just hangs and never finishes, any ideas on things I can try?
Thanks
Run following commands:
sudo nohup Xvfb :40 -ac &
export DISPLAY=:40
Since it works locally, I suspect a server or permissions issue is going on. Perhaps your user can't open up a lock file in /tmp ? Try to get more info about the problem by running:
xvfb-run -e /dev/stdout [mycommand]
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.