Java 在 Selenium 测试中指定自定义屏幕分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18596732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:21:12  来源:igfitidea点击:

Specifying custom screen resolution in Selenium tests

javaseleniumwebdriverscreen-resolution

提问by Srikanth Nakka

As mentioned in the below blog, we can modify screen resolution during selenium test runs. http://blog.testingbot.com/2013/03/15/screen-resolution-option-now-available-for-all-selenium-tests

如下文所述,我们可以在 selenium 测试运行期间修改屏幕分辨率。 http://blog.testingbot.com/2013/03/15/screen-resolution-option-now-available-for-all-selenium-tests

Tried the below code(as mentioned in "https://saucelabs.com/docs/additional-config"), but not setting the specified resolution. Is this still not available for Selenium?

尝试了以下代码(如“ https://saucelabs.com/docs/additional-config”中所述),但未设置指定的分辨率。这仍然不适用于 Selenium 吗?

DesiredCapabilities dc=new DesiredCapabilities();    
dc.setCapability("screen-resolution","1280x1024");

采纳答案by Ardesco

Sauce Labs != Selenium

Sauce Labs != Selenium

Sauce labs use that capability to provision you a VM with the desired resolution, it's not a capability that Selenium itself knows about.

Sauce 实验室使用该功能为您提供具有所需分辨率的 VM,Selenium 本身并不了解该功能。

Selenium is not capable of modifying your desktop resolution!

Selenium 无法修改您的桌面分辨率!

If you want to modify your browser size in Selenium so that it matches a specific resolution you can do a:

如果您想在 Selenium 中修改浏览器大小以使其匹配特定分辨率,您可以执行以下操作:

driver.manage().window().setSize(new Dimension(1024, 768))

The above is not supported with Opera driver, so instead you would need to do:

Opera 驱动程序不支持上述内容,因此您需要执行以下操作:

DesiredCapabilities capabilities = DesiredCapabilities.opera()
capabilities.setCapability("opera.arguments", "-screenwidth 1024 -screenheight 768")

While setting the browser size is not the same as setting the screen resolution, it should for all intents and purposes meet your requirements.

虽然设置浏览器大小与设置屏幕分辨率不同,但它应该满足您的所有意图和目的。

回答by Dingredient

The purpose of DesiredCapabilitiesis to tell the Grid where to run your tests.

的目的DesiredCapabilities是告诉网格在哪里运行您的测试。

So, if there is a remote node connected to the Grid with the resolution you specified 1280x1024, the test will run on that node. If any other nodes do not have this resolution, the test will not run on those nodes.

因此,如果有一个远程节点以您指定的分辨率连接到网格1280x1024,则测试将在该节点上运行。如果任何其他节点没有此分辨率,则不会在这些节点上运行测试。

If you do not specify a screen resolution in the DesiredCapabilities, the test will run on nodes with any resolution.

如果您未在 中指定屏幕分辨率DesiredCapabilities,则测试将在具有任何分辨率的节点上运行。

This feature of Selenium does not actually change or modify a testing node's screen resolution. It only tells the Grid on which nodes to run or not run your tests.

Selenium 的此功能实际上不会更改或修改测试节点的屏幕分辨率。它只告诉网格在哪些节点上运行或不运行测试。

回答by Stephen B.

What you are interested in doing is probably best done through native java code rather than through Selenium which is a web browser testing framework.

您感兴趣的事情可能最好通过本机 java 代码完成,而不是通过 Selenium 完成,后者是一个 Web 浏览器测试框架。

The question of changing the resolution through Java was addressed in another thread here on StackOverflow.

StackOverflow 上的另一个线程解决了通过 Java 更改分辨率的问题。

swing - Change screen resolution in Java

Swing - 在 Java 中更改屏幕分辨率

Although I am curious as to why you are trying to do this, since you didn't explain that above and may lead to a better response from the community. =)

虽然我很好奇您为什么要这样做,因为您没有在上面解释这一点,并且可能会导致社区做出更好的反应。=)

回答by JacekM

Selenium is a browser automation framework, its job is to drive a browser, not to automate your system. I don't think that a functionality such as setting a screen resolution will ever be implemented in Selenium. Setting resolution has simply nothing to do with browser automation.

Selenium 是一个浏览器自动化框架,它的工作是驱动浏览器,而不是自动化你的系统。我不认为像设置屏幕分辨率这样的功能会在 Selenium 中实现。设置分辨率与浏览器自动化无关。

I am not sure why do you want to change the resolution... How about just changing the size of your browser window? That's something you could do if you are testing responsive-designed pages.

我不知道您为什么要更改分辨率...仅更改浏览器窗口的大小如何?如果您正在测试响应式设计的页面,那么您可以这样做。

driver.manage().window().setSize(new Dimension(800, 600));