欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Custom Firefox Profile for Selenium

程序员文章站 2022-07-15 20:07:35
...

原文地址: http://www.toolsqa.com/selenium-webdriver/custom-firefox-profile/

What is Profile

Firefox saves your personal information such as bookmarks, passwords, and user preferences in a set of files called your profile, which is stored in a separate location from the Firefox program files. 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.

Why do I need New Profile

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.

 

You should be consistent with the profile you use on all development and test execution machines. If you used different profiles everywhere, the SSL certificates you accepted or the plug-ins you installed would be different and that would make the tests behave differently on the machines.

 

  • There are several times when you need something special in your profile to make test execution reliable. The most common example is a SSL certificate settings or browser plug-ins that handles self-signed certs. It makes a lot of sense to create a profile that handles these special test needs and packaging and deploying it along with the test execution code.
  • You should use a very lightweight profile with just the settings and plug-ins you need for the execution. Each time Selenium starts a new session driving a Firefox instance, it copies the entire profile in some temporary directory and if the profile is big, it makes it, not only slow but unreliable as well.

Finding Your Profile Folder

Your profile folder’s location depends on the operating system you use. The following table shows the typical location of the default profile:

Operating System Profile Folder Path
Windows 7 / 8

%AppData%\Local\Mozilla\Firefox\Profiles\xxxxxxxx.default

%AppData%\Roaming\Mozilla\Firefox\Profiles\xxxxxxxx.default

Linux ~/.mozilla/firefox/xxxxxxxx.default/
Mac OS X ~/Library/Application Support/Firefox/Profiles/xxxxxxxx.default/

 

There are two points of interest in this table. The first is the xxxxxxxx string preceding each profile name. This string is simply a collection of 8 random numbers and characters, used to ensure that each profile is unique. Firefox automatically prepends a randomized string to any new profile for you, so you never need to worry about creating this portion of the name.

The second point of interest lies in the Windows XP / 2000 / Vista / 7 / 8 paths. The %AppData% string is actually a special Windows variable, pointing to your “Application Data” path. This is typically of the form:

C:\Users\{User Name}\AppData

Creating a New Profile

Creating a New Firefox profiles and use the same in the Test script involves three steps process. First you need to Start the Profile Manager, second is to Create a New Profile and third is to use the same profile in Test scripts.

Step 1: Starting the Profile Manager

1) At the top of the Firefox window, click on the File menu and then select Exit.
Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 

2) Press ‘Win + R’  or click on the Windows Start Menu (bottom left button) and then select Run.
Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 

3) In the Run dialog box, type in: ‘firefox.exe -p' and then Click OK.
Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
 

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.


Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
 

Step 2: Creating a Profile

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

Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
 2) Click ‘Next >’ in the ‘Create Profile Wizard’ window that comes up.


Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
 3) Type in a new name ‘profileToolsQA’ in the ‘Enter new profile name’ box and click ‘Finish’.
Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
 4) ‘Choose User Profile’ window will display the newly created profile in the list.


Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
 

5) 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.

Step 3: User Custom Profile in Selenium

Once the Automation Profile is created, it needs to be called in the Test scripts. You can now add the below code to your Test scripts to Instantiating the Firefox Driver:

 

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

 
 Disable firefox firstrun page

 

profile.setPreference("browser.startup.homepage_override.mstone", "ignore");

 

Do not prompt for authentication if password is saved

 

profile.setPreference("signon.autologin.proxy", true);

 

附:启动浏览器、设置profile&加载插件

  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 17.7 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 18.2 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 26.8 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 18.7 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 21.7 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 24.5 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 27.1 KB
  • Custom Firefox Profile for Selenium
            
    
    博客分类: Test SeleniumFirefoxProfile 
  • 大小: 23.8 KB