【问题标题】:Selenium Java handle object(alert dialog) exception without delaying. (unpredictable Java pop-up)Selenium Java 无延迟地处理对象(警报对话框)异常。 (不可预测的 Java 弹出窗口)
【发布时间】:2017-07-28 15:32:44
【问题描述】:

目标:弹出警报。无论是否显示,我都希望它继续。如果显示,必须选中复选框,然后点击继续。如果没有,请忽略。

拦截器:如果警报显示,它将处理该操作并关闭对话框。但是如果不是,selenium 在未显示时会挂在那里而不处理条件。

背景:我之前用过 UFT,可能我的逻辑有问题。 弹出的是应用程序警报(不是系统),因此假设“切换到(),接受()/解雇()将不起作用。我将在登录后立即添加句柄警报,并在下面的登录方法中。

Selenium 框架背景。 : 我们使用 selenium maven 框架,serenity BDD。对象设置在页面的开头。和 serenity.properties 处理超时默认值等等。

弹出对象(如果出现):

@FindBy(xpath = "//input[@id='notification-ack']")
private WebElement PcoNoticeChbx; //this is a check box, needs to be checked
@FindBy(xpath = "//button[contains(.,'Continue')]")
private WebElement PcoNoticeContinueBtn;//button to close the alert

*登录方式*

 public void loginIntoExtApplication(String baseURL, String loginURL, String uId, String pwd, String extAppEnv)throws Exception {
            gotoExtLoginPage(baseURL, loginURL);         
            enterLoginCredential(uId, pwd);
     openAt("https://" + extAppEnv + ".programportaltest.hrsa.gov/sdms-
    extranet/index.xhtml");

我的方法:

//1.

     if (PcoNoticeChbx!=null) {
            PcoNoticeChbx.click();
            PcoNoticeContinueBtn.click();
        } else {
            System.out.println("Dialog didn't display, no need any action");
        }

//2.登录操作后挂在这里。

     if(!getDriver().findElements(By.xpath("//*[@id='submit']")).isEmpty()){
                 PcoNoticeChbx.click();
                 PcoNoticeContinueBtn.click();
            } 
             else {  
                System.out.println("Dialog didn't display, no need any action");
            } 

//3. 添加到手表不起作用,它显示待处理,下面的代码也失败了。我在 Junit 中以调试模式运行我的 Maven。它曾经工作正常。但手表元素总是显示(待处理)..

     boolean isPresent = getDriver().findElements(By.id("noticebox")).size() >0  
      System.out.println("the diaolog exist=  " + isPresent);

//4.甚至尝试了try-catch方法。

try{    
             PcoNoticeChbx.click();
             PcoNoticeContinueBtn.click(); 
         }catch (Exception e){ 
            // Printing logs for my report 
             Log.error("Report Category button element is not found."); 

            // After doing my work, now i want to stop my test case 
             throw(e); 
         } 
    return;
     }  

//5.尝试列出 webelemets:

列表 temp = webdriver.findElements(org.openqa.selenium.By.id("noticebox"));

    if (temp.Count > 0 && temp[0].Displayed) {
                    // script to execute if element is found
                } else {
                    //  continue the test
         }  

//6.及以下

if (!WebDriver.findElements(By.xpath("//*[@id='submit']")).isEmpty()==true);


        {
                //handle the dialog 
            }  
            else{   
                //continue 
            }

// 7.tried 使用布尔值,但它也停留在第一步

boolean Nbox = PcoNoticeChbx.isDisplayed(); {




    if (Nbox==false)
               {
             System.out.println("Dialog didn't display, no need any action");
               } 
             else if (Nbox==true) {
             PcoNoticeChbx.click() ;
             PcoNoticeContinueBtn.click();  
             }

【问题讨论】:

  • 当使用try catch代码时,它停止为:元素不可点击。
  • public void checkAlert() { try { WebDriverWait wait = new WebDriverWait(getDriver(), 2); System.out.println("尝试步骤"); wait.until(ExpectedConditions.alertIsPresent()); org.openqa.selenium.Alert alert = getDriver().switchTo().alert();警报.accept(); // } catch (Exception e) { //异常处理 System.out.println("catch exception steps"); } } }

标签: java maven selenium serenity-bdd findby


【解决方案1】:

如果这就像我处理过的弹出窗口一样,它们的工作方式如下:

  1. 客户来到站点,站点检查是否存在 cookie。如果该 cookie 存在,则永远不会启动弹出窗口(理想状态)。如果该 cookie 不存在(典型状态),则在指定的时间段后会出现一个弹出窗口。
  2. 客户关闭弹出窗口并创建一个带有过期时间的 cookie
  3. 一旦过期时间过去,cookie 就会过期,弹出窗口将再次触发

您需要进行一些调查并找到弹出窗口消失后创建的 cookie。清除缓存,浏览到该站点,并记下所有存在的 cookie。等待弹出窗口并关闭它。现在找到刚刚创建的 cookie 并检查它。这是您需要创建的 cookie。有很多关于创建 cookie 的教程。这很简单。

一旦您学会了如何创建 cookie,您就可以将其添加到您的脚本中,如下所述:

  1. 导航到域上您知道不存在的某个页面,例如www.domain.com/some404page。我们这样做是因为它不会触发弹出倒计时,而且我们需要在域上才能创建 cookie。
  2. 创建 cookie
  3. 进行常规测试

没有更多的弹出窗口。

【讨论】:

  • 嗨,杰夫C。这对我来说实际上是一个新想法。它适用于我的一些与您类似的测试。
  • 另外,这不是基于 cookie 的警告,而是 DB。
【解决方案2】:

为我的案例找到了解决方案。

这可能很容易。但我需要一些时间来研究。希望它会帮助你。

在使用了很多方法之后,对于这个javascripts的确认提示。我用过下面的方法。 .CurrentlyVisible() 方法的所有帮助。因为这个,我猜只有这个会给你结果,即使元素不存在或为空..

if (element(NoticeContinueBtn).**isCurrentlyVisible**()==true) {

    PcoNoticeChbx.click();
    PcoNoticeContinueBtn.click();
    //else just continue
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 2014-07-15
    • 2015-12-16
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    相关资源
    最近更新 更多