java - Webdriver:为什么我仍然得到 'Unexpected Alert Open'?

标签 java selenium jenkins selenium-webdriver webdriver

我创建了以下方法,单击特定按钮将打开警报,下面列出的相同方法将处理警报。

但我似乎得到了奇怪的 Jenkins 构建,它将(间歇性地) 由于意外警报打开(下面列出的异常)而失败,有什么想法吗?

public void clickPaySecurelyNowButton() throws InterruptedException {
        waitAndclickElementUsingJS(button_PaySecurelyNowTop);

        WebDriverWait tempWait = new WebDriverWait(this.driver, 30);
        Alert alert = tempWait.until(ExpectedConditions.alertIsPresent());
        boolean boolAlert = false;
        int attempts = 0;
        while (!boolAlert && attempts < 1000) {
            try {
                driver.switchTo().alert().accept();
                System.out.println("Successfully clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
                boolAlert = true;
            } catch (org.openqa.selenium.UnhandledAlertException e) {
                driver.switchTo().alert().accept();
                boolAlert = true;
                System.out.println("Sucesffuly clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
            } catch (Exception e) {
                System.out.println("Unable to click on the 'Pay Securely Now Button', Exception: " + e.getMessage());
                Assert.fail("Method failed: clickPaySecurelyNowButton");
            }
        } attempts++;
    }

异常标记:

unexpected alert open
  (Session info: chrome=58.0.3029.110)
  (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)
Command duration or timeout: 63 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
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'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, 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=}]
Session ID: 699ee1257882397cd7695aeaf919c68f
*** Element info: {Using=xpath, value=//button[contains(text(), "Pay Securely Now")]}



org.openqa.selenium.UnhandledAlertException: 
unexpected alert open
  (Session info: chrome=58.0.3029.110)
  (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)
Command duration or timeout: 63 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
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'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, 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=}]
Session ID: 699ee1257882397cd7695aeaf919c68f
*** Element info: {Using=xpath, value=//button[contains(text(), "Pay Securely Now")]}
    at BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest.clickOn_PaySecurelyNowButton(AddingDrivingExperienceToBasketTest.java:51)






TEST CLASS END TIME: 03.10.30
0
0
Tests run: 314, Failures: 80, Errors: 0, Skipped: 214, Time elapsed: 62.972 sec <<< FAILURE! - in TestSuite
clickOn_PaySecurelyNowButton(BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest)  Time elapsed: 0.298 sec  <<< FAILURE!
org.openqa.selenium.UnhandledAlertException: 
unexpected alert open
  (Session info: chrome=58.0.3029.110)
  (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)
Command duration or timeout: 63 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
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'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, 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=}]
Session ID: 699ee1257882397cd7695aeaf919c68f
*** Element info: {Using=xpath, value=//button[contains(text(), "Pay Securely Now")]}
        at BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest.clickOn_PaySecurelyNowButton(AddingDrivingExperienceToBasketTest.java:51)

EmailFormatAndDataCapture(BuyAGiftFramework.complete.AddingDrivingExperienceToBasketTest)  Time elapsed: 0.299 sec  <<< FAILURE!
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.}
  (Session info: chrome=58.0.3029.110)
  (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)
Command duration or timeout: 4 milliseconds: null
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
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'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\GIANNI~1.BRU\AppData\Local\Temp\2\scoped_dir9140_30313}, 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,

最佳答案

在这一行中,您已经收到警报:Alert alert = tempWait.until(ExpectedConditions.alertIsPresent());

与其编写 driver.switchTo().alert().accept();,不如尝试这样做:alert.accept();

更新:
如果您只想不惜一切代价接受警报,最好将您的 try block 移到点击线之外。我有充分的理由相信点击行会导致您未处理的警报异常,因为我之前遇到过同样的问题。

尝试像这样更新您的代码:

public void clickPaySecurelyNowButton() throws InterruptedException {

    try {
        waitAndclickElementUsingJS(button_PaySecurelyNowTop);

        WebDriverWait tempWait = new WebDriverWait(this.driver, 30);
        tempWait.until(ExpectedConditions.alertIsPresent());
        boolean boolAlert = false;
        int attempts = 0;
        while (!boolAlert && attempts < 1000) {
            attempts++;
            driver.switchTo().alert().accept();
            System.out.println("Successfully clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
            boolAlert = true;
        }
    } catch (org.openqa.selenium.UnhandledAlertException e) {
        driver.switchTo().alert().accept();
        boolAlert = true;
        System.out.println("Sucesffuly clicked on the 'Pay Securely Now Button' and 'Closed the popup'");
    } catch (Exception e) {
        System.out.println("Unable to click on the 'Pay Securely Now Button', Exception: " + e.getMessage());
        Assert.fail("Method failed: clickPaySecurelyNowButton");
    }
}

==或==
您可以在驱动程序实例化期间使用 chrome 功能使用全局设置:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

WebDriver driver = new ChromeDriver(capabilities);

关于java - Webdriver:为什么我仍然得到 'Unexpected Alert Open'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44161326/

相关文章:

javascript - Apache POI excel 准备好从 A 到 N 阅读专栏

java - Eclipse 为什么以及何时将 Maven 依赖项放在 CLASSPATH 中而不是 MODULEPATH 中?

javascript - 如何在所有浏览器中运行单元测试?

python - 如何使用 webdriver 作为上下文管理器

java - 线程 "main"java.lang.NoClassDefFoundError : com/google/common/base/Function 中出现异常

google-chrome - ChromeDriver 在 Jenkins Selenium 作业上不断崩溃 : "WebDriverError: Chrome failed to start: crashed"

java - 需要帮助在java中分割字符串

java - 无法以正确的顺序向 ArrayList 添加元素

perl - Selenium:FF 和 Chrome 之间的不同帧处理?

jenkins - Karma 无法使用 Jenkins 和 Ubuntu 12.04 启动 Firefox