网络驱动程序:为什么我仍然会发现意外警报打开'?

时间:2017-05-24 14:27:49

标签: 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,

1 个答案:

答案 0 :(得分:1)

在这一行中,您已经开始提醒:Alert alert = tempWait.until(ExpectedConditions.alertIsPresent());

而不是写driver.switchTo().alert().accept();,请尝试这样做:alert.accept();

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

尝试更新您的代码:

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");
    }
}

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

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

WebDriver driver = new ChromeDriver(capabilities);