Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a definite Selenium solution to modal pop up dialogs in Internet Explorer with Java?

Tags:

java

selenium

I have searched many, many places for a solution to my problem, but haven't found it. I figured that by now, Selenium would have provided a straight forward and simple solution to handling modal windows/dialogs from Internet Explorer using Java.

The web application that I am testing has the following characteristics:

  • It is ONLY supported by Internet Explorer (no way around this)
  • Main page accepts a userid and password, with a "Login" button
  • Upon login and on page load, there is a pop up "Welcome - What's new" window with a checkbox to "Don't display this again" and an "OK" button to dismiss the window.
  • I cannot do anything to the parent window until I dismiss the pop up window
  • Right-click is disabled on the pop-up window (however, I can see the source code by opening the F12 tools before login and window pop-up)

This is what I've tried:

  • getWindowHandles() always returns 1 for the parent window, so this makes driver.switchTo(handle) not-applicable
  • It is not an alert, so driver.switchTo().alert() or accept() do not work
  • findElement(By whatever) will NOT find any elements in the pop up window (like the "OK" button or the checkbox, etc.)
  • Robot class is the only thing that I have seen work, where I can send keypresses to navigate to the "OK" button and click it to dismiss the window...

Here is my issue:

  • Since there is a checkbox to "Don't show this again", there are users for which this modal pop up window will display and some for which it won't. I need to account for both cases
  • I need to find a 100% sure way to know whether the pop up is displayed or not. If I have this information, I can make use of the Robot class (although "dirty") to perform actions on the pop up if needed
  • I tried finding out if the parent window elements are enabled using isEnabled(), but even though items are not manually "clickable" while the modal pop up window is displayed, isEnabled() always returns TRUE--so this does not work--is there a better way to check for this for the "blocked" elements in the background?

My questions:

  • How do you check for the existence of a modal pop up that does not display 100% of the time? (on Internet Explorer 10, using Selenium with Java)
  • Besides using Robot class, how do you interact with the actual Elements in a modal pop-up dialog (for example, dynamic Radio Buttons that don't always display the same options to the user)?

Thank you.

like image 796
Oscar R Avatar asked Mar 11 '16 17:03

Oscar R


1 Answers

You should use WebDriverWait with some expected condition. For example,

WebDriverWait wait = new WebDriverWait(driver, 5); // sets timeout to 5 seconds
wait.until(...); // Use ExpectedCondition to set the condition you need to check for (i.e. element to be clickable, frame to be visible, etc.)
// Do your thing.

The until method will return an object type relative to the function passed. For example, until(ExpectedConditions.elementToBeClickable(...)); will return a WebElement object you can use to exert an action on (like clicking on it).

Lastly, you should wrap those lines in a try/catch and handle the TimeoutException the wait method will throw if the condition never arises.

To summarize, structurally, your code should look something like this:

// instantiate the WebDriver
...
int timeoutMax = 2; // The maximum number of seconds you wish to wait before timing out (let's assume 2 seconds is reasonable for your case)
try {
    By checkboxLocator = By.id("checkboxID"); // Locate element by some criteria (id, css, xpath). Using by ID for illustration purposes only
    By buttonLocator = By.id("buttonID"); // same as above
    By popupLocator = By.id("frameid"); // same as above
    WebDriverWait wait = new WebDriverWait(driver, timeoutMax);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(popupLocator)); // assuming it is an iframe

    // The next lines will not be executed if the "Don't display this again" and clicking "OK" were clicked before (locating the frame will timeout because it not be visible)
    WebElement checkbox = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
    WebElement okBtn = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
    checkbox.click();
    okBtn.click();
    driver.switchTo().defaultContent(); // Switch back to default window
} catch (TimeoutException exc) {
    // Handle exception (i.e. log a warning) - This should be thrown as long as the modal dialog doesn't become visible
    // If modal dialog ever becomes visible again, clicking on the checkbox and OK button will be executed again.
}

Something like this should work. Of course, this make some assumptions that might not be true for your case. However, if you use the right locating technique for your modal dialog, you should be able to:

  1. Locate the modal window (use By class to locate it)
  2. Use WebDriverWait to setup your timeout conditions
  3. Tell the driver to switch to it (if this times out, skip steps 3, 4, and 5)
  4. Locate the checkbox and OK buttons
  5. Click the checkbox and the OK button in that order
  6. Tell the driver to switch back to the main window
  7. Continue with your test
like image 102
hfontanez Avatar answered Nov 15 '22 00:11

hfontanez