Unable to execute Headless Geckodriver with TestNG - java

Since I implemented the Headless configuration on my Test I received this issue: java.lang.NullPointerException
I tried to to switch to other types of implemantation for the Gecko Headless, but none of them worked
#BeforeTest
public static void OpenBrowser () {
System.setProperty("webdriver.gecko.driver","binaries/geckodriver");
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("-headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
driver.get(...
}
Once the test is executed I receive th3 following error: java.lang.NullPointerException

The commands to get it running in headless mode are as follows:
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
WebDriver driver = new FirefoxDriver(options);
You'll probably want to have the WebDriver driver part defined outside the test so that you can perform the following in your #BeforeTest like so:
WebDriver driver;
#BeforeTest
public static void OpenBrowser() {
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
driver = new FirefoxDriver(options);
}

Related

Selenide: How to open Chrome browser with extensions

I've faced with a problem that I'm not able to open Chrome with any extension. I've already added an extension but don't know how to run it properly with Selenide framework. Could you please help me
#BeforeClass
public static void setUp() {
Configuration.browser = "chrome";
System.setProperty("selenide.browser", "chrome");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
}
Selenide : http://selenide.org/2018/01/12/selenide-4.10/
You can set custom capabilities in Configuration, and Selenide will use them when opening a browser:
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserCapabilities.setCapability(SOME_CAP, "SOME_VALUE_FROM_CONFIGURATION");
Also you can set custom webdriver like in #dangi13 answer:
WebDriverRunner.setWebDriver(myDriverWithExtension);
I do not know how to do it in selenide but you can add extension in selenium like this :
public static WebDriver getChromeDriverWithAdblockCrx() {
System.setProperty("webdriver.chrome.driver", "src//main//resources//chromedriver.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src//main//resources//uBlock Origin.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(capabilities);
}
Hope that helps you:).
#sers, #dangi13 Thanks a lot!
But capabilities were not added from Configuration.browserCapabilities. I wrote the following code:
#BeforeClass
public static void setUp() {
Configuration.browser = "chrome";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
}
It is known issue that is mentioned on github: https://github.com/codeborne/selenide/issues/676
As workarond I'm using the following option:
#BeforeClass
public static void setUp() {
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
Configuration.browser = "chrome";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
WebDriver webDriver = new ChromeDriver(options);
setWebDriver(webDriver);
}

unable to launch FireFox custom profile with geckodriver in java

I'm trying to launch a Firefox profile with add-ons in it, with selenium v3.12 and gecko-driver v2.10 and Firefox version 60.0, how-ever it seems that the custom profile is not working. below is my code
static WebDriver driver;
ProfilesIni profile = new ProfilesIni();
myprofile = profile.getProfile("AutoProfile");
System.setProperty("webdriver.gecko.driver",
"E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
driver = new FirefoxDriver(myprofile);
the acutal error is on the line
driver = new FirefoxDriver(myprofile);
as
The constructor FirefoxDriver(FirefoxProfile) is undefined
You have to pass it through firefox options.
System.setProperty("webdriver.gecko.driver", "E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("AutoProfile");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(myprofile);
WebDriver driver = new FirefoxDriver(firefoxOptions);
If the below solution causes a java heap error, you could try DesiredCapabilities, like this:
System.setProperty("webdriver.gecko.driver","E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
File file = new File(path_to_your_firefox_profile);
DesiredCapabilities dc = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile(file);
dc.setCapability(FirefoxDriver.PROFILE, profile);
FirefoxDriver driver = new FirefoxDriver(dc);

Getting error as "NoSuchSessionException" when trying to open an website through FirefoxProfile

I am running the below code, to open a URL. However, I am getting error as "NoSuchSessionException". Kindly suggest.
Is it because of the below versions I am using.
Selenium--> 3.12.0, Firefox Setup 50.0 and geckodriver-v0.21.0-win64
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class Gmail {
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver", "D:\\Drivers\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProf = new ProfilesIni();// all profiles
FirefoxProfile prof = allProf.getProfile("Abhi_Selenium");
options.setProfile(prof);
//FirefoxDriver driver = new FirefoxDriver(options);
WebDriver driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://gmail.com");
}
}
You have 2 ways to use a existing Firefox Profile to access a Web Application as follows:
Using DesiredCapabilities() and FirefoxOptions():
public class FirefoxProfile_dc_opt {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com");
}
}
Using FirefoxOptions():
public class FirefoxProfile_opt {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com");
}
}
Note: Ensure that you have already created a Firefox Profile as Abhi_Selenium before you trigger your Test.
Update
As you are still seeing the exception as no such session, perform the following upgradation/cleanup steps:
Upgrade JDK to recent levels JDK 8u181.
Upgrade Selenium to current levels Version 3.13.0.
Upgrade GeckoDriver to GeckoDriver v0.20.1 level.
Ensure GeckoDriver is present in the specified location.
Ensure GeckoDriver is having executable permission for non-root users.
Upgrade Firefox version to Firefox v61.0.1 levels.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
(WindowsOS only) Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
(LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint before and after the execution of your Test Suite.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your Test as a non-root user.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
You can use FireFoxProfile class and FirefoxOptions class to set a profile.
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
options.setProfile(firefoxProfile);
On the first look the path to firefox.exe is missing. There is my setup:
public class foo{
private static WebDriver driver;
#BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();}
// #Before, #After, #AfterClass and #Test
}

Selenium: changing proxy in Firefox

i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?
I have code below:
public class SodirRejestracja {
String baseUrl = "http://google.pl";
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;
#BeforeTest
public void beforeTest() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
System.setProperty("webdriver.gecko.driver", driverPath);
driver=new FirefoxDriver(profile);
}
#Test
public void test(){
driver.get("http://google.com");
}
}
Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?
but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"
A sample (not tested but that compiles) that should do it
String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
.setFtpProxy(proxyName)
.setSslProxy(proxyName);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);
This code works
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxy);
driver = new FirefoxDriver(options);

How to run chrome extension with Selenium

How to run chrome extension after that?
I already added it but how to run?
public class asset_testing {
#Test
public void user(){
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=C:\\GUI\\distr\\chrome");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", "C:/my-app/drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver(capabilities);

Categories

Resources