0

I have test case with following scenario:

1) Navigate to website.
2) Enter Login Credential.
3) Click on Login.

After login in my application I have hint popup for end user. I use window handler for closing it but problem is sometimes selenium runs so fast that it clicks on it before it is visible. Any help will be appreciated.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(60, TimeUnit.SECONDS)
                .pollingEvery(2, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);
    String mainWindowHandle = driver.getWindowHandle();
            driver.switchTo().parentFrame();
            wait.until(ExpectedConditions.urlContains("client/default"));
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    System.out.println(String.format("Window size is %d", d.getWindowHandles().size()));
                    return (d.getWindowHandles().size() == 1);
                }
            });

            for (String activeHandle : driver.getWindowHandles()) {
                if (!activeHandle.equals(mainWindowHandle)) {
                    driver.switchTo().window(activeHandle);
                }
            }

            driver.findElement(By.xpath(elementLocator("popup_close_button"))).click();
           driver.switchTo().window(mainWindowHandle);  // switch back to parent window
sKhan
  • 9,694
  • 16
  • 55
  • 53

2 Answers2

0

Refer this code (taken from here and modified):


If you want it to work till pop up comes out then add some code to it as below:

int flag=0,wait=0;

while(flag==0 && wait<60){
try{

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
                                        // perform operations on popup
driver.close();///close pop up

driver.switchTo().window(parentWindowHandler);  // switch back to parent window
 //statements executed successfully, now exit by flag set to 1
 flag=1;

}
catch(exception e){//wait for one second
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
wait++;
}

if(wait==60){
JOptionPane.showMessageDialog(null,"Failed to detect pop up after 60 seconds."); 
}


}
0

After comments, it was stated that the close button does not appear in a new window or tab. Instead, it is the next page after login. Updating answer to leverage a pre-existing ExpectedConditions element which should (hopefully) resolve the issue.

 /**
  * Performs application Login.
  * @param driver WebDriver to act upon.
  */
  public void login(WebDriver driver) {
     //Perform Login function

     //1.  Identify the lookup we're going to use for the button.
     By closeButtonLookup = elementLocator("popup_close_button");

     //2.  Set up the Wait object with a reasonable timout value.
     WebDriverWait wait = new WebDriverWait(driver, 30);

     //3.  Try to resolve the close button (from the current page/window/tab) after Selenium believes it to be a viable click target.
     WebElement closeButton = wait.until(ExpectedConditions.elementToBeClickable(closeButtonLookup));
     System.out.println("Close button valid!  Proceeding to click!");

     //4.  Click the button
     closeButton.click();
     System.out.println("I clicked the close button!");

     //Continue program
 }
Jeremiah
  • 1,145
  • 6
  • 8
  • Hi Jeremiah thanks for your valuable post. I saw your code but the problem is that I am always getting window handler size as "1" so I am facing difficulty. If you can help me to solve my issue then it wil be great. Thank you. – Ajay Shinde Mar 21 '16 at 06:18
  • From your original post I was under the impression that the additional confirmation element after login was managed in a new window. If the window count never increases, it's possible that you're dealing with an Alert. I've added another method to my suggestion to account for this possibility. Can you please post the console output you're seeing on the failure? – Jeremiah Mar 21 '16 at 09:59
  • Hi Jeremiah. That is bootstrap hint model popup in application. I think issue is that it clicks on close button of model popup as soon as it is present in dom but at that time page is not fully loaded which causes the issue. Any help would be appreciated here. – Ajay Shinde Mar 21 '16 at 10:29
  • This seems contradictory to the statement above where the window handle size is never greater than 1? My original response should guarantee that close is not resolved until the second window opens and receives focus in the driver. In that condition I don't understand how close could have been clicked before the page is resolved. If the second window never opens, the _windowCheckAndSwitch_ predicate should throw a TimeoutException, and it wouldn't even try the close behavior. I fear I'm not understanding part of the context of the issue. – Jeremiah Mar 21 '16 at 17:01
  • To address your comment above directly : "the issue that it clicks the close button [...] before the page is fully loaded", please try the following. _wait.until(ExpectedConditions.visibilityOfElementLocatedBy(elementLocator("popup_close_button"))).click();_ That should force the button to be visible before returning it to be clicked on. – Jeremiah Mar 21 '16 at 17:10
  • Hi @Jeremiah I think we are misunderstanding here. I navigate to login page. Entering the valid credential it not open in the new window tab. It is just the page refreshes the same tab. – Ajay Shinde Mar 22 '16 at 05:30
  • I've updated my suggestion to account for Selenium invoking click on an object in the DOM (possibly) before the element is clickable. You're right, I was misunderstanding the context of this issue. – Jeremiah Mar 22 '16 at 11:21
  • Good Morning, sorry I think it was my issue that initally not clearly explain my case. Thanks for you support to address issue. But want to ask one more question. After lots of R and D of popup eveyone state to use window handler for popup. But it is quite un-understable to use the window handler because generally window handler comes into picture when we are having two different windows. What do you think?..Please let me know if I am misunderstanding something. Need to clear this case as working as a automation test engineer. – Ajay Shinde Mar 23 '16 at 04:31
  • Did the last offering help with the behavior? The suggestion for the window handler was not so much based on the term 'popup' in my case, as it was based on the description and summary of the problem. You've stated you're using a 'window handler' to address the problem. With that description and the provided snippet of code it really seemed as if you were trying to act on a new Window reference in your exectution. Basically, I think it was a miscommunication of what the problem was/is. http://stackoverflow.com/help/how-to-ask has some good tips for minimizing confusion in the future. – Jeremiah Mar 23 '16 at 22:11
  • Thanks @Jeremiah I appreciate your help. Now sometimes I am getting exception as "Element is not clickable at point(x,y)". One more thing I have element locator method in BaseClass which is having return type as "String". So how *BY* will help to make it more consistent. I have used WebElement instead of BY and follow your case. But still I am facing issue. Your help would be helpful – Ajay Shinde Mar 24 '16 at 04:02