我在使用 chrome 和 selenium 2 webdriver 时遇到问题。问题是,当我使用 chromedriver 启动浏览器会话时,它总是在一个小于可用屏幕宽度一半的窗口中启动。因为我正在对随屏幕尺寸变化而变化的页面进行测试,所以我的测试失败,因为我试图将页面顶部的元素拖放到页面底部的元素。我收到滚动错误。但如果窗口最大化,那么我就不会收到此错误。但问题是,每次 chrome 通过 chrome 驱动程序启动一个新会话时,它总是在一个小窗口中启动。我探索了许多不同的选项来让浏览器最大化启动:
- 通过 javascript 调用 window.resizeTo(width,height); 来更改窗口的大小。但是,这在更高版本的 chrome(或 Firefox)中不起作用。
- 使用开关 --start-maximized 启动 chrome。这在 Windows 上有效,但在 Mac 上不起作用。
- 使用指定配置文件目录的开关启动 chrome。这样配置文件将定义窗口大小。 --profile-directory=~/chromeprofile 当我从命令行启动 chrome 时,这有效,但如果我从 selenium webdriver/chromedriver 执行此操作,则没有任何区别。
-
当我尝试时: driver.manage().window().setSize()
我得到这个例外:
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)
为了实现这一目标,我还可以探索哪些其他选择?问题是窗户太小?每次在 Mac 上通过 WebDriver 启动 chrome 时,我该怎么做才能自动最大化窗口?
driver.manage().window().setSize() 目前不适用于 ChromeDriver(从 v19 开始)。有一个活性铬问题 http://code.google.com/p/chromium/issues/detail?id=107630与此功能请求相关。
The 当前的解决方法 http://code.google.com/p/selenium/wiki/ChromeDriver#Starting_Chromium_with_Specific_Flags通常建议使用 DesiredCapability 来设置窗口大小,但你是对的,它似乎根本不适用于 Mac。
我发现的唯一解决方案是使用 JavaScript 打开一个新窗口并以相同的方式调整新窗口的大小。里面有深入的描述这个主题的主题 http://groups.google.com/group/webdriver/browse_thread/thread/f6b431e025629674。相关解决方法如下代码所示:
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 本地工作很有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)