Selenium Firefox 配置文件:设置指南
Selenium Firefox 配置文件
Firefox 配置文件是 Firefox 浏览器上可以进行设置、自定义、附加组件和其他个性化设置的集合。您可以自定义 Firefox 配置文件以满足您的 Selenium 自动化需求。
此外,Firefox 或任何其他浏览器都处理 SSL 证书设置。因此,将其与测试执行代码一起自动化非常有意义。
简而言之,配置文件是用户的个人设置。当您想在 Firefox 浏览器上运行可靠的自动化时,建议制作一个单独的配置文件。
磁盘上配置文件文件夹的位置
Firefox 配置文件就像不同的用户使用 Firefox 一样。Firefox 保存个人信息,例如书签、密码和用户偏好设置,这些都可以使用程序管理器进行编辑、删除或创建。
配置文件位置如下
- 对于 Windows 7 > /AppData/MozillaFirefoxProfile_name.default
- 对于 Linux > /.mozilla/firefox/profile_name.default/
- 对于 Mac OS X > ~/Library/ApplicationSupport/Firefox/Profiles/profile_name.default/
为了成功运行 Selenium 测试,Firefox 配置文件应该具备以下特点:
- 易于加载
- 如果需要,可以进行代理设置
- 根据自动化需求进行其他用户特定设置
如何为 Selenium 测试设置 Firefox 配置文件
让我们一步步了解如何创建 Firefox 配置文件。
步骤 1) 关闭 Firefox 浏览器
第一步,首先关闭已打开的 Firefox。
步骤 2) 打开运行 (Windows 键 + R) 并输入 firefox.exe –p
注意:如果打不开,您可以尝试使用带引号的完整路径。
- 在 32 位 Windows 上:“C:Program FilesMozilla Firefox.exe” –p
- 在 64 位 Windows 上:“C:Program Files(x86)Mozilla Firefox.exe” –p
步骤 3) 选择用户配置文件
现在,将打开一个名为 Firefox 的对话框
步骤 4) 创建配置文件
现在,从窗口中选择“创建配置文件”选项,将打开一个向导。单击“下一步”。
步骤 5) 命名您的配置文件
现在您的配置文件已准备好,您可以选择您的配置文件并打开 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
// 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 行:设置超时用于等待一段时间,以便浏览器在进入下一页之前加载页面。
- 代码第 21 行:关闭 Firefox。
让我们看另一个例子。
Firefox 配置文件示例 2
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 行:创建一个公共类 FirefoxProfile 2。
- 代码第 12 行:通过引用确切路径创建 myprofile 的对象。
- 代码第 14 行:为 Firefox 创建对象
- 代码第 16 行:最大化窗口。
- 代码第 18 行:Driver.get 用于导航到给定的 URL。
- 代码第 20 行:设置超时用于等待一段时间,以便浏览器在进入下一页之前加载页面。
- 代码第 22 行:关闭 Firefox。
摘要
- 自动化 Firefox 配置文件很有意义,因为它们处理 SSL 证书设置。
- Firefox 配置文件可以自定义以满足您的 Selenium 自动化需求。
- Firefox 配置文件应易于加载,并具有一些用户特定的代理设置以运行良好的测试。
- 要在 Selenium Webdriver 软件测试中访问新创建的 Firefox 配置文件,我们需要使用 webdriver 的内置类 'profilesIni' 及其方法 getProfile。