Issue installing extension with Selenium remote Firefox webdriver in Saucelabs - java

Issue
Trying to install a Firefox browser extension during remote execution of Selenium tests on Saucelabs. When executing the tests locally, the extension is installed and active in Firefox, but in remote execution on Saucelabs the extension does not appear in the list of installed extensions. Following the steps outlined in this Saucelabs support article.
Setup
Selenium.Support v2.48.2 or v2.49.0
Selenium.WebDriver v2.48.2 or v2.49.0
Windows 10 or 7
Firefox 43
C# test setup
private static FirefoxProfile CreateFirefoxProfile()
{
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension("Tools/modify_headers-0.7.1.1-fx.xpi");
profile.SetPreference("general.useragent.override", "UA-STRING");
profile.SetPreference("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
profile.SetPreference("modifyheaders.headers.count", 1);
profile.SetPreference("modifyheaders.headers.action0", "Add");
profile.SetPreference("modifyheaders.headers.name0", "SampleHeader");
profile.SetPreference("modifyheaders.headers.value0", "test1234");
profile.SetPreference("modifyheaders.headers.enabled0", true);
profile.SetPreference("modifyheaders.config.active", true);
profile.SetPreference("modifyheaders.config.alwaysOn", true);
profile.SetPreference("modifyheaders.config.start", true);
return profile;
}
private static IWebDriver GetRemoteDriver()
{
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile);
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
capabilities.SetCapability(CapabilityType.Version,"");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}
Firefox settings
When looking at about:support in Firefox during local execution and opening the user.js file, it includes the following extension setup which matches the web driver configuration. Inspecting user.js on the Saucelabs remote instance does not include that. Here's a paste bin of the contents of the remote user.js file.
user_pref("general.useragent.override", "UA-STRING");
user_pref("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
user_pref("modifyheaders.headers.count", 1);
user_pref("modifyheaders.headers.action0", "Add");
user_pref("modifyheaders.headers.name0", "SampleHeader");
user_pref("modifyheaders.headers.value0", "test1234");
user_pref("modifyheaders.headers.enabled0", true);
user_pref("modifyheaders.config.active", true);
user_pref("modifyheaders.config.alwaysOn", true);
user_pref("modifyheaders.config.start", true);
I've also tried referencing an external version of the xpi with same result.
https://addons.mozilla.org/firefox/downloads/latest/967/addon-967-latest.xpi

Posted a bug report to SeleniumHQ and received this response, which fixed the above code.
In the RemoteWebDriver case for .NET, you need to use the
ToBase64String() method. This should resolve the issue. Note that this
is one of the reasons that other drivers have type-safe options
classes instead of passing raw capabilities. Future versions of the
.NET bindings should extend this pattern to Firefox as well, removing
this as an issue in the future.
The GetRemoteDriver method from above should be updated to this.
private static IWebDriver GetRemoteDriver()
{
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
// Note the change here, calling .ToBase64String()
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
capabilities.SetCapability(CapabilityType.Version,"");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}
After seeing what the fix is, I was able to find additional resources that mentioned this change.
https://stackoverflow.com/a/14285902/276681
https://code.google.com/p/selenium/issues/detail?id=2696#c4

Related

selenium load chrome profile java

I tried loading the chrome profile with selenium. However, whenever I upload the profile, I get an error:
invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use - user-data-dir.
String chromeProfile = "C:\\Users\\ad\\AppData\\Local\\Google\\Chrome\\User Data";
ChromeDriverService chSvc = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("C:\\Driver\\chromedriver.exe")).usingAnyFreePort().build();
ChromeOptions chOption = new ChromeOptions();
chOption.addArguments("--user-data-dir=" + chromeProfile);
chOption.addArguments("--profile-directory=Profile 33");
chOption.addArguments("--start-maximized");
ChromeDriver driver = new ChromeDriver(chSvc, chOption);
driver.get("https://google.com");
You cannot run multiple instance of ChromeDriver with the same user-data-dir. What you can do is every time you create a ChromeDriver instance, create a temp directory then set it in ChromeOptions chOption.addArguments("--user-data-dir=" + tempDir);

Appium tests are not performed on my Android device

I am learning automation testing with Appium(Lastest 1.8.2)-Mobile Automation Testing from Scratch course on Udemy.
I am trying with real and virtual devices and I can launch the application, but my operation(.click) is not working.
I am working on IntelliJ IDEA Community 2019.2.
Appium version: v1.14.1
I created Java Project with Maven Module
JAR:
commons-lang3-3.0, client-combined-3.141.59, java-client, selenium-java
My code:
BASE CLASS:
public class base {
public static AndroidDriver Capabilities() throws MalformedURLException {
File f = new File("src");
File fs = new File(f, "ApiDemos-debug.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Galaxy Tab S2");
cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,"UiAutomator2");
cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
return driver;
}
}
BASICS CLASS:
public class basics extends base {
public static void main(String[] args) throws MalformedURLException {
AndroidDriver<AndroidElement> driver = Capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[#text='Preference']")).click();
driver.findElementByXPath("//android.widget.TextView[#text='3. Preference dependencies']").click();
driver.findElementById("android:id/checkbox").click();
}
}
The problem is that I am receiving (probably) good exit code in Appium:
Got response with status 200
But I cannot see that test is performed on my device with Android.
Did I omit something?
You don't need that many dependencies, it is quite enough to have io.appium.java-client only, Appium Java Client includes Selenium libraries and guaranteed to work only with the version which it's linked with
So
First of all try to remove all the dependencies apart from the Appium Java Client.
Explicitly set up the following Desired Capabilities:
MobileCapabilityType.PLATFORM_NAME with the value of android
AndroidMobileCapabilityType.APP_PACKAGE with the value of your application package
AndroidMobileCapabilityType.APP_ACTIVITY with the value of your application activity
MobileCapabilityType.AUTOMATION_NAME should be uiautomator2
You can check out Appium -> Code Examples -> Java for comprehensive information and sample code repository which you can use as the "skeleton" for your test

Insecure Connection error Firefox v 53.0

I am using selenium 3.4.0 , firefox version 53.0 and gecko driver 0.16.1 , java compiler 1.7.
For some sites insecure connection error is displayed.
I have used firefox profile object as follow but still it's not resolving:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 3128);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
To work with Selenium 3.4.0 with Mozilla Firefox browser 53.x you need to download the latest geckodriver from here. Save it in your machine & provide the absolute path of the geckodriver.
Create a new Firefox profile manually by the name debanjan and use the AcceptUntrustedCertificates & setAssumeUntrustedCertificateIssuer options.
This code executes fine with some simple tweak to your own code.
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
testprofile.setPreference("network.proxy.type", 1);
testprofile.setPreference("network.proxy.http", "localhost");
testprofile.setPreference("network.proxy.http_port", 3128);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
Let me know if this helps you.
Try using firefox below 48 version. You won't face any problem or include below code in your existing code:
System.setProperty("webdriver.firefox.bin" ,"C:/Users/siddhesh.kalgaonkar/AppData/Local/Mozilla Firefox/firefox.exe");
It should solve your problem because this is what I use for current firefox version.
Use Firefox 54.0 64bit, Selenium v3.4.0, jcommender v1.7, TestNG v6.9.9, Java v8.0, Gecko driver v0.17.0
Use below code-
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
public class AppUrl {
public static WebDriver driver;
public static final String url = "https://10.10.1.1";
#BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.gecko.driver","C:/Users/Downloads/geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setAcceptInsecureCerts(true);
driver = new FirefoxDriver(desiredCapabilities);
driver.get(url);
}
}
Even I tried other sample codes from different sites. Today after I upgrade all the softwares and run the code, it worked for me.

How to run ghostdriver with Selenium using java

I want to use phantomJS for some web testing, and I've come across GhostDriver (https://github.com/detro/ghostdriver). I've built it using the instructions in the readme and I can run it on a specified port, but I am not sure how to access the web driver from my java code. To clarify, I've seen this example in ruby:
caps = {
:browserName => "phantomjs",
:platform => "LINUX"
}
urlhub = "http://key:secret#hub.testingbot.com:4444/wd/hub"
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120
#webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
#webdriver.navigate.to "http://www.google.com/"
puts #webdriver.title
#webdriver.save_screenshot("./screenshot.png")
#webdriver.quit
I'm just not sure how to do the same from java.
Just to clarify for others who might see this, to run it from java:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/Path/to/bin/phantomjs");
driver = new PhantomJSDriver(caps);
Then it can be used like a usual WebDriver.
I believe this link will answer your questions. You will need Selenium 2.28.0, and PhantomJS 1.8. I have tested this, and it works as advertised, although my tests were precursory. Note that you need to download the Selenium zip file to get the jar which contains the bindings. The Maven repo does not yet include it.
http://ivandemarino.me/2012/12/04/Finally-GhostDriver-1-0-0/
First download the exe file of the PhantomJSDriver. Don't need to install, only download this file from http://phantomjs.org/download.html and simply give the path of the exe file in the given code.
public class Browserlaunch {
public static void main(String[] args) {
DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/Drivers/phantomjs/bin/phantomjs.exe");
WebDriver driver=new PhantomJSDriver(DesireCaps);
driver.get("http://google.com");
}
}
Only set system property:
System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

Passing options to chrome driver selenium

I am trying to disable the output to the console for chrome. If I pass the --start-maximized option it works fine. I may have the wrong command?
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--silent"));
chrome = new ChromeDriver(_chromeservice,capabilities);
I also tried
ChromeOptions options = new ChromeOptions();
options.addArguments("silent");
chrome = new ChromeDriver(options);
Output
Started ChromeDriver port=26703 version=23.0.1240.0
log=/Brett/workspace/TestNG/chromedriver.log
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending
sendsBlockquote
Hinted by this Chromedriver ticket (about the silent option), I looked in the source of ChromeDriverService.java, and found a reference to "webdriver.chrome.logfile".
After adding -Dwebdriver.chrome.logfile="/dev/null" to my java command, the logs became readable again: The usless ChromeDriver logs were gone, while theSystem.out.println calls and exceptions are still shown in the console.
I start java with the following parameters (Linux / Mac):
DIR=path/to/dir/containing/selenium/and/stuff
cd "$DIR" && java -cp "$DIR\
:$DIR/output\
:$DIR/bin/selenium-server-standalone-2.33.0.jar" \
-Dwebdriver.chrome.driver="$DIR/bin/chromedriver" \
-Dwebdriver.chrome.args="--disable-logging" \
-Dwebdriver.chrome.logfile="/dev/null" \
AllTests
If you're on Windows:
set DIR=path\to\dir\containing\selenium\and\stuff
cd "%DIR%" && java -cp "%DIR%;%DIR%\output;%DIR%\bin\selenium-server-standalone-2.33.0.jar" ^
-Dwebdriver.chrome.driver="%DIR%\bin\chromedriver.exe" ^
-Dwebdriver.chrome.args="--disable-logging" ^
-Dwebdriver.chrome.logfile=NUL ^
AllTests
Explanation for the composition of my classpath (-cp): My tests are located in a directory at "$DIR/output". The Selenium jar file is placed in "$DIR/bin/selenium-server-standalone-2.33.0.jar". "AllTests" is the name of my class containing public static void main(String[] args) - this launches my tests.
The other parameters are self-explanatory, adjust it to your needs. For convenience (used in a shell/batch script), I've declared the common directory in a variable DIR.
When I was setting chrome up with
selenium-chrome-driver-2.48.2.jar
chromedriver 2.20
selenium-java-2.48.2.jar
none of the above answers worked for me,
Since I see some of the answers are a few years old, I will post what worked for me.
ChromeOptions chromeOptions = setupChromeOptions();
System.setProperty("webdriver.chrome.logfile", "\\path\\chromedriver.log");
System.setProperty("webdriver.chrome.driver", "\\path\\chromedriver.exe");
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver(chromeOptions);
Try "--disable-logging" instead.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-logging"));
chrome = new ChromeDriver(_chromeservice,capabilities);
As of Selenium 3 at least, you can use ChromeDriverService and its inner class Builder to be able to launch the driver in silent mode.
A oneliner for that:
new ChromeDriver(new ChromeDriverService.Builder().withSilent(true).build());
The constructor is straight-forward, you create a new service builder setting silent to true (that's the critical part) and you finally build it into a ChromeDriverService which is required by ChromeDriver's constructor.

Categories