handling pop up with many buttons using selenium webdriver - java

I'm using selenium web driver with Java language. when there are two buttons in a pop up i.e. ok and cancel , it can be easily handled with web driver using the following code:
Alert alert = driver.switchTo().alert();
alert.accept(); // or alert.dismiss(); depending upon the action u want to perform.
but what to do when there are more than two button, i.e. there are 3 to 4 buttons in the pop up ( e.g. like ok,cancel,try again, ignore/continue), in that case what do we do? how can we click on whichever button that we want?
Thank you very much for your help in advance

What we're talking about are JavaScript's dialog popups. There's alert (has an OK button), confirm (has OK / Cancel) and prompt (has an input field and OK). Nothing more. Therefore, the dialog you're seeing is not a JavaScript dialog and can't be handled by Selenium's Alert interface.
You could be dealing with one of those two:
A custom dialog like jQuery's dialog() (or something similar). That's good news! That's no real popup, that's just a well designed overlay consisting of normal HTML made to look like a dialog. You should be able to interact with this the usual way of WebDriver: inspect the elements with the tool of your choice, then find and click the button that needs to be clicked.
A native browser's or even operating system's dialog (a download dialog, for example). That's bad news, as WebDriver can't handle these. Moreover, they tend to look differently across browsers / systems / language settings, so you'll need to detect and handle every case. Your options include:
The Robot class, it allows you to "press" programatically anything on the keyboard (or clicking blindly) and therefore getting rid of the dialog by, say, pressing Enter
AutoIt. It's a Windows program useful for handling any system-level automation.
That's more or less it. You can specify which dialog are you particularly dealing with and we might be able to come up with a better workaround. For example the download dialogs can be avoided entirely, etc.

You only want to use alert() when dealing with native browser popup dialogs. If the web app your testing pops up an HTML dialog then you can select and click on any of the buttons using the element ID, xpath, CSS selector etc.

Related

Blueprism - Java PopUp Window does not return the control back

I am automating a java application using blueprism. When I click a button on the application, a popup window comes. Then I have to interact with the buttons on this popup window.
However it seems impossible since, once the button is clicked (using java press action), the control is not returned back to blueprism. It waits forever until the popup window is closed. So I cannot proceed to next stage where I have the interactions with the popup window.
Is there anyway that I can stop blueprism waiting for the popup to close and I can continue automating the pop up window by attaching it separately?
The actions done with following modes will wait until a return of control from application:
some code-stages (for example MS Excel VBO)
java-mode
sap-mode
If that is causing the problems, then try other modes, for example win32 mode, UImode or Region mode. If you click this button with these actions, then you should use wait stages to control the flow of application.
I was able to continue my automation using spying. I used Navigate->Focus action to select the button and then used global send keys on "spacebar" to press the button. That way blueprism does not wait for popup window to return the control back. Alternatively if there are any shortcut keys, you can global send keys for them instead of using "Press" action. Only drawback is when using spying mode, you can't run anything on the screen except for the app being automated.

WinAPI - Determine if a window has maximize/restore capability

Problem: Determining if a window (by, say, hWnd) has the capability of being maximized or restored. Purpose of this is to programmatically send maximize/restore events to windows (for automation) but exclude windows which can't handle it.
Please consider the following two example windows while trying to determine this from GetWindowLong with GWL_STYLE:
a) The Discord client app (it is built with Electron). It has styles:
WS_CAPTION
WS_SIZEBOX
It does NOT have styles:
WS_SYSMENU
WS_MAXIMIZEBOX
This window does display a restore/maximize button and behaves properly when SW_MAXIMIZE/SW_RESTORE are posted to it.
b) The "Are you sure you want to quit?" prompt in InteliJ IDEA. It has styles:
WS_CAPTION
WS_SIZEBOX
WS_SYSMENU
It does NOT have style:
WS_MAXIMIZEBOX
This window does NOT display a restore/maximize button and does NOT behave properly when SW_MAXIMIZE/SW_RESTORE are posted to it (it crashes the whole IDE).
So, given these two examples, the following facts are true:
1) A window with WS_SIZEBOX style may not respond to SW_MAXIMIZE/SW_RESTORE messages properly (wasn't designed to);
2) A window without WS_SYSMENU nor WS_MAXIMIZEBOX may still respond to SW_MAXIMIZE/SW_RESTORE messages properly (draws system menu and/or buttons itself in a non-standard way)
3) Both of them have a native system menu that opens via title bar secondary-click and they correctly list maximize/restore as enabled or disabled (whatever matches their capability).
Given those two facts, how do I actually determine if a window can or cannot handle being maximized/restored? It seems it is not possible from window style alone, but I can't find anything in the Win32 API WinAPI to do this.
Note that I am deciding on whether to send SW_MAXIMIZE/SW_RESTORE by calling GetWindowPlacement and checking the showCmd.
Also note that I am using Java/JNA but I understand C++ or C# perfectly fine if you want to share code snippets.
I found that as a last resort I can use GetSystemMenu > GetMenuItemInfo to check if either Restore or Maximize system menu entry is enabled. So this is a decent fallback if the window has no WS_MAXIMIZEBOX style for whatever reason.

ZK Framework: How to capture Buttons click event "Leave this page", "Stay on this page" of window popped up by Clients.confirmClose()?

Clients.confirmClose() method in ZK Framework pops up window saying "Are you sure want to leave this page?" with two buttons- leave this page , stay on this page. I want to perform some operation when user clicks on buttons either "leave this page" or "stay on this page"? Can any body suggest me how can i achieve this?
This happens in mount.js; search for window.onbeforeunload.
The standard JavaScript event onbeforeunload shows the dialog when the registered function returns a string (docs).
If you want to override the behavior, you need to install your own handler and use the setTimeout() trick described in this question: Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?
Note that it's not possible to do something in the "leave page" case since the browser will have killed all the JavaScript that you have written at this time.

JOptionPane vs. JDialog

This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog
I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.
For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.
I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog
Also I could use some pointers how one should appropriately design and implement this.
Thank you.
Here's a statement I found on the Java website that says one key point about the difference between the two.
How to make Dialogs
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs.

Bring browser from back to front? (Selenium Web Driver, Java)

Is there any way I can bring browser from the back to the top(front)??
The situation is that there are two browsers, firefox and chrome. So, I instantiated two drivers, new FirefoxDriver() and new ChromeDriver(), let's call them fdriver and cdriver.
What I want is when the program is using Firefox, the firefox browser should be on the top. And so does Chrome. But, I am stuck how to bring the browser to the top when they are on the back.
I already tried,
Javascript: self.focus() and window.focus(). / WebDriverBackedSelenium to make driver back to selenium and use windowMaximize and windowFocus
Any idea is appreciated, thanks
You should be able to do this with
driver.SwitchTo().Window("//name of the window");
and that will bring whatever window you want into focus.
Now I don't use selenium, but I use Geb which is a wrapper around selenium so calling the javascript may be different for you, but this is how I did it (should be similar)
browser.js."alert()"
webdriver.switchTo().alert().accept()
I called the javascript alert function which brought the window to the foreground then I dismissed the alert with webdriver.switchTo().alert().accept().
Robot from java.awt; could help you:
Robot.mouseMove(webDriver.manage().window().getPosition().getX(),webDriver.manage().window().getPosition().getY());
Robot.mousePress(InputEvent.BUTTON1_MASK);
Robot.mouseRelease(InputEvent.BUTTON1_MASK);
Limitations:
if you have more than one monitor, you need to calculate absolute coordinates. As webDriver.manage().window().getPosition().getX() give you offset for the currently used display.
window should be visible

Categories