Selenium Firefox 配置文件:设置指南

Selenium Firefox 简介

Firefox 配置文件是设置、自定义、附加组件和其他个性化设置的集合,可以在 Firefox Browser.您可以自定义 Firefox 适合您的个人资料 Selenium 自动化要求。

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

简而言之,配置文件是用户的个人设置。当你想在 Firefox 浏览器,建议创建一个单独的配置文件。

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

Firefox 个人资料就像不同的用户使用 Firefox. Firefox 保存个人信息,例如书签、密码和用户偏好,可以使用程序管理器编辑、删除或创建这些信息。

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

配置文件的位置如下

  • 对于 Windows 7 > /AppData/MozillaFirefox配置文件名称.默认
  • 对于 Linux > /.mozilla/firefox/profile_name.default/
  • 对于 Mac OS X > ~/Library/ApplicationSupport/Firefox/配置文件/配置文件名称.default/

为了成功举办 Selenium 测试,一个 Firefox 个人资料应该是 -

  • 容易装载
  • 如果需要,请设置代理
  • 根据自动化需求的其他用户特定设置

如何设定 Firefox 个人资料 Selenium 检测

让我们一步一步看看如何创建一个 Firefox 个人资料。

步骤1) 关上 Firefox 浏览器

第一步,首先关闭 Firefox 如果打开。

步骤2) 打开运行 (Windows 键 + R)并输入 firefox.exe –p

米 Firefox 个人资料 Selenium 检测

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

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

步骤3) 选择用户个人资料

米 Firefox 个人资料 Selenium 检测

现在将打开名为 Firefox

步骤4) 创建个人资料

米 Firefox 个人资料 Selenium 检测

现在,从窗口中选择“创建配置文件”选项,将打开一个向导。单击下一步。

步骤5) 提供您的个人资料名称

米 Firefox 个人资料 Selenium 检测

现在您的个人资料已准备好,您可以选择您的个人资料并打开 Firefox.

你会注意到新的 Firefox 窗口将不会显示任何书签和收藏夹图标。

注: 最后选择的配置文件将在下次自动加载 Firefox 启动。如果您想更改配置文件,则需要重新启动配置文件管理器。

自动化脚本 Selenium

访问新创建的 Firefox 简介 Selenium Webdriver 软件测试,我们需要使用 webdrivers 内置类“profilesIni”及其方法 getProfile,如下所示。

Selenium 个人资料代码

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

ProfilesIni profile = new ProfilesIni();

// 这将为 Firefox 轮廓

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

// 这将初始化 Firefox 司机

WebDriver driver = new FirefoxDriver(myprofile)

让我们在下面的例子中看看该代码的实现。

Firefox 配置文件示例 1

Firefox 概要文件示例

// 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:创建公开课程“Firefox轮廓。”
  • 代码行 9:创建一个对象(您需要具备 oops 概念的基本知识)。
  • 代码行 10-11:我们需要初始化 Firefox profile 与 myprofile 对象。
  • 代码行 13:创建对象 Firefox
  • 代码行 15:最大化窗口。
  • 代码行 17:Driver.get 用于导航到给定的 URL。
  • 代码行 19:设置超时用于等待一段时间,以便浏览器可以在继续下一页之前加载该页面。
  • 代码行 21:关闭 Firefox.

我们再看一个例子。

Firefox 配置文件示例 2

Firefox 概要文件示例

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:创建公开课 Firefox简介 2。
  • 代码行 12:通过引用精确路径来创建myprofile的对象。
  • 代码行 14:为 Firefox 创建对象
  • 代码行 16:最大化窗口。
  • 代码行 18:Driver.get 用于导航到给定的URL。
  • 代码行 20:设置超时用于等待一段时间,以便浏览器可以在继续下一页之前加载该页面。
  • 代码行 22: 关闭 Firefox.

结语

  • 自动化 Firefox 配置文件非常有意义,因为它们处理 SSL 证书设置。
  • Firefox 可以定制个人资料以适合您 Selenium 自动化要求。
  • Firefox 配置文件应该易于加载,并具有一些用户特定的代理设置以运行良好的测试。
  • 访问新创建的 Firefox 简介 Selenium Webdriver 软件测试,我们需要使用 webdrivers 内置类“profilesIni”及其方法 getProfile。

总结一下这篇文章: