我有一个框架,我正在运行我的测试;任何错误都会导致WebDriver实例调用类似handleexception方法的东西,我调用driver.quit()方法。我觉得driver.quit()试图关闭浏览器但是然后浏览器抛出一个带有消息的对话框你想要离开这个页面 - 这个没有被处理。抛出没有异常,但是剩下的测试用例在启动浏览器时不起作用,但抛出模态对话框存在异常,这是可以理解的。 我该如何解决这个问题?
handleexception方法:
Driver.getFailedTestCaseNames().add(this.getTestcaseName());
String failedStepStr = "";
failedStepStr = "Test Case failed while executing step " + stepNo + " - Method Name:" + t.getMethodnm() + ", Field Name:" + t.getField() + " , Field value:" + t.getValue();
LOG.error("Test Case Name: " + testcaseName + " failed while executing step " + stepNo + " - Method Name:" + t.getMethodnm() + ", Field Name:" + t.getField() + " , Field value:"
+ t.getValue() + ExceptionUtils.getStackTrace(th));
CaptureScreenshot objCapturScreenshot = new CaptureScreenshot();
objCapturScreenshot.caputureImage(testcaseName);
System.out.println(" in handle exception");
driver.quit();
System.out.println("Test case failed : "+this.getTestcaseName());
throw new TestAutomationException(failedStepStr, th);
有罪测试用例的日志是:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_11'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:423)
at com.bmc.core.test.domain.TestCase.handleException(TestCase.java:364)
at com.bmc.core.test.domain.TestCase.execute(TestCase.java:136)
at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:768)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1022)
at org.testng.TestNG.privateMain(TestNG.java:1325)
at org.testng.TestNG.main(TestNG.java:1294)
Caused by: org.openqa.selenium.WebDriverException: java.net.SocketTimeoutException: Read timed out
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_11'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:75)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
... 26 more
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:319)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:298)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66)
... 27 more
后续测试用例的日志是:
com.bmc.core.test.exception.TestAutomationException: Test Case failed while executing step 1 - Method Name:waitAndSwitchToWindow,
Field Name:Inquira Information Manager , Field value:20000
at com.bmc.core.test.domain.TestCase.handleException(TestCase.java:400)
at com.bmc.core.test.domain.TestCase.execute(TestCase.java:136)
at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:768)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1022)
at org.testng.TestNG.privateMain(TestNG.java:1325)
at org.testng.TestNG.main(TestNG.java:1294)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.bmc.core.test.domain.TestCase.execute(TestCase.java:99)
... 22 more
Caused by: org.openqa.selenium.UnhandledAlertException: Modal dialog present:
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_11'
Session ID: ac1a28e7-2c24-443f-9c0e-d308645fab2d
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true,
ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss,
version=10, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=false, cssSelectorsEnabled=true, requireWindowFocus=false,
initialBrowserUrl=http://www.localohst:10879/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0,
ie.browserCommandLineSwitches=, takesScreenshot=true}]
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:191)
at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:172)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
at org.openqa.selenium.remote.RemoteWebDriver.getWindowHandles(RemoteWebDriver.java:432)
at com.bmc.core.test.app.Base.waitAndSwitchToWindow(Base.java:278)
... 27 more
答案 0 :(得分:1)
在保罗的建议之后我想出了这个片段,这个工作正常....它会在退出时关闭所有浏览器,无论他们是否在关闭时抛出警报......
try
{
Set<String> windowIterator = driver.getWindowHandles();
Iterator<String> iter = windowIterator.iterator();
System.out.println("no of windows to close ="+windowIterator.size());
LOG.debug("no of windows to close ="+windowIterator.size());
int i=0;
while (iter.hasNext())
{
try
{
i++;
System.out.println("lopp value = "+i);
LOG.debug("loop value = "+i);
String windowHandle = iter.next();
driver.switchTo().window(windowHandle);
String windowTitle = driver.getTitle();
System.out.println("title="+driver.getTitle()+" - attempting to close this window");
LOG.debug("title="+driver.getTitle()+" - attempting to close this window");
driver.close();
Thread.sleep(5000);
while(isAlertPresent())
{
try
{
driver.switchTo().alert().accept();
System.out.println("alert accepted after close");
LOG.debug("alert accepted after close");
Thread.sleep(5000);
}
catch (Exception e)
{
e.printStackTrace();
LOG.debug("alert loop... inner while --> exception : "+e.getMessage());
}
}
System.out.println("title="+windowTitle+" - window closed");
LOG.debug("title="+windowTitle+" - window closed");
}
catch (Exception e)
{
e.printStackTrace();
LOG.debug("window loop... outer while --> exception : "+e.getMessage());
}
}
Thread.sleep(5000);
while(isAlertPresent())
{
driver.switchTo().alert().accept();
System.out.println("alert accepted before quit");
LOG.debug("alert accepted before quit");
Thread.sleep(5000);
}
}
catch (Exception e)
{
e.printStackTrace();
LOG.debug("outer exception : "+e.getMessage());
}
driver.quit();
System.out.println("quitting the driver instance");
LOG.debug("quitting the driver instance");
//==================================================================================
答案 1 :(得分:0)
driver.switchTo().alert().accept();
上面的代码应该在出现后关闭对话框。