Getting java.lang.NullPointerException in profile.setPreference("browser.popups.showPopupBlocker", false); - selenium

System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
ProfilesIni profile2 = new ProfilesIni();
FirefoxProfile profile3 = profile2.getProfile("AutoProfile");
profile3.setPreference("browser.popups.showPopupBlocker", false);
profile3.setPreference("browser.download.dir", "D:\\WebDriverDownloads");
profile3.setPreference("browser.download.folderList", 2);
profile3.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");
profile3.setPreference( "browser.download.manager.showWhenStarting", false );
profile3.setPreference( "pdfjs.disabled", true );
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile3);
WebDriver driver = new FirefoxDriver(firefoxOptions);

The most probhable reason you are seeing a java.lang.NullPointerException is because the Firefox Profile which you are trying to use i.e. AutoProfile doesn't exist (yet to be created) on your local system.
Solution
Before you start using the Firefox Profile AutoProfile ensure that this Firefox Profile exists in your local system. You can create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows.
To configure auto downloads setPreference("browser.popups.showPopupBlocker", false) is not required so you can remove it.
Execute your #Test.
Here you can find a detailed discussion on how to use a new/existing Firefox Profile for your tests

Related

How to load a specific firefox profile using Selenium webdrivermanager?

I'm trying to use webdrivermanager library to load firefox driver in my selenium tests. I'm unable to load a specific firefox profile using this library. Here is what i'm trying to do:
FirefoxDriverManager.getInstance().setup() // To instantiate the firefox driver
ProfilesIni Prof = new ProfilesIni();
FirefoxProfile profile = Prof.getProfile("C:\\Users\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\6xv9ndwh.SELENIUM");
WebDriver driver = new FirefoxDriver(profile);
But this instantiates a new driver and does not force the driver instantiated by firefoxdrivermanager to use the specific profile.
I tried using default gecko driver too that does not load the profile either. Here is the code that i'm trying:
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver-v0.20.0-win64\\geckodriver.exe");
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile Profile = allProfiles.getProfile('default');
Profile.setAcceptUntrustedCertificates(true);
Profile.setAssumeUntrustedCertificateIssuer(false);
driver = new FirefoxDriver(Profile);
Can someone help me on this please?
First of all create a new firefox profile
steps for it are
1. Run this command firefox.exe -p in run window
It will show this dialog box create profile with new name and exit the window.
After that perform this command in webdriver
System.setProperty("webdriver.firefox.marionette", "Path to the exe of firefox driver");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("UrProfile Name which u created");
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("http://www.google.com");
Hope it may help u...
This worked for me (though not using webdrivermanager) for instantiating webdriver using Gradle:
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability("acceptInsecureCerts", true);
driver = {new FirefoxDriver(capabilities)}

Running headless Firefox WebDriver on Jenkins (Windows OS)

My test cases involve export/download the excel files from web pages. For which I am using Firefox profile to accept the downloads when the download dialog popup on windows. The following code is working when I execute my test on local windows.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile fProfile = profile.getProfile("Selenium");
fProfile.setPreference("browser.download.folderList", 2);
fProfile.setPreference("browser.download.manager.showWhenStarting", false);
fProfile.setPreference("browser.download.dir", "C:\\temp\\reports\\");
fProfile.setPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
fProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
fProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
fProfile.setPreference("browser.download.manager.focusWhenStarting", false);
fProfile.setPreference("browser.download.manager.useWindow", false);
fProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
fProfile.setPreference("browser.download.manager.closeWhenDone", false);
fProfile.setAcceptUntrustedCertificates(true);
fProfile.setAssumeUntrustedCertificateIssuer(true);
fProfile.setPreference("security.insecure_field_warning.contextual.enabled", false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, fProfile);
capabilities.setCapability("marionette", true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setAcceptInsecureCerts(true);
driver = new FirefoxDriver(capabilities);
I want to run the tests on Jenkins and I have been running into the issues. I receive Nullpointer exception on the line right after I initialize the firefox profile. Which means the firefox profile did not pickup. Following is the error.
I am wondering if Jenkins is not understanding the firefox profile "Selenium" which I created through Firefox Profile section.
Note: I can run my tests from the windows command line but not through the Jenkins.
Any help is highly appreciated.
Instead of using capabilities, use FirefoxOptions
FirefoxOptions options = new FirefoxOptions();
options.addArgument("--headless");
WebDriver driver = new FirefoxDriver(options);

Unable to load custom profile in FirefoxDriver : The constructor FirefoxDriver(FirefoxProfile) is undefined

I am trying to handle multiple popup blocker in www.naukri.com; For this I have created a custom profile in firefox naming "AutoProfile". But I am having an issue with loading this custom profile in firefox driver;
System.setProperty("webdriver.gecko.driver", "F:\\abc\\geckodriver-v0.18.0-win64\\geckodriver.exe");
ProfilesIni profile2=new ProfilesIni();
FirefoxProfile profile3=profile2.getProfile("AutoProfile");
profile3.setPreference("browser.popups.showPopupBlocker", false);
driver =new FirefoxDriver(profile3);
driver.get("www.naukri.com");
But I am getting an error in driver=new FirefoxDriver(profile3); It says:
The constructor FirefoxDriver(FirefoxProfile) is undefined.
Some times I get a message as constructor is deprecated.
What are the version of Selenium and the Geckodriver that are you using?
From https://raw.githubusercontent.com/SeleniumHQ/selenium/master/rb/CHANGES
3.4.1 (2017-06-13)
==================
Firefox:
* Added new Firefox::Options class that should be used to customize browser
behavior (command line arguments, profile, preferences, Firefox binary, etc.).
The instance of options class can be passed to driver initialization using
:options key. Old way of passing these customization directly to driver
initialization is deprecated.
In order to set the profile, you should do something like this:
System.setProperty("webdriver.gecko.driver", "F:\\abc\\geckodriver-v0.18.0-win64\\geckodriver.exe");
ProfilesIni profile2 = new ProfilesIni();
FirefoxProfile profile3 = profile2.getProfile("AutoProfile");
profile3.setPreference("browser.popups.showPopupBlocker", false);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile3);
WebDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("www.naukri.com");
The issue is due to older selenium version coexisted. mvn clean install resolved the issue.
Update gecko driver and library
Hope your problem will be solved

How to automatically close firebug tab when launching selenium firefox webdriver with firebug?

I have included firebug when launching firefox driver and it works very fine with latest selenium web driver 3.0 but meanwhile it also opens new firebug tab every time when launching browser.
As code says, i have included firebug file and added this extension in created profile. Is there any way to close the firebug tab automatically after launching the browser? If there is no automatic way then i need to use tweak to close window named "Firebug" right?
Code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
You can set the "showFirstRunPage" flag to "false" in your porfile.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("extensions.firebug.showFirstRunPage", false);
Here is a link to all firebug preferences you can set.
Firebug Preferences
An other solution is to set the "currentVersion" to a big number like this.
profile.setPreference("extensions.firebug.currentVersion", "999");
I prefer the first on ;)
I was looking for automatically closing Firebug window but i think its not possible so posting an answer to handle opened windows after launching firebox browser with firebug capabilities, unfortunately you need to deal with extra lines of code due to this issue :)
Below solution it works fine, just use it like below:
Working code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
// Close the firebug window
Thread.sleep(4000); // Firebug window takes time to open it
Set <String> windows = webDriver.getWindowHandles();
String mainwindow = webDriver.getWindowHandle();
for (String handle: windows) {
webDriver.switchTo().window(handle);
if (!handle.equals(mainwindow)) {
webDriver.close();
}
}
webDriver.switchTo().window(mainwindow);

Browser Plugin Testing With Selenium

I am writing a webapp that has a browser plugin component for both firefox and chrome. My current testing system uses a series of Selenium tests created through Selenium IDE.
Is it possible to also have selenium install, activate, and delete browser plugins for firefox and chrome (possibly other browsers as well)?
I think the biggest concern is that installing/enabling the browser plugin requires a browser restart, and I'm not sure if that would through selenium off.
The acquisition of the plugin is easily handled by visiting an internal site-link to a php-script that detects your browser.
The answer is Yes, Selenium 2 supports (remote) installation of browser extensions.
The Chrome and Firefox WebDriver support the installation of extensions, remotely. Here's sample code for Chrome and Firefox:
Chrome
File file = new File("extension.crx"); // zip files are also accepted
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new ChromeDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Firefox
File file = new File("extension.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new FirefoxDriver(firefoxProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
I have also implemented automated installation of Opera and Safari extensions, and they have been merged upstream:
OperaDriver: https://github.com/operasoftware/operadriver/pull/93
SafariDriver: https://github.com/SeleniumHQ/selenium/pull/87
Opera
This API is similar to the FirefoxDriver.
File file = new File("extension.oex"); // Must end with ".oex"
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new OperaDriver(operaProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.profile", operaProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Safari
This API is similar to the ChromeDriver.
File file = new File("extension.safariextz");
SafariOptions options = new SafariOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new SafariDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Internet Explorer
Good luck.
Short answer: no
Installing a browser extension is outside of the scope of handling in Selenium.
In Chrome, it displays a modal window that is not "clickable" with Selenium when you want to add a plugin or app. Chrome does not require restarting.
Firefox has the same kind of behaviour to prompt for extension permissions.
You can try something that resides outside of the browser to do what you want. Sikuli might do the trick.