I am trying to upgrade to selenium 4 to make the tests work in Edge in IE mode. These are Java tests using testNG. The tests work fine when using selenium 3 but fail to launch the browser when using selenium 4. Below is the command used to run the test:
$ java -classpath ".\out\production\baseFramework;.\lib\*;.\bin\*" org.testng.TestNG /LoginToHomePage.xml
Below is my code to launch the browser:
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.attachToEdgeChrome();
ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
ieOptions.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
ieOptions.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
ieOptions.setCapability(InternetExplorerDriver.ENABLE_ELEMENT_CACHE_CLEANUP, true);
ieOptions.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
ieOptions.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
WebDriver driver = new InternetExplorerDriver(ieOptions);
driver.get(url);
I have tried with no options and with a combination of all above options but none of them seem to work. There is no other error displayed other than one below line in stack trace making it difficult to debug.
*org.openqa.selenium.ie.InternetExplorerDriver.<init>(Lorg/openqa/selenium/ie/InternetExplorerOptions;)V*
Java : 1.8,
testNG : 6.14,
selenium : 4.1.2,
Edge : 99, also tried 97
Works fine from IDE and also works from command line when using selenium 3, but doesn't work from command line if selenium 4 is used. Weirdly, if i expand the command to mention each jar individually under lib, then Edge launches fine but has a problem when i use the wild card lib*. There are more than 80 jars, so running the command with expanded version is not what i prefer. I have tried to turn on debugging in IE driver options but no other logs are printed. I am not sure if its a Java issue or Selenium issue or something else.
All I want to do it to launch Edge browser and cant seem to make it work. Any idea what more I can try would be appreciated. Thanks
Related
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 can I launch Firefox 52 with opened devtools using selenium and Java?
Before the merge of Firebug into the Firefox DevTools I used FirefoxProfile to open the console programmatically:
profile.setPreference("extensions.firebug.console.enableSites", true);
etc.
But Firebug does not work anymore now.
So what is the proper way to launch with opened network or console tab?
And also, is there any analog extension to FirePath to launch automatically and use instead of the currently broken FirePath extension?
I managed to launch Firefox with given page and native devtools open from command line using -devtools parameter:
firefox -no-remote -profile "c:\deleteme" -devtools -url "http://example.com/"
It seems to be possible to pass that parameter to the WebDriver by calling
addCommandLineOptions("-devtools") on the FirefoxBinary instance.
(Via How can I tell selenium to start firefox with certain commandline options?.)
Or in Node perhaps by firefox.Options().setBinary(…).addArguments("-devtools")
Spotted the parameter in firefox -help | more, but alas, seems that not all information presented there as well as info given at Command_Line_Options MDC page are still valid. The -devtools one is missing at the MDN page at this moment.
action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build() works fine on windows but doesn't work on linux. I am writing a testcase using selenium webdriver and I am trying to select all the text which is written in a rich textbox and have used this code snippet to perform the select all command.It works fine on windows on firefox 38 browser but when I run my testcases on jenkins machine which is a linux machine,browser is firefox(don't know the exact version but above version 33) this code snippet doesn't work.I have tried some alternatives like driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "a")); and double click on rich textbox to select the text written in it but nothing works.What could be the reason,why these code snippets are not working on linux machine.
I have found the answer to my own question action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build() and driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "a"));was not running on Linux machine Firefox browser because the native events were not enabled in my driver factory.To enable Native Events you need to write the following code in the driver factory i.e where the required driver is created
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
FirefoxDriver driver = new FirefoxDriver(profile);
Hello,
I have a working Selenium IDE script that is working using this code:
getEval | this.page().findElement('xpath=//html/body/div[18]/div[9]/div[2]/div/div[9]/div[2]/center/div/div/div[2]/div/span/span/a').removeAttribute('target')
I then exported the entire test case as a Junit test case. The command for the getEval was not included with the export. I tried the following code:
if (driver instanceof JavascriptExecutor) {
js = (JavascriptExecutor)driver;
}
js.executeScript("this.page().findElement('xpath=//html/body/div[18]/div[9]/div[2]/div/div[9]/div[2]/center/div/div/div[2]/div/span/span/a').removeAttribute('target');");
When running this line of code, I get and exception: page is not defined
I have also tried:
sel = new WebDriverBackedSelenium(driver, driver.getCurrentUrl());
sel.getEval("page().findElement('xpath=//html/body/div[18]/div[9]/div[2]/div/div[9]/div[2]/center/div/div/div[2]/div/span/span/a').removeAttribute('target')");
The Junit test crashes when this line in ran, and the test case fails. I was wondering if I am sending the correct javascript script or if there is another, better, way of doing such a Selenium command through java.
I am using java 6 and Selenium 2.33.0
Why don't you try selenium webdriver API or selenium 2.0 ? It is a better way if you want to write more complicated tests. You can see some examples here: http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations
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.