WebDriver, ChromeDriver, Selenium, and Jenkins Integration - java

I have a selenium script written in eclipse, which invokes a ChromeDriver that launches a URL, reads title and closes the browser. I'm able to successfully execute this in Eclipse and also at a command prompt.
However, when I create a Jenkins job with a build routine (via execute batch command) the build keeps running for a long time. Upon tracing with sysout statements, I found that the build is stopping at drive.get()
driver.get("google.com");
The build fails with the error message below after waiting for a long time:
Starting ChromeDriver 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3) on port 39626
Only local connections are allowed.
[304.538][SEVERE]: Timed out receiving message from renderer: 298.714
[904.539][SEVERE]: Timed out receiving message from renderer: 600.000
[914.540][SEVERE]: Timed out receiving message from renderer: 10.000
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE

Selenium Webdriver actually needs display to work (X server to be accurate).
You can use Xvfb to run it in a headless mode, detailed instruction (a bit outdated, but still valid) - http://alex.nederlof.com/blog/2012/11/19/installing-selenium-with-jenkins-on-ubuntu/

You have to change the code to
driver.get("http://google.com");
you must have seen error running from eclipse and command prompt.

Related

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

Jenkins Cant Connect to Master

I have a setup up a master and slave on the same machine.
The master monitors External Jobs the slave runs a batch script and notifies the master about the same.
I followed the Windows instructions from:
https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs
and setup a buld step on the slave as follows:
set JENKINS_HOME=http://localhost:8080/jenkins/
java -jar C:\apache-tomcat-7.0.56\webapps\jenkins\WEB-INF\lib\jenkins-core-1.624.jar "POC_Main_Ext_Job" jenkins_poc_test_1
On building the slave job, I get the following error message in the console output:
Building remotely on MySlave in workspace D:\Temp\Jenkins\workspace\Slave_FreeStyle_1
[Slave_FreeStyle_1] $ cmd /c call C:\Users\ROHIT~1.BIS\AppData\Local\Temp\hudson853358493293228093.bat
D:\Temp\Jenkins\workspace\Slave_FreeStyle_1>set JENKINS_HOME=http://localhost:8080/jenkins/
D:\Temp\Jenkins\workspace\Slave_FreeStyle_1>java -jar C:\apache-tomcat-7.0.56\webapps\jenkins\WEB-INF\lib\jenkins-core-1.624.jar "POC_Main_Ext_Job" jenkins_poc_test_1
http://localhost:8080/jenkins/job/POC_Main_Ext_Job/ is not a valid external job (404 Not Found)
D:\Temp\Jenkins\workspace\Slave_FreeStyle_1>exit -1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
However, the above url does work in Jenkins and takes to the corresponding job. Any Idea what I'm doing wrong?
As you can see I had missed out the user credentials while setting JENKINS_HOME.
After adding them it works smoothly.

Metamap run local raise "Error when querying Prolog Server: Connection refused"

I follow this steps to run the Metamap Java API 2014 on Linux platform
(Main page from metamap)
After start Metamap server, I run some main class in Metamap JavaAPI pakages, but it raises Error when querying Prolog Server: Connection refused
Then I check if the Metamap server is running on port 8066 by using the command:
sudo netstat -tulpn | grep 8066
but I receive nothing.
Did anyone had the same problem before and knows solution for this?
After running skrmedpostctl (and the optional word sense diambiguation server wsdserverctl), you need to run the mmserver executable in order to use the Java API for Metamap. This can be run by running the command
./bin/mmserver{two-digit-year}
Also, be sure to include the two jar files for metamap and prolog-beans in your classpath (in your IDE, this can be done by adding a dependency to these jars).
The issue may reside with the timeout: when the timeout parameter is not specified for the MetaMapImpl, it is set to 0, so the instance interprets any latency as failure and reports an error.
Increase the timeout to a reasonable value (for me 500 msec was enough).
I was getting this error after getting error SPIO_E_TOO_MANY_OPEN_FILES and losing the connection to the mmserver. I was tryig to construct instances of MetaMapApiImpl in a loop and calling processCitationsFromString for thousands of times. Therefore, I was getting error SPIO_E_TOO_MANY_OPEN_FILES after around a hundred repeats.
What I did to fix this error was to simply disconnect the api at the end of the loop, by calling the function disconnect().

RSelenium UnknownError - java.lang.IllegalStateException with Google Chrome

I am running the following script based on the RSelenium Basics CRAN page:
library(RSelenium)
startServer(args = c("-port 4455"), log = FALSE, invisible = FALSE)
remDr <- remoteDriver(browserName = "chrome")
remDr$open()
This produces the following error:
Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.
at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:492)
at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:305)
at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:245)
at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:64)
Based on the comments from this conversation on GitHub, I've modified my startServer() command like so:
startServer(args = c("-port 4455"), log = FALSE, invisible = FALSE)
I then receive the following error in my console:
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: java.lang.IllegalStateException
And this error in the Java prompt that pops up:
14:38:55.098 INFO - Launching a standalone Selenium Server
14:38:55:161 INFO - Java: Oracle Corporation 25.40-b25
14:38:55.161 INFO - OS: Windows 7 6.1 amd64
14:38:55.161 INFO - v2.46.0, with Core v2.46.0. Built from revision 87c69e2
14:38:55.209 INFO - Driver class not found: com.opera.core.systems.OperaDriver
14:38:55.209 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
14:38:55:289 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4455/wd/hub
14:38:55:289 INFO - Selenium Server is up and running
I'm not sure if the lack of an Opera driver is an actual error or just a warning. Regardless, I would like to use Chrome, so it seems like it shouldn't matter. What am I doing wrong?
I was finally able to get RSelenium to work by piecing together information from a number of different sources. I think it would be helpful to have all of this information in one location, so here is the process that I went through to get RSelenium to work on Windows 7 (64-bit) with Chrome as the browser:
Download the 64-bit version of Java. I could not get anything to work with the standard download.
Download ChromeDriver.
Download the Selenium Standalone Server or run checkForServer() from R.
Create a batch file to start the Selenium server. I initially tried to use startServer() from an R script, but it would frequently get stuck and not carry on to the next line in the script. Here is the batch file that I created:
java -jar C:\path\to\selenium-server-standalone.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe
ChromeDriver can be put in the PATH environmental variable, but I decided to add in the path to ChromeDriver to the batch file (which accomplishes the same goal).
Run the R script. Here is my final script:
library(RSelenium)
shell.exec(paste0("C:\\path\\to\\yourbatchfile.bat"))
Sys.sleep(5)
remDr <- remoteDriver(browserName = "chrome")
remDr$open(silent = TRUE)
remDr$navigate("http://www.google.com")
The Sys.sleep() call was necessary because I would get an error in the remoteDriver() call if it ran before the Selenium Server had finished starting.
It is worth noting that RSelenium has some annoying differences for OSX. The invisible=T/silent=T arguments will not work when you run the yourcommand.command file and the remDr$open() method, respectively. The invisible=T will actually remind you that it only works on Windows. Not a huge deal (and if someone has a workaround I'd appreciate it).
For posterity's sake here's a slight variation for OSX to replace shell.exec using a .command file instead of a .bat with the same contents as above:
yourcommand.command file contents
java -jar /path/to/selenium-server-standalone.jar -Dwebdriver.chrome.driver=/path/to/chromedriver
R script modification
library(RSelenium)
system(paste("open","/path/to/yourcommand.command"))
Sys.sleep(5)
...

Categories