2

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

拦截器:如果显示警报,它将处理该操作并关闭对话框。但如果不是,硒挂在那里没有处理条件,当它没有显示。

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

硒框架背景。:我们使用 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();  
             }
4

2 回答 2

1

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

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

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

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

    PcoNoticeChbx.click();
    PcoNoticeContinueBtn.click();
    //else just continue
}
于 2017-07-31T19:46:32.290 回答
1

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

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

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

了解如何创建 cookie 后,您可以将其添加到脚本中,如下所述:

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

没有更多的弹出窗口。

于 2017-07-29T03:31:25.450 回答