一尘不染

用selenium浏览器插件测试

selenium

我正在编写一个webapp,其中包含适用于Firefox和chrome的浏览器插件组件。我当前的测试系统使用通过Selenium
IDE创建的一系列Selenium测试。

是否可以为Firefox和chrome(也可能是其他浏览器)安装,激活和删除selenium浏览器插件?

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

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


阅读 682

收藏
2020-06-26

共1个答案

一尘不染

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

Chrome和Firefox WebDriver支持远程安装扩展。以下是Chrome和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);

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

歌剧

此API与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);

苹果浏览器

此API与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);

IE浏览器

祝好运。

2020-06-26