Selenium and Jenkins, test suite works in commandline not on jenkins - selenium

I've got a Selenium stand alone server, I run a simple test suite with a simple test case in command line and it works, here is the command line :
java -jar D:\POC\selenium-server-standalone-2.45.0.jar -Dhttp.proxyHost=XXXX -Dhttp.proxyPort=8080 -Dhttp.proxyUser=XXXX -Dhttp.proxyPassword=XXXX -htmlSuite *firefox http://XXXXX D:\Users\XXXX\Desktop\test_selenium.html D:\Users\XXXX\Desktop\result_selenium.html -firefoxProfileTemplate "C:\Users\XXXXX\AppData\Roaming\Mozilla\Firefox\Profiles\1yd4vpna.selenium" -debug
The same command is used by jenkins to run the test, here is the selenium result file :
info: Starting test /selenium-server/tests/test_case_selenium.html
info: Executing: |open | /index.php/fr/ | |
warn: currentTest.recordFailure: Timed out after 30000ms
info: Executing: |clickAndWait | css=li.item-175.first > a.subMenuBtn > span | |
error: Element css=li.item-175.first > a.subMenuBtn > span not found
warn: currentTest.recordFailure: Element css=li.item-175.first > a.subMenuBtn > span not found
When I read command line logs, Jenkins and commandline are different from firefox launch. On command line I can see a firefox poping, not with jenkins.
Without -debug, jenkins stops at "checking resource aliases".
I launched tomcat for jenkins as admin, still got my problem.
Any ideas?

From the paths being referenced, it looks like your Selenium server is in a Windows environment.
Since Selenium interacts with the desktop, make sure that the user account your Jenkins service is running under has the appropriate permissions to do that. (ie, run it as a named account and give that account access to the desktop via Computer Management / Services).
That would explain why you're seeing different behaviour running from the command line (as your user account) compared to in Jenkins (as the Jenkins service user account).

You can try js-code to fix this error. JS-redirect. As:
storeEval window.location="http://yourWebsiteToOpen.com"; js
echo ${js}
At least It helped for us.

Related

Selenium Tests running locally but failing on Azure DevOps Pipeline

I'm kind new to the DevOps world, and I'm having an issue where my test team can run their tests locally however some of them are failing when using a Pipeline.
The tests that are failing are upload files ones, however this only happens on a specific webpage of our solution.
Any idea of what can be happening?
Pipeline Steps:
We are using an Azure Agent (windows 2019)
1 - We install Google chrome webdrive (its then configured on the code)
2 - We run the following Maven script
mvn clean verify -P $(MavenProfiles) -e -Dcucumber.options="--tags $(Tags)" -
Dheadless=$(headless) -Denv=$(Environment) -Dlog4j.configurationFile=src/log4j2.xml
This is the error the the test throws:
java.lang.IllegalStateException: WebElement 'By.cssSelector: [id='Client_Name']' was not found on page. Exception: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for ui.controllers.GlobalUtilities$1#29a33620 (tried for 16 second(s) with 2000 milliseconds interval)
I noticed this error all through the pipeline logs, however only fail on specific tests.

Selenium+Jenkins+Chromedriver = WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally

I have problem with running my Selenium tests in Jenkins.
A result of execution is always:
WebDriverException: Message: unknown error: Chrome failed to start:
exited abnormally
My tests are written in Robot Framework and are using Chromium webdriver.
I'm setting needed paths in my command, which looks like this:
export PATH=$PATH:/usr/lib/chromium-browser; export PATH=$PATH:/usr/lib/chromium-browser/chromedriver; . /home/michal/robot_env/bin/activate; robot -L TRACE /home/michal/project_robot/tests
And when I run this command manually in terminal IT WORKS fine (Chromium starts automatically and the test goes on).
So the problem suppose to be in Jenkins. I have installed Xvfb plugin, but it didn't help.
Additionally, in /etc/init.d/jenkins I put these lines:
/usr/bin/X :0 vt7 -ac
export DISPLAY=:0
xhost +
And once again - nothing changed. What else should I set or check?
i got stuck same way.
The problem is that jenkins has it own user, called jenkins, and jenkins user cannot open the browser.
if you try to make "su jenkins" and then "chromium-browser" you obtain the display error.
That because you obtain this issue. The problem is not the webdriver, the problem is the user.
i removed the jenkins user created by jenkins and i createad a normal user called jenkins before installing jenkins.
then i installed jenkins.
now jenkins user can run the test (because it can open the browser) but jenkins itself will not load anymore.

How to configure Geckodriver for Seleniumhq plugin for Jenkins

I am currently working on configuring Jenkins to run selenium tests.
I am using Seleniumhq plugin and the Selenium HTML runner in the Selenium RC slot in the Jenkins configuration*.
In the particular job, I have a shell script running export PATH=$PATH:/path/to/geckodriver.exe to add it to the path followed by the SeleniumHQ htmlSuite Run step.
When it runs with this configuration, the job fails with the following errors message:
Started by timer
Building in workspace /var/lib/jenkins/jobs/MYJOB-selenium-ide-test/workspace
[workspace] $ /bin/sh -xe /tmp/hudson2672803749243149546.sh
+ export PATH=/var/lib/jenkins/tools/hudson.model.JDK/Oracle_8u45/bin:/var/lib/jenkins/tools/hudson.model.JDK/Oracle_8u45/bin:/var/lib/jenkins/.rvm/gems/ruby-2.2.2/bin:/var/lib/jenkins/.rvm/gems/ruby-2.2.2#global/bin:/var/lib/jenkins/.rvm/rubies/ruby-2.2.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/var/lib/jenkins/.rvm/bin:/path/to/geckodriver.exe
java -jar /var/lib/jenkins/users/me/selenium-html-runner-3.0.1.jar -htmlSuite *firefox example.com /var/lib/jenkins/jobs/MYJOB-selenium-ide-test/workspace/suite1/JenkinsTestSuite /var/lib/jenkins/jobs/MYJOB-selenium-ide-test/workspace/result.html
[workspace] $ java -jar /var/lib/jenkins/users/ME/selenium-html-runner-3.0.1.jar -htmlSuite *firefox example.com /var/lib/jenkins/jobs/myjob-selenium-ide-test/workspace/suite1/JenkinsTestSuite /var/lib/jenkins/jobs/MYJOB-selenium-ide-test/workspace/result.html
Multi-window mode is longer used as an option and will be ignored.
Dec 14, 2016 6:31:00 PM org.openqa.selenium.server.htmlrunner.HTMLLauncher mainInt
WARNING: Test of browser failed: *firefox
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:37)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:95)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:247)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:242)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:135)
at org.openqa.selenium.server.htmlrunner.HTMLLauncher.createDriver(HTMLLauncher.java:285)
at org.openqa.selenium.server.htmlrunner.HTMLLauncher.runHTMLSuite(HTMLLauncher.java:107)
at org.openqa.selenium.server.htmlrunner.HTMLLauncher.mainInt(HTMLLauncher.java:245)
at org.openqa.selenium.server.htmlrunner.HTMLLauncher.main(HTMLLauncher.java:273)
Publishing Selenium report...
ERROR: Test reports were found but none of them are new. Did tests run?
For example, /var/lib/jenkins/jobs/myjob-selenium-ide-test/workspace/suite1/results.html is 7 days 1 hr old
Build step 'Publish Selenium Report' changed build result to FAILURE
Finished: FAILURE
I can see that the issue is that the line System.setProperty("webdriver.gecko.driver","path/to/geckodriver.exe"); needs to be run. How do I properly configure this to happen? (I have tried to have it run as a Groovy build step, but this failed).
*(I have also attempted replacing it with an older version of the standalone server)

Jenkins & TestNG start browsers

Is it possible to make Jenkins use actual browser instead of headless browser? I a running some tests written in TestNG (using Selenium webdriver). When I run the testng.xml file in Eclipse, the browser starts and the tests run. But when I use Jenkins and run the tests with maven, it doesn't start any browsers.
If your jenkins is hosted in a Windows machine, there are some special configurations you should know about services that are allow to use the interface.
By the way, the easiest way to see the browsers running is starting jenkins using the command line:
java -jar jenkins.war
In linux you could use the same command or use xvfb plugin to run browsers in background.
Hope helps
In addition to this, the main reason for not launching the browser is JNLP (java network launch protocol) , when we execute the war we can interact with the desktop applications.
Using Selenium Grid will allow you to execute the test on Jenkins but open the browser on a remote slave.
To achieve this you need to create an instance RemoteWebdriver than ChromeDriver, IEDriver etc I
For linux. If jenkins is running as a daemon, you could specify active display to connect to and run your browser on it. Check what display you could connect to:
ps e | grep -Po " DISPLAY=[\.0-9A-Za-z:]* " | sort -u
My output is:
DISPLAY=:10.0
DISPLAY=:2
DISPLAY=:2.0
Then go to your jenkins project -> Configure -> Build and add the next string above your main build configuration through "Add build step -> Execute shell"
/bin/bash -c "export DISPLAY=:10"
Edited: I've encountered the issue again recently. To resolve it:
I've given for the jenkins' user ability to interact with the desktop of my current user:
xhost +si:localuser:jenkins
so if I connect to my linux system through ssh using jenkins' user credentials, export display of my current user (export DISPLAY=:10) and run, for example google-chrome or firefox inside of putty's terminal, they are launching on my current user's desktop.
After this I've checked If I could start "mvn test" command inside of workspace/MyTests folder from my putty so it will start browser and execute tests.
At the end I've created simple script in the root of my current user:
vi ~/.startup.sh
#!/bin/bash
xhost +si:localuser:jenkins
and added it to my Xfce4 GUI: Application -> Settings -> Session and Startup -> Application Autostart, specifying command field as:
sh -c $HOME/.startup.sh
It's because of this script should work only when desktop is loaded to share my current user's display with jenkins' user. After reboot and connecting to this server through RDC desktop loads with xhost command applied. And after this jenkins could interract with desktop even when I close the RDP connection but leaving current user's session alive.
I've removed Build step in my jenkins' project configuration that was stated "Invoke top-level Maven targets". It could not start my browsers.
I've changed my "Add build step -> Execute shell" to:
export DISPLAY=:10
cd /var/lib/jenkins/workspace/MyTests
mvn test
I've tried grid also, turning selenium-server-standalone -hub and -node into daemons. But it was slower than launching browsers with WebDriver in the such way.

Jenkins stops at Launching Internet Explorer

we are using Jenkins as CI in our project. We were running the CI from the command line using the following command
java -jar C:\\jenkins\\jenkins.war --httpPort=8085 --ajp13Port=8009
As the system needs to restart frequently, we change CI to start as a windows service.
Now we are facing the issues for Selenium test cases. Selenium test cases are not running after we make Jenkins as service. We are getting the following log and no more progress from that point
18:36:30,718 INFO [org.openqa.selenium.server.SeleniumDriverResourceHandler] Command request: getNewBrowserSession[*iexploreproxy, http://192.168.132.105:8080/, ] on session null
18:36:30,718 INFO [org.openqa.selenium.server.BrowserSessionFactory] creating new remote session
18:36:30,796 INFO [org.openqa.selenium.server.BrowserSessionFactory] Allocated session 80b95d0273ac4ea4a82860c79438f071 for http://192.168.132.105:8080/, launching...
18:36:30,796 INFO [org.openqa.selenium.server.browserlaunchers.WindowsProxyManager] Modifying registry settings...
18:36:31,781 INFO [org.openqa.selenium.server.browserlaunchers.InternetExplorerCustomProxyLauncher] Launching Internet Explorer...
Per hudson wiki, you should be running Hudson (or jenkins) as tasks rather than service for GUI testing. Check here. Look at the section GUI Testing in Windows
Following changes will resolve the problem
Update the selenium version.
Use *iexploreproxy or *piiexplore for IE instead of *iehta/*iexplore