Launching firefox browser in selenium - java

I have added selenium jar files in eclipse mars .When I launched the firefox browser, I'm getting the following error
Exception in thread "main" java.lang.RuntimeException: Unable to find a free port
at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:67)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:326)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
at Mypackage.Myfirstprogram.main(Myfirstprogram.java:10)

Selenium standalone server uses port no 4444 by default. Make sure that this port is not being used by any other process. Kill any process that was running on that port number or use any other port number for selenium standalone server.

Related

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 Chrome webdriver SessionNotCreatedException

Using selenium & Chrome webdriver, I'm getting below exception on trying to launch. I have gone through lot of posts and tried all possible ways.
I'm using compatible chrome browser and chrome drivers versions -
Version 80.
Java - 1.8
Windows 10 os
The same test when i run on my personal computer, its working. But it's giving below error on my organization s machine. Also, no issues if I use edge drivers.
org.openqa.selenium.SessionNotCreatedException: session not created
disconnected: unable to send message to renderer
Any thoughts to solve this?
Check for chromedriver version in organization's machine. Please update your chromedriver with lastest version in organization machine. Issue will get resolve
The ChromeDriver Version you are using in your code might be different than the Chrome Browser installed in your system.
Check and download the same version of driver as of your browser version.
You need to use the matching ChromeDriver (matching MAX number) with respect to the installed google-chrome version installed in your system.
As an example if you are using the current chrome=88.0.4324.104 effectively you can use either of the following versions of ChromeDriver:
ChromeDriver 88.0.4324.27
ChromeDriver 88.0.4324.96
References
You can find a couple of relevant detailed discussions in:
SessionNotCreatedException: Message: session not created from disconnected: unable to connect to renderer with ChromeDriver 2.45 Chrome v71
WebDriverException: disconnected: unable to connect to renderer even on providing correct path of latest chromedriver
Automation Testing Error : org.openqa.selenium.WebDriverException: disconnected: unable to connect to renderer

selenium server, selenium client, on an UBUNTU GUI server

i have a VPS with ubuntu 14.04 LTS and with the desktop package installed, that mean I can launch firefox from a ssh -X session.
To make tests, I launched from my server the selenium standalone server jar (selenium-server-standalone-3.0.0-beta3.jar)
After launching it, in another ssh session, i just enter python commands :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
And after that, following the instructions from
http://selenium-python.readthedocs.io/getting-started.html#using-selenium-with-remote-webdriver, I enter :
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX)
After 45sec, i have lots of errors in both server window and client window.
Here is the main error :
Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: GDK_BACKEND does not match available displays
I saw some people with the same problem, but even with the latest java and selenium versions, i still got this issue.
Thank you in advance for your help
It seems you're trying selenium 3 with latest firefox version. To support latest firefox with selenium 3, need to download latest geckodriver executable from this link and extract it into you system at any location.
Now to run selenium-server-standalone-3.0.0-beta3.jar use below command :-
java -jar selenium-server-standalone-3.0.0-beta3.jar -Dwebdriver.gecko.driver = "path/to/downloaded geckodriver"
Now you need to set capability with marionette property to true to support latest firefox with selenium 3 as below :-
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
# Tell the Python bindings to use Marionette.
caps["marionette"] = True
driver = webdriver.Remote(command_executor = 'http://127.0.0.1:4444/wd/hub', desired_capabilities = caps)
Note :- For more information about marionette follow this Mozilla official page

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.

Trouble in launching of firefox browser using selenium for MAC OS

I am using Selenium to test a website in Java and trying to run it in Firefox on a MAC. But when I am trying to execute the code below
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.example.com/");
I am getting the following runtime exception
java.lang.RuntimeException: Could not start Selenium session: Failed to start new browser session: Browser not supported: /Users/sumitghosh/Desktop/*firefox3
(Did you forget to add a *?)
Supported browsers include:
*firefox
*mock
*firefoxproxy
*pifirefox
*chrome
*iexploreproxy
*iexplore
*firefox3
*safariproxy
*googlechrome
*konqueror
*firefox2
*safari
*piiexplore
*firefoxchrome
*opera
*iehta
*custom
I have also tried changing the browser to *googlechrome, but the same error was firing!
But when *safari was used it ran successfully.
Since I want the application to run on Windows and MAC also, I was trying for *firefox or *googlechrome to run, but both browsers are giving exceptions both on Windows and MAC!
I have only ever got firefox 3.X to work on MACOSX with selenium.
Try downloading and installing a 3.X version (I got 3.18 to work).
Instead of "*firefox" you can try "*firefox /Apps/Firefox/firefox.exe" or any other absolute path to the file firefox.exe which works for your computer.

Categories