如何设置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 |
+-------------------+----------+-------------------+
如何解决这个问题?我知道我可以使用ChromeOptions
和OperaOptions
将参数传递给驱动程序.但我想在测试执行期间更改大小.如果我不需要评估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(); }
- Chrome: