1

There are a lot of Stack Overflow given solutions how to handle JavaScript windows, but my case is very specific.

I'm working on an automation process of our web base application. Recently dev team launched a MODAL (which means that window appears on top of the entire web page, disabling all the elements locators) JavaScript pop-up survey window, prompting our guests either to take this survey or not. I run my tests in parallel (two or three dozens threads at a time). For real users, that can manually interact with the given page, there are several options how to get rid of the window: either clicking on "NO" button, or just clicking anywhere on the page. I found the way how to click on "NO" button using Selenium where driver.findElement(By.xpath("xpathOfNOButton").click

The first problem is: There is no clue when and on what page this modal window may appear any next time I run the test. There are cases where modal window doesn't appear at all. Sometimes user may just open the home page, and window appears right away, sometimes it might take four to six minutes. No logic. Dev team only provided the explanation that this JavaScript modal window will appear for 33% of web site visitors (third party vendor, no internal control). That's it. So solutions like wait for certain amount of time, and then click on "NO" button won't work. No step exists that can trigger this modal window to pop-up (like on some other applications).

Tried solution: Because this is only happening on the production version of our web application, I set up a logic for Firefox profile once it launched with www. URL, it will disable all the JavaScript on the page. But this solution is breaking entirely all the pages because many other functionalities rely on that.

Question: Is there any way in Java or Selenium to inspect the entire HTML structure of the given page in order to figure out if this modal pop-up script is present within the HTML or not?

I understand this might increase my testing time because I will have to insect every other page.

The second problem is: Is there any way to click on blank area of the page without specifying particular element with Selenium WebDriver?

If yes, and modal window is caught, I can just do this action in order to close the modal window.

9
  • As I understood it's a custom js Alert? Commented Aug 31, 2016 at 15:56
  • @NickQ Hello. Not really familiar with the custom and non-custom js Alerts, but this is the line of HTML structure that corresponds to my headache <img usemap="#IPEMap" border="0" width="640" height="360" src="ips-invite.iperceptions.com/invitations/invitationsJS/125/…"> Commented Aug 31, 2016 at 16:08
  • How is this model window created? If this is a DIV element with a specific name, you can check them on the page. On the second problem, as this isa model window certianly this will be lesser in size than the page, so just click any where may be few pixcel from the page corners. Commented Aug 31, 2016 at 16:08
  • @Isiva Yes, there is a div with id exist for that modal window. Bu it can pop-up within seconds, or sometimes it might take 3-5 minutes to appear. The question: is it possible to look for this div first, and if found, somehow disable it, and only then perform any other steps? Commented Aug 31, 2016 at 16:24
  • Have a look here, may be it helps stackoverflow.com/questions/39145178/… Commented Aug 31, 2016 at 17:36

4 Answers 4

2

I ran into a similar situation with our sites. After 15 seconds, a modal dialog would popup and ask the customer to sign up for a newsletter. The problem was that depending on the script, I could be anywhere on the site in 15s and then the modal would pop up messing up the script execution. I did some investigating and found that the function that pops up the modal was checking the existence of a cookie and then if it didn't exist, it would popup the modal. The idea was that if the customer was presented the modal and made a choice, they shouldn't be prompted again for a certain period of time. At that point, I found the cookie and created it before starting the test. When the script first reached the site, the create modal function saw that the cookie existed and never created the modal.

I have no idea how the modal works on your site but I'm guessing it might follow a similar procedure so this info might be helpful to you. Creating the cookie is really easy. There are plenty of posts on SO and elsewhere describing this process, if you need it. A lot of it will depend on what info is in the cookie but the code is straightforward once you figure that out.

The process is navigate to some 404 page on your site, e.g. http://www.example.com/abcdefg, create the cookie, and then start your test.

Sign up to request clarification or add additional context in comments.

2 Comments

this is exactly what I was explained by the dev team. Modal window should look for a cookie, and if there is any of this exist saying that user already took this survey, modal window won't pop-up.
Great. So can dev give you the info on the cookie and you should be able to create one before the script starts and avoid the issue altogether.
0

First Problem Awnser:

Yes.

When you perform a findElement() or findElements(), Selenium will search in the whole document what you are trying to find. If findElement() does not found anything, throws an exception, instead findelements() will return an empty list. This way, you can use some cssselector or xpath in a findelements() to check if it exists, if not (returns a empty list) just skip the modal step.

Second Problem Awnser:

Yes.

There are two good methods to do it: You can perform a click in the point (1, 1) of the page do hide the modal using a javascript or jquery code inside selenium, but if you have a modal fade that hides the modal when you click on it, surely is better click using selenium than js, only targeting the modalfade div and clicking

Comments

0

We faced a similar problem before. There was an ad popup is coming up randomly on pages and we can't click the buttons we wanted because of that. So instead of webDriver.click(), we made a click method. It was look like;

public void clickTo(WebElement element) {
    try {
        element.click();
    } catch (WebDriverException ex) {
        logger.warn("WebDriver exception: ", ex);
        webDriver.findElement(By.xpath("//*[@id='xlightBox']//span[@class='closeBtn']")).click();
        element.click();
    }

6 Comments

How did you handle the cases where the popup comes up when webdriver is about to do other actions like select an option from dropbox, move to an element, input values to a textbox etc.
@Taylan I believe that it will work great only if pop-up appears between the click commands. But sometimes in my case the modal javascript window pop-up appears when user doesn't perform any clicks (cases where data is inputted to the text boxes, cases where user verifies the existence of certain elements).
@Grasshopper We have other methods then clickTo() for doing basically the same thing. If we want to input something to a textbox we're not using element.sendKey(), we have a type() method in our framework with try catch. By the way, I haven't wrote the whole method we are using. Inside of catch, there are if blocks for different type of popup windows. But the idea is same. I hope it helps some.
@GordonFreaman What happens if you remove the div which contains the modal window from the page DOM by using javascriptexecutor of selenium? No div no modal window to show up, perhaps....
@Grasshopper The problem is I don't really know on what pages this javascript pop-up may appear any next test iteration. Your advise is to look for a div that associates with the window won't work. Selenium will be looking for a div that may not be on the certain page and fail.
|
0

I think the best thing you can do is to use custom click method, which will execute another method(checkModal() for example) before clicking. CheckModal will check each time if the modal is up and will close if it is, otherwise do nothing.

if you can provide html for the modal, i edit the answer and provide java code to do the logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.