Using Selenium IDE's getEval command with FirefoxWebdriver - java

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

Related

Edge in IE mode selenium 4 with testNG and Java

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

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

How do I specify the Selenium Hub URL when running Serenity tests from Eclipse?

I'm trying to use the Serenity BDD testing framework with JUnit, instead of using Selenium directly, but I can't figure out how give the Serenity-managed WebDriver instance the URL of my Selenium Hub in a way that works for running tests from Eclipse (with "Run As > JUnit Test").
Using #Managed with driver="remote" correctly tries to create a org.openqa.selenium.remote.RemoteWebDriver instance...
#RunWith(SerenityRunner.class)
public class SerenityIT {
#Managed(driver="remote") WebDriver browser;
//[...]
}
... but this fails with
Caused by: java.lang.NullPointerException: null at
java.net.URL.(URL.java:532) ~[na:1.8.0_151]
which isn't surprising because there is no URL specified. So how should I pass the Selenium Hub URL?
You can also configure it in serenity.conf file
So something like that;
# Remote
webdriver {
driver = remote
remote {
url="http://localhost:4445/wd/hub"
driver=chrome
}
}
It turns our Serenity also loads serenity.properties when running tests from Eclipse, even though this is neither documented nor implied. I wrote one and Serenity found it, which is confirmed by logs in the console:
DEBUG [net.thucydides.core.util.PropertiesFileLocalPreferences:115] -
LOADING LOCAL PROPERTIES FROM
/integration-testing/serenity.properties
Selenium Hub's URL can then be provided as documented with webdriver.remote.url. Below are the properties I'm currently using, with an example of passing Firefox preferences.
webdriver.driver=remote
webdriver.remote.driver=firefox
webdriver.remote.url=http://127.0.0.1:4444/wd/hub
webdriver.timeouts.implicitlywait=10000
firefox.preferences=devtools.jsonview.enabled=false

Selenium, Java, Maven, PhantomJS + TeamCity

I've got a project in Selenium/Java with tests that normally run on chromedriver.exe (I work on Windows) - no problems at all.
I have recently decided to add the project to our TeamCity that runs on Linux and since it cannot run tests on a browser, I want to switch to PhantomJS driver.
I've tried multiple variations of all the solutions I've found online and still can't get it working.
Currently I've got the phantomjs binary file in my resources, and I get the driver like this in an enum class:
PHANTOMJS {
#Override
public WebDriver initNewDriver() {
System.setProperty("phantomjs.binary.path", ClassLoader.getSystemResource(RELATIVE_PATH_TO_FILE_IN_RESOURCES).getFile());
return new PhantomJSDriver();
}
Then in the test class I start with driver.get(SOME_URL) - which is the furthest it ever got.
No matter what I do, when I tried to run it on TeamCity through 'clean test', I get something like this:
[userLoginTest] java.lang.IllegalStateException: The driver executable does not exist: /opt/buildAgent1/work/c4641c7cfd3331f7/web/drivers/phantomjslinux/phantomjs
[14:41:41]
[userLoginTest] java.lang.IllegalStateException: The driver executable does not exist: /opt/buildAgent1/work/c4641c7cfd3331f7/web/drivers/phantomjslinux/phantomjs
at com.google.common.base.Preconditions.checkState(Preconditions.java:518)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:123)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:254)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:190)
at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:104)
at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:94)
at test.core.base.SeleniumDriver$3.initNewDriver(SeleniumDriver.java:47)
at test.core.base.TestBase.(TestBase.java:25)
I don't really understand TeamCity well and it's difficult to find solutions that are applicable to my project and that I actually understand. So any help is welcome. Thanks :)
Solved by running npm install phantomjs on the server and using PhantomJS from there.

How do I run Firebug within Selenium WebDriver (Selenium 2)?

What's the best way to activate Firebug in Firefox when running Selenium 2?
Edit: Ok, I realize "best" is open to interpretation, but the profile-based solution really used to be a pain with selenium 1.0. So any alternative is considered better until proved worse ;)
You can create your profile in code and dynamically add required add-ons. Let's assume that you saved Firebug XPI into the C:\FF_Profile folder as firebug.xpi (go to Firebug download page, right-click on the "Add To Firefox" and save as C:\FF_Profile\firebug.xpi).
In code:
final String firebugPath = "C:\\FF_Profile\\firebug.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
// Add more if needed
WebDriver driver = new FirefoxDriver(profile);
This is described in WebDriver FAQ
Do you mean having firebug installed in the browser instance that webdriver launches? If so, you can pass an extension when you instantiate the driver, but the eaisest way is to create a firefox profile with firebug installed and then use the following code before you instantiate the driver:
System.setProperty("webdriver.firefox.profile", "NAME_OF_FIREFOX_PROFILE_WITH_FIREBUG");
Just reference your profile by name. Example in Ruby:
#driver = Selenium::WebDriver.for :firefox, :profile => "default"
Then, load Firefox normally, and add your desired extensions. They will now show up in your Selenium test runs.
Apparently the way the firefox-profile options are consumed has changed in Selenium WebDriver.
The old commandline (Selenium RC):
java -jar selenium-2.28.0.jar -firefoxProfileTemplate ~/.mozilla/firefox/3knu5vz0.selenium
Updated for WebDriver: (note that it wants the profile name rather than the directory)
java -jar selenium-2.28.0.jar -Dwebdriver.firefox.profile=selenium
modify your firefox location to something like
C:\Users\user-name\AppData\Roaming\Mozilla\Firefox\Profiles\sgmqi7hy.default
launch your firefox from selenium / webdriver
make all your required settings
close and restart firefox browser from selenium / webdriver
that's it, it solves your problem !!
I found a profiles.ini in ~/.mozialla/firefox/. In there was a profile named default, which I specified a like the following and then firefox was opened in test just like I opened it regularly (with all plugins etc).
java -jar selenium.jar -Dwebdriver.firefox.profile=default
If none of the above option works. Then try this.
1) Open terminal and type below command (close all existing firefox
sessions first)
firefox -p
2) This will open an option to create a new Firefox profile.
3) Create a profile lets say "SELENIUM".
4) Once the firefox is open straight away install firebug or any
other plugins extension that you want. once done close the window.
5) Now load this new profile via selenium , use below java
statements.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
WebDriver driver = new FirefoxDriver(ffprofile);
6) Done. Enjoy.
I have observed that the firebug is adding to browser and it is disabled by default and not enabled ,when i add firebug to firefox at runtime by using webdriver. So to make it enable we may need to add the below line to profile.
profile.setEnableNativeEvents(true);
Assuming that, Firebug is installed. Your objective is to run Firebug. Firebug can be run/execute by pressing F12 key. So Firebug can be run by following command of Selenium WebDriver with Java:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();

Categories