При проходе тестов и получении ошибки от сервера отображается алерт и случается эксепшн unexpected alert open, который роняет сборку TestNG и:
- весь проход становится зеленым даже при наличии ошибок
- часть тестов после этого эксепшна не выполняется
Как правильно отлавливать данное исключение, если я не знаю, в какой именно момент может эта штука выстрелить?
Как сделать так, чтобы если эксепшн выстрелил, сборка с тестами продолжала ехать?
Я добавил в capabilities драйвера обработку алертов: unexpectedAlertBehaviour=accept и в метод класса BasePage так же добавил обработку ситуации с исключением.
Код конструктора:
public BasePage(boolean openPageByUrl, ProfileDataProvider profileDataProvider){
if(openPageByUrl) {
String urlPrefix = profileDataProvider.getTestProfile().getUrlPrefix();
openPage(urlPrefix);
WebDriverFactory.getDriver().manage().window().maximize();
}
PageFactory.initElements(new HtmlElementDecorator(new HtmlElementLocatorFactory(getDriver())), this);
waitForOpen();
}
Код метода с обработкой исключения, т.к. какое-то время оно случалось при открытии следующей страницы после заполнения формы
public void waitForOpen() {
try {
int secondsCount = 0;
boolean isPageOpenedIndicator = isPageOpened();
while (!isPageOpenedIndicator && secondsCount < WAIT_FOR_PAGE_LOAD_IN_SECONDS) {
TimeUtils.waitForSeconds(1);
secondsCount++;
isPageOpenedIndicator = isPageOpened();
}
if(!isPageOpenedIndicator) {
throw new AssertionError("Page was not opened");
}
} catch (org.openqa.selenium.UnhandledAlertException exception) {
Alert alert = getDriver().switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert was catched and accepted: " + alertText);
alert.accept();
}
}
Логи и ошибка вот такая:
Please refer to C:\Program Files (x86)\Jenkins\workspace\selenium-seven\target\surefire-reports for the individual test results.
Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
There was an error in the forked process
unexpected alert open: {Alert text : Произошла ошибка при проверке номера. Пожалуйста, попробуйте позднее.}
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'motestrunner', ip: '10.91.13.193', os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\SRV_JE~1\AppData\Local\Temp\scoped_dir6696_23207}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.132, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=accept}]
Session ID: 1de9439de2d80ea4a34928f1bddb2692
org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
unexpected alert open: {Alert text : Произошла ошибка при проверке номера. Пожалуйста, попробуйте позднее.}
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'motestrunner', ip: '10.91.13.193', os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\SRV_JE~1\AppData\Local\Temp\scoped_dir6696_23207}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.132, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=accept}]
Session ID: 1de9439de2d80ea4a34928f1bddb2692
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:665)
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:533)
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:279)
at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:243)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1077)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:907)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:785)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.jvnet.hudson.maven3.launcher.Maven32Launcher.main(Maven32Launcher.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchStandard(Launcher.java:330)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:238)
at jenkins.maven3.agent.Maven32Main.launch(Maven32Main.java:186)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at hudson.maven.Maven3Builder.call(Maven3Builder.java:136)
at hudson.maven.Maven3Builder.call(Maven3Builder.java:71)
at hudson.remoting.UserRequest.perform(UserRequest.java:153)
at hudson.remoting.UserRequest.perform(UserRequest.java:50)
at hudson.remoting.Request$2.run(Request.java:336)
at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:68)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Версии ОС и софта следующие:
<testng.version>6.8.17</testng.version>
<selenium.version>3.4.0</selenium.version>
<compiler.version>1.8</compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aspectj.version>1.8.10</aspectj.version>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
Браузер: Google Chrome 63.0.3239.132 x64
OS: Windows Server 2008 R2 Standart