使用Selenium的浏览器插件测试

使用Selenium的浏览器插件测试

本文介绍了使用Selenium的浏览器插件测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个webapp,它有一个浏览器插件组件为firefox和chrome。我当前的测试系统使用通过Selenium IDE创建的一系列Selenium测试。



是否也可以安装,激活和删除firefox和chrome的浏览器插件(可能还有其他浏览器)?



我认为最大的问题是安装/启用浏览器插件需要浏览器重新启动,我不确定是否通过selenium关闭。



通过访问内部站点链接,可以轻松处理对插件的获取。链接到检测您的浏览器的php脚本。

解决方案

答案是,Selenium 2支持(远程)安装浏览器扩展。



Chrome和Firefox WebDriver支持远程安装扩展程序。下面是Chrome和Firefox的示例代码:



Chrome



 文件file = new File(extension.crx); // zip文件也被接受
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);

//选项1:本地。
WebDriver driver = new ChromeDriver(options);

//选项2:远程
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new RemoteWebDriver(新URL(http:// localhost:4444 / wd / hub),capabilities);



Firefox



 文件file = new File(extension.xpi); 
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);

//选项1:本地
WebDriver driver = new FirefoxDriver(firefoxProfile);

//选项2:远程
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE,firefoxProfile);
WebDriver driver = new RemoteWebDriver(新URL(http:// localhost:4444 / wd / hub),capabilities);






我还实现了Opera和Safari的自动安装扩展程序,并且已在上游合并:




  • OperaDriver:

  • SafariDriver:



Opera



这个API类似于FirefoxDriver。

 文件file = new File(extension.oex); //必须以.oex结尾
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);

//选项1:本地
WebDriver driver = new OperaDriver(operaProfile);

//选项2:远程
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability(opera.profile,operaProfile);
WebDriver driver = new RemoteWebDriver(新URL(http:// localhost:4444 / wd / hub),capabilities);



Safari



 文件file = new File(extension.safariextz); 
SafariOptions options = new SafariOptions();
options.addExtensions(file);

//选项1:本地。
WebDriver driver = new SafariDriver(options);

//选项2:远程
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY,options);
WebDriver driver = new RemoteWebDriver(新URL(http:// localhost:4444 / wd / hub),capabilities);



Internet Explorer



祝你好运。 / p>

I am writing a webapp that has a browser plugin component for both firefox and chrome. My current testing system uses a series of Selenium tests created through Selenium IDE.

Is it possible to also have selenium install, activate, and delete browser plugins for firefox and chrome (possibly other browsers as well)?

I think the biggest concern is that installing/enabling the browser plugin requires a browser restart, and I'm not sure if that would through selenium off.

The acquisition of the plugin is easily handled by visiting an internal site-link to a php-script that detects your browser.

解决方案

The answer is Yes, Selenium 2 supports (remote) installation of browser extensions.

The Chrome and Firefox WebDriver support the installation of extensions, remotely. Here's sample code for Chrome and Firefox:

Chrome

File file = new File("extension.crx"); // zip files are also accepted
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);

// Option 1: Locally.
WebDriver driver = new ChromeDriver(options);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Firefox

File file = new File("extension.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);

// Option 1: Locally
WebDriver driver = new FirefoxDriver(firefoxProfile);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);


I have also implemented automated installation of Opera and Safari extensions, and they have been merged upstream:

Opera

This API is similar to the FirefoxDriver.

File file = new File("extension.oex"); // Must end with ".oex"
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);

// Option 1: Locally
WebDriver driver = new OperaDriver(operaProfile);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.profile", operaProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Safari

This API is similar to the ChromeDriver.

File file = new File("extension.safariextz");
SafariOptions options = new SafariOptions();
options.addExtensions(file);

// Option 1: Locally.
WebDriver driver = new SafariDriver(options);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Internet Explorer

Good luck.

这篇关于使用Selenium的浏览器插件测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:41