java - Mac selenium webdriver chrome 窗口总是以小窗口开头

标签 java selenium webdriver

我在使用 chrome 和 selenium 2 webdriver 时遇到问题。问题是,当我使用 chromedriver 启动浏览器 session 时,它总是在小于可用屏幕宽度一半大小的窗口中启动。因为我在屏幕尺寸改变时改变的页面上进行测试,所以我的测试失败了,因为我试图从页面顶部拖动一个元素并将其放到页面底部的元素。我收到滚动错误。但是如果窗口被最大化,那么我就不会得到这个错误。但问题是,每次 chrome 通过 chrome 驱动程序启动新 session 时,它总是在一个小窗口中启动。我探索了许多不同的选项来让浏览器以最大化方式启动:

  • 通过 javascript 调用以使用 window.resizeTo(width,height); 更改窗口的大小。但是,这在更高版本的 chrome(或 firefox 就此而言)中不起作用。
  • 使用开关启动 chrome --start-maximized。这适用于 Windows,但在 Mac 上不起作用。
  • 使用指定配置文件目录的开关启动 chrome。这样,配置文件将定义窗口大小。 --profile-directory=~/chromeprofile 这在我从命令行启动 chrome 时有效,但如果我从 selenium webdriver/chromedriver 执行此操作,则没有任何区别。
  • 当我尝试:driver.manage().window().setSize() 我得到这个异常(exception):

    INFO: Executing: [93debf43cf70ad3557442a7e1aee4620, setWindowSize {"windowHandle":"current","width":2560,"height":1440}]
    org.openqa.selenium.UnsupportedCommandException: [GET, HEAD, DELETE]
    Command duration or timeout: 16 milliseconds
    Build info: version: '2.15.0', revision: '15105', time: '2011-12-08 09:56:25'
    System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.2', java.version: '1.6.0_29'
    Driver info: driver.version: RemoteWebDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:424)
        at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.setSize(RemoteWebDriver.java:578)
        at com.domo.automation.framework.utility.WebDriverUtil.startWebDriver(WebDriverUtil.java:36)
        at com.domo.automation.tests.DomoWebDriverTestCase.setUp(DomoWebDriverTestCase.java:45)
        at junit.framework.TestCase.runBare(TestCase.java:132)
        at junit.framework.TestResult$1.protect(TestResult.java:110)
        at junit.framework.TestResult.runProtected(TestResult.java:128)
        at junit.framework.TestResult.run(TestResult.java:113)
        at junit.framework.TestCase.run(TestCase.java:124)
        at junit.framework.TestSuite.runTest(TestSuite.java:243)
        at junit.framework.TestSuite.run(TestSuite.java:238)
        at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    

为了实现这一目标,我还可以探索哪些其他选择?问题是 window 太小了?每次通过 Mac 上的 webdriver 启动 chrome 时,我该怎么做才能自动最大化窗口?

最佳答案

driver.manage().window().setSize() 当前不适用于 ChromeDriver(从 v19 开始)。有一个 active chromium issue与此功能请求相关。

current workaround通常推荐的是使用 DesiredCapabilities 来设置窗口大小,但你是对的,它似乎在 Mac 上根本不起作用。

我找到的唯一解决方案是使用 JavaScript 打开一个新窗口并以相同的方式调整新窗口的大小。在 this thread on the topic 中对其进行了深入描述.相关解决方法如下面的代码所示:

JavascriptExecutor js = ((JavascriptExecutor)driver);
js.executeScript("window.open('','testwindow','width=400,height=200')");
driver.close();
driver.switchTo().window("testwindow");
js.executeScript("window.moveTo(0,0);");
js.executeScript("window.resizeTo(1280,800);");

恐怕它很笨重,但它在 Mac (Lion) 上使用 chrome 在本地对我有用。

关于java - Mac selenium webdriver chrome 窗口总是以小窗口开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607753/

相关文章:

java - WebDriver:executeScript() 导致 BindException

java - java中的递归搜索

java - 将 .ttf 字体包含到使用 Flying Saucer 和 Spring-Boot 生成的 pdf 中

unit-testing - 使用<unitTestProvider>的Visual Studio 2012中的Specflow错误

html - 在 Selenium IDE 中断言元素颜色

java - 我已经尝试了所有可能的方法来使用 Webdriver 定位元素,但出现错误 "Unable to locate Element"

java - yarn 异常 : Unauthorized request to start container

java - spring-data-jpa 存储库在查询中使用 Collection 作为 @Param

Selenium WebDriver 如何关闭浏览器弹出窗口

python - 在 fedora 上安装 python selenium 绑定(bind)时出错