How to create custom Firefox profile and handle Firefox profile in Selenium Webdriver using Java.

A collection of browser bookmarks, settings, extensions, passwords, history and user preferences call profile, which is stored in a separate location from the browser program files; in short, all of your personal settings.You can have multiple Firefox profiles, each containing a separate set of user information. The Profile Manager allows you to create, remove, rename, and switch profiles. The default Firefox profile is not very automation friendly.  When you want to run automation reliably on a Firefox browser it is advisable to make a separate profile. Automation profile should be light to load and have special proxy and other settings to run good test.

How to create custom Firefox profile

1) At the top of the Firefox window, click on the File menu and then select Exit.

2) Press ‘Windows Key + R’  or click on the Windows Start Menu (bottom left button) and then select Run.

3) In the Run dialog box, type in: ‘firefox.exe -p' and then Click OK.

Note: If the Profile Manager window does not appear, it may be opened in the background. It needs to be closed properly, you may use Ctrl+Alt+Del program to kill it. If it still does not open then you may need to specify the full path of the Firefox program, enclosed in quotes; for example:
On 32-bit Windows: "C:Program FilesMozilla Firefoxfirefox.exe" -p
On 64-bit Windows: "C:Program Files (x86)Mozilla Firefoxfirefox.exe" -p

4) The Choose User Profile window will look like this.

5) Click the ‘Create Profile…’ button on the ‘Firefox – Choose User Profile’ window that comes up.

6) Click ‘Next >’ in the ‘Create Profile Wizard’ window that comes up.

7) Type in a new name ‘profileToolsQA’ in the ‘Enter new profile name’ box and click ‘Finish’.

8) ‘Choose User Profile’ window will display the newly created profile in the list.

9) Click on the ‘Start Firefox’ box. Firefox will start with the new profile.

Note: You will notice that the new Firefox window will not show any of your Bookmarks and Favorite icons.
Note: The last selected profile will then start automatically when you next start Firefox and you will need to start the Profile Manager again to switch profiles.

How to handle new Firefox profile in Selenium Webdriver

Two way We can call custom Firefox profile.

One Way:
ProfilesIni profile = new ProfilesIni();
// Create object for FirefoxProfile
FirefoxProfile myprofile = profile.getProfile("profileToolsQA");
// Initialize Firefox driver     
WebDriver driver = new FirefoxDriver(myprofile)
Example one:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class HandlecustomProfile1 {
public static void main(String[] args) {

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("profileToolsQA");
  
// Initialize Firefox driver    
WebDriver driver = new FirefoxDriver(myprofile);

//Maximize browser window       
driver.manage().window().maximize();
//Go to URL      
driver.get("http://www.google.com");
//Set  timeout      
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//close firefox browser  
driver.close();
    }

}
Other way:
// Create object for FirefoxProfile
FirefoxProfile myprofile = new FirefoxProfile(new File("C:\\Users\\Hiro Mia\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\giyi7yui.profileToolsQA"));
// Initialize Firefox driver     
WebDriver driver = new FirefoxDriver(myprofile)
Example Two
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class HandlecustomProfile2 {
public static void main(String[] args) {

// Create object for FirefoxProfile
FirefoxProfile myprofile = new FirefoxProfile(new File("C:\\Users\\Hiro Mia\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\giyi7yui.profileToolsQA"));
  
// Initialize Firefox driver    
WebDriver driver = new FirefoxDriver(myprofile);

//Maximize browser window       
driver.manage().window().maximize();
//Go to URL      
driver.get("http://www.yahoo.com");
//Set  timeout      
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//close firefox browser  
driver.close();
    }

}

No comments:

Post a Comment