如何在Selenium WebDriver中创建Firefox配置文件

Firefox配置文件是可以在Firefox浏览器上完成的设置、自定义、加载项和其他个性化设置的集合。可以自定义Firefox配置文件以满足Selenium自动化需求。

此外,Firefox或任何其他浏览器处理SSL证书设置。因此,将它们与测试执行代码一起自动化非常有意义。

简而言之,个人资料是用户的个人设置。当想要在Firefox浏览器上运行可靠的自动化时,建议创建单独的配置文件。

在本教程中,将了解-

  • 配置文件文件夹在磁盘中的位置
  • 如何创建Firefox配置文件
  • Selenium的自动化脚本
  • Firefox配置文件示例1
  • Firefox配置文件示例2

配置文件文件夹在磁盘中的位置

Firefox配置文件就像不同的用户使用Firefox一样。Firefox保存个人信息,如书签、密码和用户首选项,这些信息可以使用程序管理器进行编辑、删除或创建。

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver

配置文件的位置如下

  • 对于Windows 7>/appdata/MozillaFirefoxProfile_name.default
  • 对于Linux>/.mozilla/Firefox/profile_name.default/
  • 对于MacOSX>~/Library/ApplicationSupport/Firefox/Profiles/profile_name.default/

为了成功运行Selenium测试,Firefox配置文件应该-

  • 易于加载
  • 代理设置(如果需要)
  • 基于自动化需求的其他特定于用户的设置

如何创建Firefox配置文件

让我们逐步看看如何创建一个Firefox配置文件。

步骤1)首先,如果打开了Firefox,则将其关闭。

步骤2)打开Run(Windows键+R)并键入Firefox.exe-p,然后单击确定

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver

注意:如果没有打开,可以尝试使用用引号括起来的完整路径。

  • 在32位Windows上:“C:Program FilesMozilla Firefox.exe”-p
  • 64位上:Windows:“C:Program Files(x86) Mozilla Firefox.exe”-p

步骤3)将打开一个名为Firefox-Choose User Profile的对话框

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver

步骤4)在窗口中选择“创建配置文件”选项,将会打开一个向导。单击Next(下一步)

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver

步骤5)提供要创建的配置文件名称,然后单击完成按钮

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver

现在配置文件已经准备好了,可以选择配置文件并打开Firefox。

会注意到新的Firefox窗口不会显示任何喜欢的书签和图标。

注意:上次选择的配置文件将在下次Firefox启动时自动加载。如果希望更改配置文件,则需要重新启动配置文件管理器。

Selenium的自动化脚本

要在Selenium Webdriver软件测试中访问新创建的Firefox配置文件,我们需要使用webdriver内置的类‘profilesIni’及其方法getProfile,如下所示。

配置文件的Selenium代码

这是实现配置文件的代码,可以嵌入到Selenium代码中。

ProfilesIni profile = new ProfilesIni();

//这将为Firefox配置文件创建一个对象

FirefoxProfile myprofile = profile.getProfile("xyzProfile");

//这将初始化Firefox驱动程序

WebDriver driver = new FirefoxDriver(myprofile)

让我们在下面的示例中查看此代码的实现。

Firefox配置文件示例1

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver
// import the package
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 FirefoxProfile {
    public static void main(String[] args) {
        ProfilesIni profile = new ProfilesIni();
        FirefoxProfile myprofile = profile.getProfile("xyzProfile");
// Initialize Firefox driver
        WebDriver driver = new FirefoxDriver(myprofile);
//Maximize browser window
        driver.manage().window().maximize();
//Go to URL which you want to navigate
        driver.get("http://www.google.com");
//Set  timeout  for 5 seconds so that the page may load properly within that time
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//close firefox browser
        driver.close();
    }
}

代码说明:

下面是代码的逐行说明。

  • 代码行2-7:首先,我们需要导入运行Selenium代码所需的包。
  • 代码行8:创建一个公共类“FirefoxProfile”。
  • 代码行9:创建一个对象(需要具备OOPS概念的基本知识)。
  • 代码行10-11:我们需要用myprofile对象初始化Firefox配置文件。
  • 代码行13:为Firefox创建对象
  • 代码行15:最大化窗口。
  • 代码行17:Driver.get用于导航到给定的URL。
  • 代码行19:set timeout用于等待一段时间,以便浏览器可以在进入下一页之前加载该页。
  • 代码行21:关闭Firefox。

让我们再看一个例子。

Firefox配置文件示例2

How to Create Firefox Profile in Selenium WebDriver
How to Create Firefox Profile in Selenium WebDriver
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 FirefoxProfile2 {
    public static void main(String[] args) {
// Create object for FirefoxProfile
        FirefoxProfilemyprofile=newFirefoxProfile (newFile("\c:users\AppData\MozillaFirefoxProfile_name.default "));
// 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(5, TimeUnit.SECONDS);
//close firefox browser
        driver.close();
    }

代码说明:

下面是代码的逐行说明。

  • 代码行1-6:首先,我们需要导入运行Selenium代码所需的包。
  • 代码行8:创建一个公共类FirefoxProfile2。
  • 代码行12:通过引用确切路径创建myprofile的对象。
  • 代码行14:为Firefox创建对象
  • 代码行16:最大化窗口。
  • 代码行18:Driver.get用于导航到给定的URL。
  • 代码行20:set timeout用于等待一段时间,以便浏览器可以在进入下一页之前加载该页。
  • 代码行22:关闭Firefox。

总结:

  • 自动化Firefox配置文件非常有意义,因为它们处理SSL证书设置。
  • 可以定制Firefox配置文件以满足Selenium自动化需求。
  • Firefox配置文件应该很容易加载,并且要有一些特定于用户的代理设置才能运行良好的测试。
  • 要在Selenium Webdriver软件测试中访问新创建的Firefox配置文件,我们需要使用webdriver内置的类‘profilesIni’及其方法getProfile。

IT赶路人

专注IT知识分享