为什么' UnhandledAlertException'还在输出到控制台窗口?
我有下面列出的方法,它们使用TestNG'之前'和'之后'关闭模态弹出窗口的方法, 但是为什么' UnhandledAlertException'即使我忽略未处理的警报异常,仍然会输出到控制台窗口。
TestNG调用方法:
@BeforeMethod(alwaysRun = true)
public void closeModalPopup() throws Exception {
basePage.closeModalPopup();
}
@AfterMethod(alwaysRun = true)
public void closeModalPopup2() throws Exception {
basePage.closeModalPopup();
}
为关闭弹出窗口等而创建的主要方法:
public void waitUntilModalDisapears() {
WebDriverWait tempWait = new WebDriverWait(this.driver, 60);
try {
tempWait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));
} catch (UnhandledAlertException e) {
// Do nothing
} catch (StaleElementReferenceException e) {
// do nothing
} catch (NullPointerException e) {
// do nothing
} catch (Exception e) {
// do nothing
}
}
/** Modal Closer **/
public void closeModalPopup() throws InterruptedException {
int attempts = 0;
while (attempts < 10) {
try {
jsExecutor.executeScript("var p=document.querySelector('.modal.fade.in button.close span'); if ( p) p.click();");
} catch (UnhandledAlertException e) {
//Do nothing
} catch (StaleElementReferenceException e) {
//do nothing
} catch (NullPointerException e) {
// do nothing
} catch (Exception e) {
// do nothing
}
attempts++;
waitUntilModalDisapears();
}
}
Jenkins控制台输出:
11:48:48 Jun 01, 2017 11:48:48 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
11:48:48 WARNING: WebDriverException thrown by findElement(By.cssSelector: .modal-body)
11:48:48 org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : Click OK to confirm your personal message is correct as this is exactly how it will be printed.}
11:48:48 (Session info: chrome=58.0.3029.110)
11:48:48 (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
11:48:48 Command duration or timeout: 31 milliseconds: null
11:48:48 Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
11:48:48 System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
11:48:48 Driver info: org.openqa.selenium.chrome.ChromeDriver
11:48:48 Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\3\scoped_dir6676_5813}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=ignore}]
11:48:48 Session ID: 738b699457547ad20f328acd1580afab
11:48:48 *** Element info: {Using=css selector, value=.modal-body}
11:48:48 at sun.reflect.GeneratedConstructorAccessor17.newInstance(Unknown Source)
11:48:48 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
11:48:48 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11:48:48 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
11:48:48 at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:173)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:437)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.findElementsByCssSelector(RemoteWebDriver.java:505)
11:48:48 at org.openqa.selenium.By$ByCssSelector.findElements(By.java:441)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:398)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:882)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions$20.apply(ExpectedConditions.java:580)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions$20.apply(ExpectedConditions.java:576)
11:48:48 at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
11:48:48 at BuyAGiftFramework.pageObjects.Base_Page.waitUntilModalDisapears(Base_Page.java:706)
11:48:48 at BuyAGiftFramework.pageObjects.Base_Page.closeModalPopup(Base_Page.java:734)
11:48:48 at BuyAGiftFramework.utilities.BrowserFactory.closeModalPopup2(BrowserFactory.java:417)
11:48:48 at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
11:48:48 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:48:48 at java.lang.reflect.Method.invoke(Method.java:498)
11:48:48 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
11:48:48 at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
11:48:48 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
11:48:48 at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
11:48:48 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
11:48:48 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
11:48:48 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
11:48:48 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
11:48:48 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
11:48:48 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
11:48:48 at java.lang.Thread.run(Thread.java:748)
11:48:48
11:48:48 Jun 01, 2017 11:48:48 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
11:48:48 WARNING: WebDriverException thrown by findElement(By.cssSelector: .modal-body)
11:48:48 org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : Click OK to confirm your personal message is correct as this is exactly how it will be printed.}
11:48:48 (Session info: chrome=58.0.3029.110)
11:48:48 (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
11:48:48 Command duration or timeout: 0 milliseconds: null
11:48:48 Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
11:48:48 System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
11:48:48 Driver info: org.openqa.selenium.chrome.ChromeDriver
11:48:48 Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\3\scoped_dir6676_5813}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=ignore}]
11:48:48 Session ID: 738b699457547ad20f328acd1580afab
11:48:48 *** Element info: {Using=css selector, value=.modal-body}
11:48:48 at sun.reflect.GeneratedConstructorAccessor17.newInstance(Unknown Source)
11:48:48 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
11:48:48 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11:48:48 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
11:48:48 at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:173)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:437)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.findElementsByCssSelector(RemoteWebDriver.java:505)
11:48:48 at org.openqa.selenium.By$ByCssSelector.findElements(By.java:441)
11:48:48 at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:398)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:882)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions$20.apply(ExpectedConditions.java:580)
11:48:48 at org.openqa.selenium.support.ui.ExpectedConditions$20.apply(ExpectedConditions.java:576)
11:48:48 at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
11:48:48 at BuyAGiftFramework.pageObjects.Base_Page.waitUntilModalDisapears(Base_Page.java:706)
11:48:48 at BuyAGiftFramework.pageObjects.Base_Page.closeModalPopup(Base_Page.java:734)
11:48:48 at BuyAGiftFramework.utilities.BrowserFactory.closeModalPopup(BrowserFactory.java:412)
11:48:48 at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
11:48:48 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:48:48 at java.lang.reflect.Method.invoke(Method.java:498)
11:48:48 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
11:48:48 at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
11:48:48 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
11:48:48 at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
11:48:48 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
11:48:48 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
11:48:48 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
11:48:48 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
11:48:48 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
11:48:48 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
11:48:48 at java.lang.Thread.run(Thread.java:748)
11:48:48
答案 0 :(得分:3)
可能在你的catch {UnhandledAlertException e)块中通过提供:
来处理它 Alert alert = driver.switchTo().alert();
alert.accept();