2

Goal: alert pop up. whether it's shown or not, I want it to continue. if it shows, have to select the checkbox, and hit continue. if not, ignore.

Blocker: if alert shows, it will handle the action and dialog will be closed. but when it's not, selenium hangs there without handling condition when it's not shown.

background: I use UFT before, maybe my logic could be wrong. the pop up is application alert(not systems), so assumed "switch to(),accept()/dismiss() won't work. and I will add handle alert right after login and within the login method below.

Selenium framework background. : we use selenium maven framework, serenity BDD. object is set at the beginning of the page. and serenity.properties handle the time out default and so on.

Pop up objects (if it appears):

@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

*Log in method *

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

My Approaches:

//1.

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

//2. hanged here after login actions.

     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. added to watch doesn't work, it shows pending, below code failed too. I ran my Maven in Junit in debug mode. it used to work fine. but watch elements always show (pending)..

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

//4. even tried the try-catch method.

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. tried list webelemets:

List 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. and below

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


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

// 7.tried with a boolean value, but it also hangs on here first steps

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();  
             }
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • when used try catch code, it stoped as: element not clickable. – MemphisTony V Montana Jul 28 '17 at 18:11
  • public void checkAlert() { try { WebDriverWait wait = new WebDriverWait(getDriver(), 2); System.out.println("TRYing steps "); wait.until(ExpectedConditions.alertIsPresent()); org.openqa.selenium.Alert alert = getDriver().switchTo().alert(); alert.accept(); // } catch (Exception e) { //exception handling System.out.println("catch exception steps"); } } } – MemphisTony V Montana Jul 28 '17 at 18:11

2 Answers2

1

If this is like the popups that I've dealt with they work like this:

  1. Customer comes to site and the site checks for the existence of a cookie. If that cookie exists, the popup is never launched (ideal state). If that cookie does NOT exist (typical state), after a specified period of time a popup appears.
  2. Customer dismisses the popup and a cookie is created with an expiration time
  3. Once that expiration time passes, the cookie expires and the popup will fire again

You need to do some investigation and find the cookie that is created once the popup is dismissed. Clear your cache, browse to the site, and note all the cookies that exist. Wait for the popup and dismiss it. Now find the cookie that was just created and examine it. This is the cookie you need to create. There are a lot of tutorials on creating cookies. It's pretty straightforward.

Once you learn how to create the cookie, you add it to your script as described below:

  1. Navigate to some page on the domain that you know doesn't exist, e.g. www.domain.com/some404page. We do this because it won't trigger the popup countdown and we need to be on the domain to create the cookie.
  2. Create the cookie
  3. Do your normal test

No more popups.

JeffC
  • 22,180
  • 5
  • 32
  • 55
1

Solution found for my case.

this maybe very easy. but takes some times for me to research. hopefully it will help you.

after use of many methods, for this javascripts confirmation alert. i have used below method. all of help of .CurrentlyVisible() method. because this one, and i guess only this one will give you result even when the element does not exist or null..

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

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