如何在 Protractor/WebdriverJS 中设置默认浏览器窗口大小 [英] How to set default browser window size in Protractor/WebdriverJS

查看:38
本文介绍了如何在 Protractor/WebdriverJS 中设置默认浏览器窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当我在工作中运行测试时,浏览器已最大化,但当我在家中运行它们时,它只打开大约 50% 宽度的浏览器窗口.这会导致向下滚动等出现一些差异,因此我理想地希望它在运行测试的每台机器上打开相同大小的浏览器窗口.这样做的最佳方法是什么?

(我为其他语言找到了一些答案,但我无法将它们改编为 JavaScript)

添加

browser.executeScript('window.moveTo(0,0);' +'window.resizeTo(screen.width, screen.height);');

什么都不做(显然 Google Chrome 不支持 window.moveTowindow.resizeTo).

解决方案

您可以通过运行来设置默认浏览器大小:

var 宽度 = 800;变量高度 = 600;browser.driver.manage().window().setSize(width, height);

要最大化浏览器窗口,请运行:

browser.driver.manage().window().maximize();

要设置位置,请运行:

var x = 150;变量 y = 100;browser.driver.manage().window().setPosition(x, y);

如果出现错误:

<块引用>

WebDriverError:未知错误:远程调试不支持操作

<块引用>

使用远程调试时不支持操作 Some WebDriver命令(例如调整浏览器窗口大小)需要 Chrome 扩展程序加载到浏览器中.ChromeDriver 通常会加载这个自动化扩展"每次启动新的 Chrome 会话时.

但是可以指示 ChromeDriver 连接到现有的Chrome 会话而不是启动一个新会话.这是使用Capabilities(又名 ChromeOptions)对象中的debuggerAddress".由于自动化扩展仅在启动时加载,因此有ChromeDriver 在使用时不支持的一些命令通过远程调试现有会话.

如果您看到错误使用远程时不支持操作调试",尝试重写测试以启动新的 Chrome会议.这可以通过从能力对象.

来源:远程调试时不支持操作

For some reason when I run my tests at work the browser is maximized, but when I run them at home it only opens a browser window of about 50% width. This causes some discrepancies with scrolling down, etc., so I'd ideally like to have it open a browser window of the same size on every machine the tests are run on. What's the best way to do this?

(I've found some answers for other languages, but I haven't been able to adapt them to JavaScript)

Adding

browser.executeScript('window.moveTo(0,0);' +
    'window.resizeTo(screen.width, screen.height);');

does nothing (apparently window.moveTo and window.resizeTo are not supported by Google Chrome).

解决方案

You can set the default browser size by running:

var width = 800;
var height = 600;
browser.driver.manage().window().setSize(width, height);

To maximize the browser window, run:

browser.driver.manage().window().maximize();

To set the position, run:

var x = 150;
var y = 100;
browser.driver.manage().window().setPosition(x, y);

If you get an error:

WebDriverError: unknown error: operation is unsupported with remote debugging

Operation not supported when using remote debugging Some WebDriver commands (e.g. resizing the browser window) require a Chrome extension to be loaded into the browser. ChromeDriver normally loads this "automation extension" every time it launches a new Chrome session.

However ChromeDriver can be instructed to connect to an existing Chrome session instead of launching a new one. This is done using 'debuggerAddress' in the Capabilities (aka ChromeOptions) object. Since the automation extension is only loaded at startup, there are some commands that ChromeDriver does not support when working with existing sessions through remote debugging.

If you see the error "operation not supported when using remote debugging", try rewriting the test so that it launches a new Chrome session. This can be done by removing 'debuggerAddress' from the Capabilities object.

Source: Operation not supported when using remote debugging

这篇关于如何在 Protractor/WebdriverJS 中设置默认浏览器窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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