Selenium Tests hang when attempting to be run with Bamboo - java

I'm attempting to use Bamboo's build and deployment capabilities to run Selenium Automated tests with my project.
We're currently using a Maven task to build and run regular JUNIT tests, and the plan is to use another Maven task to run the Selenium tests after the code has been successfully deployed to the server. At the moment, everything seems to run just fine locally, but when bamboo attempts to run the Selenium tests it seems to hang indefinitely. Unfortunately I don't have remote access to the server to watch it first hand, but I do know that it's a Microsoft server running with OS version: Windows 2012 R2 64-bit. I also know that the server is using java version "1.8.0_101", which is the same as my local setup. I've included a sample of the code I'm running below.
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumTestExample {
WebDriver driver;
#Before
public void setup(){
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer32bit.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(null, ieCapabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}
#Test
public void printPageTitle(){
System.out.println("Title of Page is: " + driver.getTitle());
driver.quit();
}
}
When run through Bamboo, the only output in the logs are the lines...
Started InternetExplorerDriver server (32-bit)
2.53.1.0
Listening on port 8080
Only local connections are allowed

CI or Bamboo server should be used for controlling your tests. You should not try to run your tests on a CI server. The issue you are having is probably because of that. Your are trying to use CI server as you local machine, which it will not respond the same way as your local. Instead you should use selenium grid in your setup to remotely connect to a machine by making use of hub and node. You may also have to use remote webdriver. Also have a look at this post

I've seen this using TeamCity, in this case the IE tells you that its executable will only accept connections from the local machine. According to Selenium org
The HTTP server started by the IEDriverServer.exe sets an access control list to only accept connections from the local machine, and disallows incoming connections from remote machines. At present, this cannot be changed without modifying the source code to the IEDriverServer.exe. To run the Internet Explorer driver on a remote machine, use the Java standalone remote server in connection with your language binding's equivalent of RemoteWebDriver.
So first run a chromedriver via passing through a param like so :
chromedriver --whitelisted-ips=""
This will basically whitelist all IP's, not always an ideal solution of course. But will show you that your tests can run on this CI configuration. Next thing to look for is your users privileges. Ask your admin to grant you more permissions in order to do your job. Keep in mind that IE's Protected mode may require some additional changes from your user. If none of this helps, consider Selenium grid with IE nodes.

Try to get rid of the line in the code:
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer32bit.exe");
First of all, it tells where selenium should look for the webdriver for IE. Since the Bamboo server is a windows machine, you have to set it up with the absolute path of the file, like "C:\test\webdriver\IEDriverServer32bit.exe".
Secondly, the property could be set using environment variables of the Bamboo task.
Thirdly, if you want to define it on the fly, you can define the property in pom.xml as:
<webdriver.ie.driver.path>
C:\test\webdriver\IEDriverServer32bit.exe
</webdriver.ie.driver.path>
and use it in a system property with help of maven-surefire-plugin.
then you can run test with the command
mvn test -Dwebdriver.ie.driver.path=C:\test\webdriver\IEDriverServer32bit.exe
with whatever path you want.

Related

Run whole selenium project in docker (Gradle + Selenium + java + junit + docker)

My current project is java selenium (with selenide framework) auto-tests with gradle and junit.
Now, I want to wrap my whole project to docker container, to make it possible to run it on other machines using only docker.
As I see it:
User run my docker image
Image have installed java + chrome + selenium + gradle
Project tests launched within container.
(optional) image shares test results outside image (or I can connect to container and take a look at them).
What am I suppose to do?
A saw a lot of tutorials about browsers in containers, selenoid, etc.(which is cool).
But I can't find a solution for my question.
Thanks!
Suggest to run tests as docker-compose multi-container application.
It will have 2 services in docker-compose as i see it:
browser - based on selenium Chrome browser image
tests - based on custom image extending java base image. Custom image Dockerfile should have gradle installed and tests jar file built in it.
Tests should drive Chrome browser using RemoteWebDriver initialized as below (note browser hostname where remote Chrome is listening).
public void createChromeDriverForRemote(){
WebDriver driver = new RemoteWebDriver("http://browser:4444/wd/hub", DesiredCapabilities.chrome());
}
See quick start here
What you need to do is:
Create a docker image that has Java, Chrome, selenium, gradle, junit, etc
Once you have the image, run it on your local on any port example: 4444
Switch to RemoteWebdriver
public static String remote_url_chrome = "http://localhost:4444/wd/hub";
ChromeOptions options = new ChromeOptions();
driver.set(new RemoteWebDriver(new URL(remote_url_chrome), options));
Run the test now

Selenium code is not opening firefox browser when running selenium code in Jenkins server as Job

I have the below code in Selenium/Java test class. Now, this code I have pushed to GitHub.
Also, I have set up the Jenkins job to execute the same code (in the Jenkins job I have pointed the code to GitHub).
The Jenkins job is triggering fine and started executing the test, but throwing below error while opening the browser.
The test case is supposed to open the Firefox browser, but the Firefox browsing is not opening.
So, my question is, whether the below selenium code is correct if I want to execute the test case in Jenkins job (Jenkins server is running in Cento7.4 OS).
NOTE: In the same CentOS VM, I am able to execute the same (below) selenium code in eclipse and it's able to open the Firefox browser and open the URL without any issues.
The issue is coming only if I try to run the same code in the Jenkins server as a Jenkins job.
Selenium code
System.setProperty("webdriver.gecko.driver", "geckodriver");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--display=0");
WebDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("https://www.facebook.com");
Jenkins job output
Running TestSuite
Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
1597912923234 mozrunner::runner INFO Running command: "/bin/firefox" "-marionette" "--display=0" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileFz0Zr2"
Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Running without a11y support!
Error: cannot open display: 0
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.972 sec <<< FAILURE!
Results :
Failed tests: loginTest4(com.training.browsers.LinuxTest): invalid argument: can't kill an exited process
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[ERROR] There are test failures.
xauth list output
[root#localhost ~]# xauth list
localhost.localdomain/unix:0 MIT-MAGIC-COOKIE-1 4eb74af687f2dbc022ef03617614456e
#ffff#6c6f63616c686f73742e6c6f63616c646f6d61696e#:0 MIT-MAGIC-COOKIE-1 4eb74af687f2dbc022ef03617614456e
You may want to look into setting up xvfb (https://centos.pkgs.org/7/centos-x86_64/xorg-x11-server-Xvfb-1.20.4-10.el7.x86_64.rpm.html). The problem is that your Jenkins server cannot open display 0 to run in. Notice the parameters being sent in to the firefox binary specifying display 0 in your firefoxOptions match the INFO log line for the binary execution. Assuming that you are running a headless server and this is why you get this error. The same is not the case when running locally.
With xvfb you should be able to specify a screen number and set your configurations accordingly or simply use xvfb-run.
The test case is supposed to open the Firefox browser, but the Firefox browsing is not opening.
To resolve this issue, use WebDriverManager to automate the management of the drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.
To use WebDriverManager in a Maven project, add the following dependency in your pom.xml (Java 8 or upper required).
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.2.2</version>
</dependency>
Then simply add WebDriverManager.firefoxdriver().setup(); in your code as shown below:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.firefoxdriver().setup();
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--display=0");
WebDriver driver = new FirefoxDriver(options);
See the basic examples of running JUnit 4 tests on Firefox and Chrome using WebDriverManager here.
--display=n
The phrase display is used to refer to collection of monitors that share a common keyboard and pointer e.g. mouse, tablet, etc. Most workstations tend to only have one keyboard, and therefore, only one display. Multi-user systems, frequently have several displays and each display on a machine is assigned a display number (beginning at 0) when the X server for that display is started. display:0 is usually the local display i.e. the main display of the computer.
Using Jenkins
When Jenkins executes the batch file, Jenkins slave runs as service in the background for every program it initiates. So normally you won't be able to visualize the Firefox browser spinning up but in the task manager you can see Jenkins opens several Firefox processes in background.
Solution
There are a couple of approaches to address this issue as follows:
If you are using Jenkins as a windows service you need to Allow service to interact with desktop. Steps:
In windows service select the service of Jenkins:
Open properties window of the Jenkins service -> Logon-> enable the checkbox Allow service to interact with desktop
In this approach autolaunch of dbus-daemon works when under an X11 session, else it is disabled because there's no way for different applications to establish a common instance of the dbus daemon.
You can find a relevant detailed discussion in Jenkins : Selenium GUI tests are not visible on Windows
The other approach would be to run Jenkins from command prompt as java -jar jenkins.war instead of the windows installer version.
Another approach will be to use RemoteWebDriver. From Jenkins, make sure there is a machine where the selenium tests can run. On this server you spin up the firefox browser.
You can find a relevant detailed discussion in How to launch chrome browser from Jenkins directly instead of using code in eclipse
An alternative would be to invoke firefox-headless browser by setting setHeadless as true through an instance of FirefoxOptions() class as follows:
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com/");
You can find a relevant detailed discussion in How to make Firefox headless programmatically in Selenium with Python?
invalid argument: can't kill an exited process
This error is commonly observed in the two following cases:
When you are executing your tests as a root/admin user. It is recommended to execute your tests as a non-root/non-admin user.
When there is an incompatibility between the version of the binaries you are using.
You can find a detailed discussion in WebDriverException: Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium and Python on RaspberryPi3

How do I specify the Selenium Hub URL when running Serenity tests from Eclipse?

I'm trying to use the Serenity BDD testing framework with JUnit, instead of using Selenium directly, but I can't figure out how give the Serenity-managed WebDriver instance the URL of my Selenium Hub in a way that works for running tests from Eclipse (with "Run As > JUnit Test").
Using #Managed with driver="remote" correctly tries to create a org.openqa.selenium.remote.RemoteWebDriver instance...
#RunWith(SerenityRunner.class)
public class SerenityIT {
#Managed(driver="remote") WebDriver browser;
//[...]
}
... but this fails with
Caused by: java.lang.NullPointerException: null at
java.net.URL.(URL.java:532) ~[na:1.8.0_151]
which isn't surprising because there is no URL specified. So how should I pass the Selenium Hub URL?
You can also configure it in serenity.conf file
So something like that;
# Remote
webdriver {
driver = remote
remote {
url="http://localhost:4445/wd/hub"
driver=chrome
}
}
It turns our Serenity also loads serenity.properties when running tests from Eclipse, even though this is neither documented nor implied. I wrote one and Serenity found it, which is confirmed by logs in the console:
DEBUG [net.thucydides.core.util.PropertiesFileLocalPreferences:115] -
LOADING LOCAL PROPERTIES FROM
/integration-testing/serenity.properties
Selenium Hub's URL can then be provided as documented with webdriver.remote.url. Below are the properties I'm currently using, with an example of passing Firefox preferences.
webdriver.driver=remote
webdriver.remote.driver=firefox
webdriver.remote.url=http://127.0.0.1:4444/wd/hub
webdriver.timeouts.implicitlywait=10000
firefox.preferences=devtools.jsonview.enabled=false

Browser FireFox is hidden when selenium webdriver tests run

I use TeamCity as Continuous Integration and Selenium Webdriver tests written in Java code.
When I launch my tests, they all seem to run (becaue in TC it shows the final passed/failed number of tests), but I don't see any FireFox browser on the TeamCity Agent machine, where they run.
I check the processes tab in task manager and can see the firefox.exe -foreground process started, but don't see any browser in UI
How can I enable browser to display when I log in with my_admin_user?
NOTE: the TC agent service is started with "Log on as: my_admin_user"
If TeamCity Agent run as service on machine it uses 'background' desktop to run FF (as I understood all services do the same). You need to start agent manually from cmd.
I have an simmilar issue, my tests fail on TC agent - they can't open url in chrome, but when I'm running them on local machine they work fine.
Enable and start interctive service.
Open Teamcity agent services and services Go to Properties-> Logon tab and select checkbox "Allow service to intect with Desktop"...
You will get a interctive seiver popup nexttime asking to allow to run on desktop.

Selenium webdriver error when trying to lunch tests on a remote machine

I'm trying to run my web app tests using selenium on iE on a remorte machine. My tests run successfully locally but when I try to run them on a remorte XP OS using IE8, I get this log error : org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Help please ! Thanks !
Possible causes are
Selenium Server Standalone is NOT started on the remote machine
Selenium Server Standalone is started BUT inside code wrong IP address is written
Follow the steps written below in order to get things work:
1) Download Selenium Server Standalone JAR file : http://selenium-release.storage.googleapis.com/2.40/selenium-server-standalone-2.40.0.jar in the remote machine (IP address of remote machine let say: 192.168.10.100)
2) From remote machine, run the following command to start Server Standalone:
java -jar selenium-server-standalone-2.40.0.jar
3) Ensure IEDriverServer path is also set in that machine. Information is avialble here: https://code.google.com/p/selenium/wiki/InternetExplorerDriver
4) After all the above setting done write following sample code to open IE in remote machine:
public class IERemoteWebDriver {
public static void main(String[] args) throws IOException {
// Assuming Remote machine IP address '192.168.10.100'
String remote_address = "http://192.168.10.100:4444/wd/hub";
URL remote_url = new URL(remote_address);
DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
WebDriver wbdv = new RemoteWebDriver(remote_url, dc);
wbdv.navigate().to("https://www.google.com/");
}
}
This should work!
One possibly issue could be that your IE8 browser does not have Protected Enabled.
Open a new browser and then go to Tools->Internet Options->Security,
make sure all the zones have Enable Protected Mode selected.

Categories