Nightwatch测试:将浏览器设置为固定大小

时间:2021-06-19 01:21:50

Is there any way to ensure that the browser does not change from the initial window size. There are several things that are clicked during testing that are causing the window to maximize but i would like it to stay the same size throughout.

有没有办法确保浏览器不会从初始窗口大小更改。在测试期间点击了几个导致窗口最大化的东西,但我希望它始终保持相同的大小。

2 个解决方案

#1


16  

set it up once and for all in your env configuration (under test_settings in the nightwatch config file):

在你的env配置中一劳永逸地设置它(在nightwatch配置文件中的test_settings下):

"desiredCapabilities": {
    "chromeOptions": {
        "args": [
            "window-size=1280,800"
        ]
    }
}

note that this method will work because we're setting a chrome flag, so implementation may vary (e.g. safari does not have such flags).

请注意,此方法将起作用,因为我们正在设置chrome标志,因此实现可能会有所不同(例如safari没有这样的标志)。

for browsers that do not support these options, it's best to resize the window imperatively in the globals beforeEach hook:

对于不支持这些选项的浏览器,最好在全局变量beforeEach钩子中强制调整窗口大小:

{
    beforeEach: function (browser, done) {
        browser.resizeWindow(1280, 800, done);
    }
}

have a read on the nightwatch settings docs to see how globals are used.

阅读nightwatch设置文档,了解全局变量的使用方法。

using the above methods, you won't have to specify it in each test :)

使用上面的方法,你不必在每个测试中指定它:)

#2


10  

You can fix the screen size before each test like that :

您可以在每次测试之前修复屏幕大小:

module.exports = {
  tags: ['myTest'],
  before : function (browser) {
    browser.resizeWindow(800, 600);
  },
  'Test #1' : function (browser) {
    return browser
      .url('http://localhost/test1')
      .waitForElementVisible('body', 2000); 
  },
  'Test #2' : function (browser) {
    return browser
      .url('http://localhost/test2')
      .waitForElementVisible('body', 2000); 
  },
  after : function (browser) {
    browser.end();
  }
}

#1


16  

set it up once and for all in your env configuration (under test_settings in the nightwatch config file):

在你的env配置中一劳永逸地设置它(在nightwatch配置文件中的test_settings下):

"desiredCapabilities": {
    "chromeOptions": {
        "args": [
            "window-size=1280,800"
        ]
    }
}

note that this method will work because we're setting a chrome flag, so implementation may vary (e.g. safari does not have such flags).

请注意,此方法将起作用,因为我们正在设置chrome标志,因此实现可能会有所不同(例如safari没有这样的标志)。

for browsers that do not support these options, it's best to resize the window imperatively in the globals beforeEach hook:

对于不支持这些选项的浏览器,最好在全局变量beforeEach钩子中强制调整窗口大小:

{
    beforeEach: function (browser, done) {
        browser.resizeWindow(1280, 800, done);
    }
}

have a read on the nightwatch settings docs to see how globals are used.

阅读nightwatch设置文档,了解全局变量的使用方法。

using the above methods, you won't have to specify it in each test :)

使用上面的方法,你不必在每个测试中指定它:)

#2


10  

You can fix the screen size before each test like that :

您可以在每次测试之前修复屏幕大小:

module.exports = {
  tags: ['myTest'],
  before : function (browser) {
    browser.resizeWindow(800, 600);
  },
  'Test #1' : function (browser) {
    return browser
      .url('http://localhost/test1')
      .waitForElementVisible('body', 2000); 
  },
  'Test #2' : function (browser) {
    return browser
      .url('http://localhost/test2')
      .waitForElementVisible('body', 2000); 
  },
  after : function (browser) {
    browser.end();
  }
}