How to setup webdiver to perform actions in background? - selenium

I am currently working in Automation QA. Our project using Java: jbehave, thucydides, selenium. We are working in Ubuntu OS.
Every time I run my tests, I have to stop doing everything(coding, browsing the web), because every click or any action of selenium is taking driver window(we are using chrome driver) in focus(makes window active) and if i do something i mess up the test. Our team is wasting a lot of time because of this.
We have tried Phantomjs, it doesn't work correctly, cannot find elements etc... And also to run tests in virtual machine using vagrant isn't working for us(for company specific reasons).
It is fine if Chrome driver will take over active window when it starts, as long as it would continue performing different actions in background. Strange, but in my previous project(windows, c#, mstest) chromedriver would behave exactly like this without any additional setup.
Please help, I know few other teams having this issue. Seems like this issue only on Mac and linux.

After having this issue for a long time I have finally found a good solution. This issue still remains on ChromeDriver level, so if there are any updates on this side let me know.
The easiest way to run tests in Ubuntu in a background is using VNC Server. Here are the steps you should follow:
Install vnc server from ubuntu terminal:
sudo apt-get install vnc4server
Start server with any number(I use 7). First time doing it enter password for vnc server(Remember it!).
vncserver :7
To start server in full screen add "geometry" to last command with your screen resolution for example:
vncserver :7 -geometry 1920x1080
Download some VNC Viewer. I use Real VNC.
Go to Real vnc viewer, start new connection and enter:
VNC Server: localhost:7
Encryption: Let VNC Server choose
Press connect and enter your vnc server password(the one from step 2).
New window should be opened, open new terminal in it.
Run your test from terminal. I use java maven project so for me it works like this. Navigate to project folder and run: mvn clean install
If you want simply to run test in hidden mode using only terminal(without opening vncviewer) use this command:
cd path/to/project
xterm -display localhost:7 -e mvn clean install
If you want to stop server:
vncserver -kill :7
Hope this will help many of you.

For me (I had the same problem in my Grails project's functional test using Chrome; not with Firefox) works this set up:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized --disable-popup-blocking");
new ChromeDriver(options);
Regards

Related

Selenium works when started locally, but not remotely

I'm hoping this is a simple issue to solve:
I have a dedicated machine that runs selenium with chromedriver. I can physically walk to the machine and type the following in a local terminal:
java -Dwebdriver.chrome.driver=chromedriver238 -jar selenium-server-3.11.jar
I can then go to any other machine on the network and kick off testing (in my case, codeception on jenkins) and it will sucessfully run my tests in chrome without a hitch.
However, if I try to run the above command without physically being at the selenium machine (ex, trying to ssh into the machine to run the command or making this command run as part of a supervisor process) then selenium has trouble starting the instance of chrome.
Has anyone run into this scenario before? Is there a way I can give 'interactive' access to the background script to allow it to open stuff on my screen?
Because when you physically walk to the machine and run the command. the selenium can get the desktop/window interface.
When use SSH, selenium lost the desktop/window interface, selenium have to start chrome under headless model, but your code not config to run as headless model. So the conflict come out.
you need to config your code to run with headless model.
I'm to getting the same scenario, when i try to start the selenium code on the machine A it works yet when i try to start selenium and run the scripts from machine B
(remotely connecting to machine A using ssh [ssh test#machine-a]) chrome instance doesn't start/launch and driver is getting null.
logs :
2020-02-27 12:04:27,319 INFO [LogWritter] Exception in beforeclass chrome driver instantiation , driver is null
2020-02-27 12:04:27,493 INFO [LogWritter] Exception in getting screenshot---java.awt.AWTException: headless environment
Any help/suggestion would be great !

Unable to run tests against Safari 11 remotely (ssh, CI)

I'm having troubles running Ruby/RSpec tests against Safari 11 when I'm trying to run tests via ssh manually or via Jenkins (where machine where Safari exists is remote slave).
When executing tests, I'm getting following error:
Selenium::WebDriver::Error::WebDriverError:
unable to connect to safaridriver 127.0.0.1:7050
What is weird is that I'm able to run tests when I'm logged to the Mac machine directly. This leads me to the conclusion that there could be some permission which, by default, disables execution from ssh session but not sure why?
Also, to my knowledge, Safari Driver is part of Safari 10+ and as such is not installed anymore as extension
Update: I've found out that safaridriver executable that should be spawned by tests cannot be spawned from some reason when I login via ssh.
Example:
/usr/bin/safaridriver -p 7050
It will just terminate with non-zero exit code while running same command directly on machine will run safaridriver in foreground. Since I expect Jenkins to be running test job on this machine, my idea to overcome this issue would be to launch safaridriver on this machine (by cron or launchctl) and then use this instance to connect to it with my tests. However, so far, I was not able to make my tests (Selenium/Capybara) re-use existing safaridriver instead of always trying to spawn new one on different port. Any idea on this would also be greatly appreciated.
This is my environment:
OS: MacOS Sierra 10.12.6
Browser: Safari 11.0.2
Thanks in advance
There is a possible workaround to do this. First you need to create a Automator Workflow or Apple Script that launches the SafariDriver
Then save this as a application. Let assume we name it SafariDriver7050.
Then from the SSH session you need to execute
open /Applications/SafariDriver7050.app
This will actually launch SafariDriver in the logged in session and it should work for you.
The caveat being shutting it down, you will need to first kill the SafariDriver7050 app and then you need to kill the safaridriver process. The order matters, else it will create a error dialog on UI
Edit-1:
As you suggested, it would be even easier to do this, when you wrap your test as a app and then it will be automatically be able to launch SafariDriver without any issues. The key to issue is using open command in a SSH session
Edit-2
Why does SafariDriver not work in SSH? Well if you look at the linux counterpart
In case of linux we can use the DISPLAY environment variable to launch an app in an existing display or we can use something like XVFB to launch the browser in a virtual display. That is the concept that most frameworks use in case of linux machines.
But Mac doesn't have such kind of feature, which is why this workaround is needed. Now why it doesn't have that, I am not sure. There may be some other workaround that I may not be aware of, so anyone who has valuable info, can help improve this part of the answer
For my case:
On OSX host side was created Automator app as described above and modified a little bit:
security unlock-keychain -p your_host_password /Users/$USER/Library/Keychains/login.keychain-db
safaridriver --enable
safaridriver -p 7050
On CCI/Jenkins side:
open /Applications/StartSafariDriver7050.app/
run pytest cmd
osascript -e 'quit app "StartSafariDriver7050"'
pkill safaridriver
In Python:
def safaridriver():
return webdriver.Safari(desired_capabilities=Caps.SAFARI, port=7050)

How can I run Chrome headless from protractor WITHOUT using a Selenium server

I have Chrome working headless via a Selenium server by running xvfb bound to DISPLAY:10 and then running Selenium with a DISPLAY=:10 webdriver-manager start. So that's all fine.
I also have Chromedriver running directly from protractor by specifying directConnect: true in my protractor.conf.
What I'd like to do is a combination of the two, ie. run chromedriver directly (NO SELENIUM!), but somehow instruct it to run headless on DISPLAY :10.
In terms of topology, everything is run on the developer's PC. There are no servers involved. In a comment below, I mention a node server: this is being used to launch protractor instead of the more usual launching from the command line. I want protractor to be running on DISPLAY:0 so I can see its output, test failures etc. When it spawns chrome using chromedriver, I want chrome to be running on DISPLAY:10. My reason for doing this is that I want the chrome window to be bigger than my physical screen. See How can I screenshot the full height of a mobile form factor? for background.
I'm guessing the topology is like this:
Box A: xvfb, DISPLAY=:10, Selenium Server, chromedriver
Box B: DISPLAY=:0, node, protractor (directConnect: true), chromedriver
Box C: test runner.
I don't know how else it could be laid out, given that DISPLAY is an environment variable, not a parameter to be passed.
In which case, assuming you don't want a separate xvfb installed on Box B, and A is reachable (plus a decent connection) from B, the solution is simply to set:
DISPLAY=boxAHost:10
on Box B.
If A isn't reachable from B, it might be simplest to just duplicate the xvfb setup on both A and B, and have DISPLAY=:10 on both.
I had a "Doh!!!" moment of zen and the whole thing is much easier that I thought it would be.
I had conflated the stdout of node/protractor (which I want on my screen) with the X display of chrome (which I want headless on DISPLAY:10). Of course they are totally different!!!!
Simply prefixing protractor with DISPLAY=:10 eg.
DISPLAY=:10 protractor /installation_test/conf-c-direct-noserver.js
or, in my case since I'm running a node server which in turn spawns protractor, ...
DISPLAY=:10 npm start
So protractor runs in my terminal and I can watch stdout, whereas the DISPLAY=:10 is inherited down through the call layers and is eventually seen and understood by Chrome.

what's the relationship between selenium2 and vncserver or xvfb

I have got the solutions which is using xvfb to specified a display to selenium, when I test my web program. Envviroments: CentOS6, Selenium2, robotframwork, Firefox. And My steps are:
startup vncserver specified displaynumber. vncserver :6
set DISPLAY with same number. DISPLAY=:6
test my project.
kill vncserver. vncserver -kill :6
And firefox can startup correctly. Now, I know what to do to startup firefox, but I dont know why. What's the relationship among selenium2, vncserver, xvfb, robotframework? For short, I want to know how selenium2 or robotframework use xserver from vncserver or xvfb. I did not find my answer in http://docs.seleniumhq.org/docs/ and https://github.com/rtomac/robotframework-selenium2library. Anyone knows that? Any references? thx
I am not an expert but I will try to give an explanation of how I understand it.
By default, your Centos has no display configured.
It means you can not launch any application that has graphical interface.
So you not launch Firefox.
xvfb and vncserver allow you to set-up such a display.
xvfb will create a in-memory display.
Then, "set DISPLAY" will tell your OS which display to use.
If you set it to the xvfb you launched, then you are able to launch graphical app on your centos but they will not show up as they are in memory.
Then, for the Robot/Selenium part, they are not aware of all that.
Your Robot Framework test will call Selenium library that will launch Firefox as you would launch it from the command line, so it will be launched on the xvfb display.
So to your question "how did selenium know that variable DISPLAY has already been set?": selenium does not need to know. He sends it to the current DISPLAY and it will be the one you configured.
Hope this helps you to understand.

How to use Selenium Grid to run some commands on the node and system commands locally

I'm trying to solve a problem and I was hoping I could do it with Selenium Grid but I'm not entirely sure that it's possible. Here's my problem...I'm developing test cases using Selenium WebDriver and I need my tests to run on a Windows machine however the AUT runs on a Linux server. I have several tools that only run on Linux and I would like to be able to run some commands/tests on the Windows machine and others in the Linux server. For instance
Test Starts -> Firefox launches (Windows machine)
Login to site (Windows machine)
Run command in Linux server
Return running commands/tests on the Windows box.
Alternatively I could figure out how to run those commands remotely from a Windows machine but I'm not looking forward to doing that.
Out of the box, you cannot use selenium to run commands on your local machine. Selenium/Webdriver is a browser testing tool and can interact only with browser.
Updated Answer based on your comments
Common part to both ways (which I think you already know)
In your java code you should have the code for webdriver/selenium commands that will do step 1 and 2. Your java code should also the contain the logic/code to execute the linux commands as step 3. Step 4 should again be your webdriver command.
This test code should run from the linux server so that the java command execution will happen in the linux box.
Now the only pending part is the execution of test in remote machine.
Method 1 without using grid
Start the selenium server in the windows machine. Point your java tests to the selenium server running in this machine. If your windows machine IP is 10.0.0.1, you should initialize the webdriver object as
WebDriver wd = new RemoteWebDriver(new URL("http://10.0.0.1:4444/wd/hub"),DesiredCapabilities.Firefox());
This will send all your webdriver commands to the selenium server running in 10.0.0.1 on port 4444. Your tests will then get executed in the windows machine. For this to work, your linux server and windows machine should be in same network.
Method 2 using grid.
Its all the same as above, but instead of giving the ip/dns name of windows machine, you should give the ip/dns name of the hub machine. You should also have windows machine registered as an RC to that hub.
I think with AutoIT kind of windows frameworks, we can invoke C:\Windows\System32\cmd.exe to open a command prompt and run some .bat files. If it is Linux /bin/csh or /bin/bash myscript.sh.