如何设置Selenium WebDriver在隐身/私有模式下启动的Chrome和Opera的浏览器窗口大小? [英] How to set browser window size for Chrome and Opera launched by Selenium WebDriver in incognito/private mode?

查看:375
本文介绍了如何设置Selenium WebDriver在隐身/私有模式下启动的Chrome和Opera的浏览器窗口大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发可在多种浏览器类型(Chrome,FireFox,Internet Explorer和Opera)上执行测试的应用程序.我找到了如何以隐身/私有模式启动它们的方法(如何使用Selenium WD为不同的浏览器类型打开隐身/私有窗口?)并设置窗口大小(浏览器窗口控件#174 ):

I'm working on application which will execute tests on multiple browser types (Chrome, FireFox, Internet Explorer and Opera). I found the way how to launched them in incognito/private modes (How to open incognito/private window with Selenium WD for different browser types?) and set window size (Browser window control #174):

Window window = driver.manage().window();
window.setPosition(new Point(0, 0));
window.setSize(new Dimension(width, height));

但是此代码并非在所有情况下都有效:

But this code does not work in all cases:

+-------------------+----------+-------------------+
|      Browser      | Standard | Incognito/Private |
|-------------------|----------|-------------------|
|      Chrome       |  works   |   does not work   |
|-------------------|----------|-------------------|
|      FireFox      |  works   |       works       |
|-------------------|----------|-------------------|
| Internet Explorer |  works   |       works       |
|-------------------|----------|-------------------|
|      Opera        |  works   |   does not work   |
+-------------------+----------+-------------------+

如何解决这个问题?我知道我可以使用ChromeOptionsOperaOptions将参数传递给驱动程序.但我想在测试执行期间更改大小.如果我不需要评估JavaScript,那就太好了.

How to solve this problem? I know that I can pass arguments to drivers using ChromeOptions and OperaOptions. But I would like to change size during tests executions. It will be great if I don't need to eval JavaScript.

推荐答案

Chrome和Opera浏览器中的自动化测试存在一些问题.

The are some problems with automation testing in Chrome and Opera browsers.

问题:

  • Chrome:
    • Chrome:
      • Chrome automation extension isn't allowed in incognito mode
      • Selenium cannot use Window() functions when in incognito mode

      我使用代码临时解决了这些问题:

      I temporary solved them using the code:

      • Chrome

      • Chrome

      ChromeDriver driver = new ChromeDriver(capabilities);
      driver.get("chrome://extensions-frame");
      WebElement checkbox = driver.findElement(By.xpath("//label[@class='incognito-control']/input[@type='checkbox']"));
      if (!checkbox.isSelected()) {
          checkbox.click();
      }
      

    • 歌剧:

    • Opera:

      OperaDriver driver = new OperaDriver(capabilities);
      driver.get("chrome://extensions");
      WebElement checkbox = driver.findElement(By.xpath("//div[contains(@class, 'incognito-control')]/label/input[@type='checkbox']"));
      if (!checkbox.isSelected()) {
          checkbox.click();
      }
      

    • 这篇关于如何设置Selenium WebDriver在隐身/私有模式下启动的Chrome和Opera的浏览器窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆