问题
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 |
+-------------------+----------+-------------------+
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.
回答1:
The are some problems with automation testing in Chrome and Opera browsers.
Issues:
- Chrome:
- Chrome automation extension isn't allowed in incognito mode
- Selenium cannot use Window() functions when in incognito mode
- Opera:
- driver.manage().window().setSize does not work in private mode
I temporary solved them using the code:
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(); }
回答2:
Hi in Google chrome (incog mode) i think re-sizing is not possible.re-sizing in normal instance of google chrome is possible but not with incog mode on.with doing more research i closely observed the error shown in eclipse (incog mode on)
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: No current window
JavaScript stack:
Error: No current window
at checkForExtensionError (chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/background.js:14:17)
at Object.callback (chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/background.js:67:5)
at safeCallbackApply (extensions::sendRequest:21:15)
at handleResponse (extensions::sendRequest:72:7)
(Session info: chrome=49.0.2623.110)
you can see at line checkForExtensionError you will find that why its not possible.
1.please open chrome://extensions/ in chrome and enable developer option then
2.under Chrome Automation Extension you will see the same ID that is shown in the error (posted above).
- but allow in incognito not checked
4.Please open the location of the manifest.json under Loaded from: inside that you will find the below contents
{
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDr+Q7QFcTr4Wmn9sSICKWbxnYLhIM0ERbcapZCDm",
"name": "Chrome Automation Extension",
"version": "1",
"manifest_version": 2,
"description": "Exposes extension APIs for automating Chrome",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs", "management", "<all_urls>"
]
}
under description u can clearly see that
"description": "Exposes extension APIs for automating Chrome",
Hence if Point 3 is not enabled it is not possible to automate anything with webdriver in chrome.
Also i do not know how to enable point 3 via automation if you can then you can surely resize window in incog mode. hope this helps
回答3:
This is an alternative to set the size of the window once the browser is launched:
// open a new window to the desired size
((JavascriptExecutor) driver).executeScript(
"window.open(window.location.href, 'mywindow', 'height=400,width=400');");
// close the current window
driver.close();
// set the context to the new window
driver.switchTo().window("mywindow");
// the new window is now resizeable by JavaScript
((JavascriptExecutor)driver).executeScript("window.resizeTo(800,600);");
回答4:
Hi we can set the size of the browser using the dimension class.The dimension class provides the method called .using it we can do . Example for setting the size of is :
Dimension d = new Dimension(420,600);
//Resize the current window to the given dimension
driver.manage().window().setSize(d);
来源:https://stackoverflow.com/questions/36334134/how-to-set-browser-window-size-for-chrome-and-opera-launched-by-selenium-webdriv