Sunday 10 May 2015

In selenium webdriver API- defining Firefox Profile preferences



Rarely, your script may need to update the preferences within Firefox browser. Achieving this by instantiating a Firefox Profile object and then update the settings.
After that we need to pass this object into FirefoxDriver which will automatically load the profile with your defined settings.
Check them using command "about:config" in the browser. It will display a warning message and then asks you to proceed if required. Be carefully while doing any changes.
Let make an assumption that you want to have google as the startup page for Firefox browser. To achieve this will need to update the browser.startup.homepage preference.
Below steps to add a website as set your homepage:
a. Let  first start by creating the FirefoxProfile object:
FirefoxProfile fifrefoxprofile= new FirefoxProfile();
b. Now we set the preference by using the below mentioned method:
profile.setPreference("browser.startup.homepage","
https://www.google.com");
c. Get the profile to be used, we need to pass it in to the driver. To achieve this, we need
to do the following:
driver = new FirefoxDriver(fifrefoxprofile);
d. Run your test. The final script  should look like below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class FireFoxProfileExampletest {

WebDriver driver;

@Test
public void testExamplesprofile(){
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.startup.homepage","http://www.google.com");
driver = new FirefoxDriver(profile);
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("100");

}
Check if that has applied to the Firefox browser or not. Please enter "about:config" in address bar, and type "browser.startup.homepage".
It will display the website that you have added.
The below screen shot shows "www.google.com" which is added by using the code.
FireFox-about:config
Search-browser.startup.homepage
Preference name                   Status            Type         Value
browser.startup.homepage   user set         string         http://www.google.com

No comments:

Post a Comment