The case of the disappearing pop-up window!
I click a link and it is supposed to bring up a little dialog pop-up window where I can change the State (like MA, VA, etc). I then would click OK, the pop-up window would disappear and I'd be back on the main window having fun with wild abandon.
That's what happens when I do it manually. When I do it through my nifty Selenium Java project the link gets clicked a pop-up window briefly appears then poof! It's gone and I mean really gone, not just in the background.
Here's a code sniipet:
WebElement foo4 = driver.findElement(By.linkText("State:"));
String myText;
myText = foo4.getText();
System.out.println("I got: " + myText);
foo4.click();
(do stuff in the pop-up window down here)
I threw a println in there to make absolutely sure that foo4 really is the link to be clicked and it is! Sanity checks help sometimes.
When the click event happens, poof! That pop-up window shows kinda like a ghost for a split second totally blank and then it's gone. I have no idea what's happening. It IS intermittent. 10% of the time the pop-up window does appear but most of the time no dice.
I'm open to ideas here. It's not a matter of cycling through the available windows yet, I only have a main window so that's not even in the picture yet.
Any help appreciated!
Instead of clicking the element, scrape the href or similar attribute that holds the URL, then do driver.get(url); or whatever it looks like in Java.
That way you can navigate to the URL in the main browser window and do what you need to do there.
Related
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.
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:
Locate the modal window (use By class to locate it)
Use WebDriverWait to setup your timeout conditions
Tell the driver to switch to it (if this times out, skip steps 3, 4, and 5)
Locate the checkbox and OK buttons
Click the checkbox and the OK button in that order
Tell the driver to switch back to the main window
Continue with your test
Create an If statement in terms of a boolean variable to check for the existence of a modal pop up that does not display 100% of the time.
If the modal is html generated (which it should be, if it holds dynamic content), then try:
driver.switchTo().activeElement();
driver.switchTo().defaultContent();
Also, you may have to insert a wait so that the html has time to generate.
If the modal is a browser alert, then try:
alert.accept();
I have a Menu Element that pops down only when clicked and then fades out on losing focus. My task is to click on a hidden sub menu item.
From previous answers to similar situations I used the following code to click on the Sub menu Items.
Actions builder = new Actions(driver);
WebElement mainMenuElement= driver.findElement(By.id("Mainmenu"));
builder.moveToElement(mainMenuElement).build().perform();
mainMenuElement.click();
Thread.sleep(2000);
WebElement mySubMenu=driver.findElement(By.xpath("//a[text()='Kit Components']"));
mySubMenu.click();
But, when I run the code in chrome v 27, once in three times or so the submenu item never gets clicked. The mainmenu opens up and stays still forever. But the submenu.click() is executed everytime without Exceptions and the submenu is also visible.
Is this because of my code? or
Could this be because the Submenu item has a localized region for Click? i.e Manually when I click on the submenu, the click works only on the text and not on remaining blank regions of the element.
Im using selenium 2.33 Java with chrome v 27.
Any advice would be very helpful, thanks.
First off, please, don't use Thread.sleep in your code. Ever. Instead, use the FluentWait or WebDriverWait commands.
Now, to answer your question, it seems as if you have stumbled upon what some people call a "flapper", or a "flakey test". If your test fails one in three times or so, something is really flakey.
I have noticed every once in a while that the click function doesn't always do the actual click (even though every indication in the code says it did). I wonder if you're happening upon this? Usually I do a check to see if the click seems to happen. If it tried clicking in the code but nothing happens, I will let it retry the click. If the retry doesn't work, then something is really up.
I have a page where a popup is created through iFrames.
I use switchTo().frame("LookupWindow") to switch to the popup.
I then successfully enter some text and look up a value
Once the value is found, I click on on it (still on the popup)
The popup now closes (because I clicked on the value in the popup)
This all expected behavior and works fine. However, the code hangs after the statement that clicks the value (which in turn closes the window). It waits forever, does not report an error at all.
Ideas? Workarounds?
Thanks.
First of all you need to use
switchTo().window("LookupWindow") to switch the control to pop-up window.
After it got closed, you need to switch back to main/previous window using below command.
driver.switchTo.defaultContent();
See this post to know more about how to switch controls between windows.
We have an application which, as its first UI action, displays a modal JDialog without a parent frame.
public LoginDialog(Frame owner, Config config, Object... params) {
super((Frame)null, true);
It unfortunately has the annoying characteristic that when it appears, although it comes to the front, it does not grab the focus.
So the user, after launching the application by double-clicking on the start menu, has to use the mouse to select the "login" dialog and type in information.
Is there a way to make this JDialog grab the focus when it appears in the front?
I've tried calls to "requestFocus" before, after and via invokeLater "during" the call to setVisible(true) - but none of these seems to have any effect.
How do we make a modal dialog grab the focus?
UPDATE: The issue was the code used to try to present a background "wait window". This window was displayed "behind" the login dialog as a hack so that when the dialog disappeared the user would see the "Please wait" message. As it was the first window created by the application, it got the focus. I am not sure if there would have been a way to make the dialog gain the focus again inside the event dispatch thread with this hack - but I fixed it by un-hacking it and doing things properly.
First, it a little strange that modal dialog is parent-less. The point in modal dialog is that it is displayed on its parent and does not allow to access parent.
So, the first recommendation is to make it non-modal. I believe it will work.
BTW I have just tried to create such dialog and have not problems with focus. Try probably to simplify your code:
JDialog d = new JDialog();
d.setSize(200, 200);
d.setVisible(true);
This works for me and I believe will work for you. Now start changing this simple code towords your real application code. At some point it will stop working and you will see where the problem is.
If nothing helps try to use the trick I described in this article. Look for title "Portable window activation". I hope it will help.
See Dialog Focus for a potential fix using a RequestFocusListener. I have used it successfully for setting focus in JOptionPane dialogs.
1) you have to create JDialog contents and showing container wrapped inside invokeLater()
or best and safiest way is
2) you have to set for ModalityTypes or Modal for parent
3) only one from containers could be Modal in applications lifecycle
I have a Java network application and this is what I want to do:
After user logs out, his interface window closes.
Now, I want for another window to show up that will say something like: "Thank you for using our application".
This final window should be borderless and without any available option, more like a plain picture (jpeg? why not?). By clicking on it, user will be sure to close this final window.
I googled and couldn't fin anything on this matter, it's hard to phrase the question.
I hope someone helps me...
https://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JWindow.html
A JWindow is a borderless, undecorated JFrame (no titlebar or buttons).
This should be what you need.
This should help:
http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html
You're interested in the windowClosing and windowClosed events
You have various possibilities, depending on when you want this dialog to display :
if you want it to display juste before the app closes, use addShutdownHook
if you want it to display when the last window closes, use addWindowListener
You can then use a JWindow with your image inside, and use addMouseListener to wait for the user to click on it.