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.