Selenium测试在远程机器上失败,因为chrome和驱动程序

时间:2015-09-08 09:35:02

标签: selenium selenium-webdriver webdriver testng selenium-grid

我正在使用带有testng的selenium网格进行分布式测试。 我想在chrome和IE浏览器上运行远程机器上的测试。但是测试在这两个浏览器上都失败了。在Firefox上运行测试时,运行成功。

以下是用于初始化浏览器的代码:

DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(URL),cap);
driver.get(myURL);

浏览器已成功初始化。找到元素和发送键时测试失败。 找到元素的代码是:

driver.findElement(By.xpath(Xpath_Username)).sendKeys("abc");

我使用我的机器作为集线器而另一台机器作为节点。使用以下命令将节点连接到集线器:

java -jar selenium-server-standalone-2.47.1.jar -role webdriver -hub http://*myip*:4444/grid/register -port 8989 -browser browserName="chrome",version=ANY,platform=VISTA,maxInstances=5 -Dwebdriver.chrome.driver=D:\chromedriver.exe -browser browserName="internet explorer",version=ANY,platform=VISTA,maxInstances=2 -Dwebdriver.ie.driver=D:\IEDriverServer.exe

我正在不同的机器和浏览器上使用testng并行执行测试。 测试的输出:

Running TestSuite
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 7.068 sec <<< FAILURE! - in TestSuite
remoteTest(testproject.testclass)  Time elapsed: 2.313 sec  <<< FAILURE!
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 13 milliseconds
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: hostname, ip: 'ip', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version
ntCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: f3667158-1a65-4c24-866a-3d1c5444918b
*** Element info: {Using=xpath, value=//input[@id='login']}
Command duration or timeout: 214 milliseconds
Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
System info: host: 'hostname', ip: 'ip', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version
criptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: d93bfa55-13b2-427f-b11d-fca5417a0d91
*** Element info: {Using=xpath, value=//input[@id='login']}
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:138)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
        at testproject.Browser.login(Browser.java:44)
        at testproject.testclass.remoteTest(testclass.java:10)
Caused by: org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 13 milliseconds
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'hostname', ip: 'ip', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version
ntCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: f3667158-1a65-4c24-866a-3d1c5444918b
*** Element info: {Using=xpath, value=//input[@id='login']}
Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
System info: host: 'hostname', ip: 'ip', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: driver.version: EventFiringWebDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
        at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:102)
        at com.sun.proxy.$Proxy3.findElement(Unknown Source)
        at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:185)
        at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:48)
        at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:1)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)


Results :

Failed tests:
  testclass.remoteTest:10->Browser.login:44 ? NoSuchWindow Unable to find elemen...

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.

Please refer to D:\svn_workspace\SeleniumGrid\target\surefire-reports for the individual test results.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11 seconds
[INFO] Finished at: Tue Sep 08 14:45:39 IST 2015
[INFO] Final Memory: 38M/260M
[INFO] ------------------------------------------------------------------------

请提供任何解决方案。

2 个答案:

答案 0 :(得分:0)

通过添加以下代码解决了这个问题:

cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

此外,必须在Internet Explorer中启用保护模式,并且所有区域都应该相同。

答案 1 :(得分:-3)

element = findElement(By.xpath(“// * input [@ id ='login'”));