如何设置浏览器内窗口/视口大小? [英] How do you set the browser inner window/viewport size?
问题描述
如果我调用该函数:
browser.driver.manage().window().setSize(1000, 1000);
它将我的窗口大小设置为1000x1000和我的内部窗口/ viewport size 至990x918。通过内部窗口大小,我指的是窗口中实际包含内容的部分,例如,不包括窗口边框或制表符。在这种情况下,我的每边都有一个5px的边框,然后是我想要的82px的网址和标签栏。
It sets my window size to 1000x1000 and my inner window/viewport size to 990x918. By inner window size, I mean the portion of the window that actually contains the content, not including window borders or tabs, for example. In this case, I have a 5px border on each side, and then 82px worth of url and tab bars that I would like to account for.
我想要设置内部窗口大小,以便我不需要专门为运行测试的机器负责,例如它是否恰好有一个额外的工具栏。
I'd like to set the inner window size so that I dont need to potentially account specifically for the machine running the tests, should it happen to have an extra toolbar for example.
是否有用于设置窗口实际内部内容填充部分大小的量角器命令?
Is there a protractor command for setting the size of the actual inner, content-filled portion of the window?
根据答案下面,我把它添加到我的conf的onPrepare部分:
As based on the answer below, I added this to the onPrepare section of my conf:
protractor.setViewportSize = function(width, height){
const JS_GET_PADDING = "return {"
+ "w: window.outerWidth - window.innerWidth,"
+ "h: window.outerHeight - window.innerHeight };";
browser.executeScript(JS_GET_PADDING).then(function(pad){
browser.manage().window().setSize(width + pad.w, height + pad.h);
});
};
在测试中,我称之为:
protactor.setViewportSize(1440, 1000);
推荐答案
我认为没有任何内置功能将视口大小调整为给定大小的方法。但是,可以通过设置与目标内部大小匹配的外部大小来完成:
I don't think that there's any build-in method to resize the viewport to a given size. However it can be done by setting an outter size that will match the targeted inner size:
var webdriver = require('./node_modules/protractor/node_modules/selenium-webdriver');
// extension method to set the inner size
webdriver.WebDriver.prototype.setViewportSize = function(width, height){
const JS_GET_PADDING = "return {"
+ "w: window.outerWidth - window.innerWidth,"
+ "h: window.outerHeight - window.innerHeight };";
var self = this;
self.executeScript(JS_GET_PADDING).then(function(pad){
self.manage().window().setSize(width + pad.w, height + pad.h);
});
};
// start the browser
var driver = new webdriver.Builder().withCapabilities({browserName: 'firefox'}).build();
// set the window inner size to 800 x 600
driver.setViewportSize(800, 600);
// quit
driver.sleep(5000);
driver.quit();
这篇关于如何设置浏览器内窗口/视口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!