如何关闭警报消息弹出窗口

How to close alert message pop-ups

我有两个测试用例:

仅供参考 - 如果我 运行 这两个测试用例单独进行,两者都有效。

  1. 脚本在文本字段中输入数据并单击更新按钮,弹出警报消息并由脚本关闭。成功!
  2. 脚本在不同的文本字段中输入数据并单击相同的更新按钮,弹出相同的警报。此时,alert 弹窗并没有关闭。测试失败,并抛出以下错误:

java.lang.AssertionError: java.lang.AssertionError: expected [false] but found [true]

这是我的两个测试用例:

1

// locate an element field to get default impressions
    WebElement defaultImpressions = driver.findElement(By
            .name("campaignImpScheduled"));
    defaultImpressions.clear();

    // send non-numeric characters
    defaultImpressions.sendKeys(scheduledImpNumbersNonNumericCharacters);

    // verify that the Update button is clicked
    verifyDisplay("Update Button ===> is clicked", By.id("updateCamSchBtn"));
    // click the Update button
    driver.findElement(By.id("updateCamSchBtn")).click();

    // verify text in the alert message
    verifyDisplay("You have made following changes :"
            + " ===> alert box appears", By.id("over"));

    // check the checkbox "Don't Prompt Me Again"
    driver.findElement(By.cssSelector("#setcookie")).click();

    // verify that the checkbox is checked
    verifyDisplay("The checkbox is ===> checked", By.id("setcookie"));

    // verify that the continue button is clicked
    verifyDisplay("Continue Button ===> is clicked",
            By.id("continueButton"));
    // click on the continue button
    driver.findElement(By.id("continueButton")).click();

    // get an element of the error message alert window
    driver.findElement(By.id("errorM"));

    // verify the alert message
    verifyDisplay(
            "'"
                    + scheduledImpNumbersNonNumericCharacters
                    + "'"
                    + " ===> is an invalid value for field: 'Scheduled Impressions'.",
            By.xpath("//div[@class='processerror']//div[@class='closeicon']"));

    // verify that the alert message window is closed
    verifyDisplay(
            "The alert message window ===> is CLOSED",
            By.xpath("//div[@class='processerror']//div[@class='closeicon']"));
    // close the error window
    driver.findElement(
            By.xpath("//div[@class='processerror']//div[@class='closeicon']"))
            .click();

2

// locate an element for the scheduled clicks field
    WebElement schClicks = driver.findElement(By
            .xpath(".//*[@id='searchid2']"));

    // clear the default value
    schClicks.clear();

    // send numerical values
    schClicks.sendKeys(scheduledClicksNumbersNonNumericCharacters);

    // verify that the Update button is clicked
    verifyDisplay("Update Button ===> is clicked", By.id("updateCamSchBtn"));
    // click the Update button
    driver.findElement(By.id("updateCamSchBtn")).click();

    // verify text in the alert message
    verifyDisplay("You have made following changes :"
            + " ===> alert box appears", By.id("over"));

    // check the checkbox "Don't Prompt Me Again"
    driver.findElement(By.cssSelector("#setcookie")).click();

    // verify that the checkbox is checked
    verifyDisplay("The checkbox is ===> checked", By.id("setcookie"));

    // verify that the continue button is clicked
    verifyDisplay("Continue Button ===> is clicked",
            By.id("continueButton"));
    // click on the continue button
    driver.findElement(By.id("continueButton")).click();

    // get an element of the error message alert window
    driver.findElement(By.id("errorM"));

    // verify the alert message
    verifyDisplay(
            "'"
                    + scheduledClicksNumbersNonNumericCharacters
                    + "'"
                    + " ===> is an invalid value for field: 'Scheduled Impressions'.",
            By.xpath("//div[@class='processerror']//div[@class='closeicon']"));

    // verify that the alert message window is closed
    verifyDisplay(
            "The alert message window ===> is CLOSED",
            By.xpath("//div[@class='processerror']//div[@class='closeicon']"));
    // close the error window
    driver.findElement(
            By.xpath("//div[@class='processerror']//div[@class='closeicon']"))
            .click();

完整轨迹:

java.lang.AssertionError: java.lang.AssertionError: expected [false] but found [true]
at org.testng.Assert.fail(Assert.java:94)
at com.an.oas.TestBase.verifyDisplay(TestBase.java:255)
at com.an.oas.campaign.CampaignSchedulingAndBilling.schClicksEnterNumbersAndNonNumericCharacters(CampaignSchedulingAndBilling.java:274)
at com.an.oas.campaign.test.CampaignSchedulingAndBillingTest.enterScheduledClicksWithNumericsAndNonNumericCharacters(CampaignSchedulingAndBillingTest.java:89)
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:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
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:767)
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:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1220)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

如果在第一次测试后系统中存在一些 "ghost" window,则可能会出现此问题。然后第二个测试将尝试关闭此 "ghost" 而不是您期望的那个。这解释了为什么测试是分开工作的。请确认测试后系统中没有隐藏 windows。