Browser FireFox is hidden when selenium webdriver tests run - java

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.

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

Appium IOS tests running when server started manually but not when server is started via AppiumDriverLocalService

Attempting to run Appium automation scripts on iOS (simulator) on a Mac Mini (M1 chip, if that's relevant). When I run the tests, they work just fine when the Appium server is started manually (typing "appium" into the terminal and starting it that way). However, when I attempt to start the appium server programmatically, the application under test fails to launch, with the following error:
2021-02-17 03:41:27:256 [W3C] WebDriverAgentRunner-Runner.app (19077) encountered an error (Failed to load the test bundle. If you believe this error represents a bug, please attach the result bundle at /Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Logs/Test/Test-WebDriverAgentRunner-2021.02.16_22-41-22--0500.xcresult. (Underlying Error: **The bundle “WebDriverAgentRunner” couldn’t be loaded because it doesn’t contain a version for the current architecture. The bundle doesn’t contain a version for the current architecture. Try installing a universal version of the bundle.** dlopen_preflight(/Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Build/Products/Debug-iphonesimulator/WebDriverAgentRunner-Runner.app/PlugIns/WebDriverAgentRunner.xctest/WebDriverAgentRunner): no suitable image found. Did find:
2021-02-17 03:41:27:256 [W3C] /Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Build/Products/Debug-iphonesimulator/WebDriverAgentRunner-Runner.app/PlugIns/WebDriverAgentRunner.xctest/WebDriverAgentRunner: mach-o, but wrong architecture))
Here's the full appium log: https://gist.githubusercontent.com/fida10/44344b223874310cf296d38a95d4268f/raw/316855b619129680eeaa6519a446a436d0699cd6/failedLog.txt
I originally thought that this was an issue with xcode or WDA, but if that were the case, the tests would fail no matter how Appium was started, and as mentioned previously, the tests pass perfectly fine when Appium is started manually (via terminal), so it might be an issue with the PATH or environment variables upon execution from Java, not sure though.
Here is the code I am using to start the server programmatically:
HashMap<String, String> environment = new HashMap();
environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
AppiumDriverLocalService server = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder()
.withEnvironment(environment)
.usingDriverExecutable(new File("//opt/homebrew/Cellar/node/15.8.0/bin/node"))
.withAppiumJS(new File("//Users/sagolGoru20/.npm-packages/lib/node_modules/appium/build/lib/main.js"))
.usingAnyFreePort()
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File("//Users/sagolGoru20/Programming/JavaProjects/MavenProjects/MobileAutomationProjects/firstAppiumProject/appiumLog.txt"))
);
server.start();
As the appium log shows, the server seems to be started fine but then throws the above error, so I think the issue may be with how I'm building the AppiumDriverLocalService object.
I followed this tutorial in building AppiumDriverLocalService: https://appiumpro.com/editions/71-starting-an-appium-server-programmatically-using-appiumservicebuilder
Here is the project code. It's a simple project, I'm just clicking on an "Allow" button (line 26): https://gist.github.com/fida10/bec187a516fc32f907f97725263a7206
When I comment out the AppiumDriverLocalService server object (lines 46 to 58) and instead launch by uncommenting line 60, the test runs properly.
Any help would be greatly appreciated.
Was able to solve it by setting all options under "Build Active Architecture only" to "no", in XCode build settings. Details: https://github.com/appium/java-client/issues/1444#issuecomment-781078298
Hopefully this helps someone trying to run XCUITests on Mac devices with the new M1 chip.

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

Selenium Tests hang when attempting to be run with Bamboo

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.

GWT JUnit Test - Is it possible to ignore same-origin policy

I am trying to run an asynchronous test and need to send a request to my server, which has a REST-API. So my problem is that the tests are executed on the local machine, but this obviously violates the same origin policy and I get a RuntimeError if I try to run the GWTtestCase:
com.google.gwt.http.client.RequestPermissionException: The URL http://<url-to-my-rest-service> is invalid or violates the same-origin security restriction
at com.google.gwt.http.client.RequestBuilder.doSend(RequestBuilder.java:394)
at com.google.gwt.http.client.RequestBuilder.send(RequestBuilder.java:242)
...
I know that I can run these tests manually, but with the latest updates of my browser (chromium), the GWT-Plugin was removed. So I don't know how to launch the tests in SuperDevMode.
Questions:
Is there a way to ignore the same-origin policy for the GWT JUnit Tests?
Can I change the browser which is used for the test to Chrome/Chromium?
How can I manually run the tests with SuperDevMode in a browser?
Edit:
If I want to run the tests manually I write the following into the terminal:
mvn gwt:test -Dgwt.args="-prod -userAgents safari -runStyle Manual:1"
Maven then will print an URL to the terminal which I should enter into the browser to execute the tests:
http://127.0.1.1:57818/<my-package>.JUnit/junit-standards.html?gwt.codesvr=127.0.1.1:40387
But if I enter this URL into my browser (Chromium) it will notify me that the GWT-Plugin is currently not installed. If I remove the part behind the question mark (like I do if I want to run the GWT-Code in SuperDevMode on the localhost) ?gwt.codesvr=127.0.1.1:40387 then the browser will show only a blank page, and also if I started the codeserver with:
mvn process-classes gwt:run-codeserver
the codeserver-bookmarks (Dev Mode On) are not working and GWT is not able to detect a module which is "compilable".
Question:
So how excatly do I compile the JUnit-Test-Module with the SuperDevMode?
You can't bypass the SOP, but you could use a servlet to proxy requests to your remote server (servlets declared with <servlet> in your gwt.xml files are honored by JUnitShell).
And if you want to run the tests in Chrome/Chromium, you can either use -runStyle Manual or Selenium (note: there's even a custom run style for PhantomJS), and because DevMode no longer works, as you noted, you'd have to run tests in prod mode: -prod -userAgents safari

Categories