How can I set a default profile for the Firefox driver in Selenium Webdriver 3? How can I set a default profile for the Firefox driver in Selenium Webdriver 3? selenium selenium

How can I set a default profile for the Firefox driver in Selenium Webdriver 3?


As you are using Selenium 3.14.0 as per the FirefoxDriver Class the valid constructors are:

  • FirefoxDriver()
  • FirefoxDriver(FirefoxOptions options)
  • FirefoxDriver(GeckoDriverService service)
  • FirefoxDriver(GeckoDriverService service, FirefoxOptions options)
  • FirefoxDriver(XpiDriverService service)
  • FirefoxDriver(XpiDriverService service, FirefoxOptions options)

So, as per your code attempts the following is not a valid option to invoke FirefoxDriver()

WebDriver driver = new FirefoxDriver(profile);

Solution

To invoke invoke FirefoxDriver() with the default profile you need to use the setProfile(profile) method to set the FirefoxProfile through an instance of FirefoxOptions() and you can use the following code block:

  • Code Block:

    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.ProfilesIni;import org.testng.annotations.Test;public class A_FirefoxProfile {      @Test      public void seleniumFirefox() {        System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");        ProfilesIni profileIni = new ProfilesIni();        FirefoxProfile profile = profileIni.getProfile("default");        FirefoxOptions options = new FirefoxOptions();        options.setProfile(profile);        WebDriver driver = new FirefoxDriver(options);        driver.get("http://www.google.com");        System.out.println(driver.getTitle());      }}
  • Console Output:

    [RemoteTestNG] detected TestNG version 6.14.21537775040906   geckodriver INFO    geckodriver 0.20.11537775040923   geckodriver INFO    Listening on 127.0.0.1:28133Sep 24, 2018 1:14:30 PM org.openqa.selenium.remote.ProtocolHandshake createSessionINFO: Detected dialect: W3CGooglePASSED: seleniumFirefox===============================================    Default test    Tests run: 1, Failures: 0, Skips: 0==============================================================================================Default suiteTotal tests run: 1, Failures: 0, Skips: 0===============================================


move your setup to @BeforeClass

I'm using this setup, works just fine:

@BeforeClasspublic 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();}

just change paths and profile designation.