Can't we identify the elements present in a Modal window? - java

I've been using Java Robot to handle Modal windows by performing Mouse and Key events on them. But is there a way to actually identify an element and its properties present in a Modal window using some tool?, And what are the alternatives for Robot. Thanks in advance.

Related

How to drag & drop the element from one window to another window?

We have a scenario to automate where we have to drag an element from one window & drop it into another window.
We have tried a few options using Actions class but that didnt work out because after we click & hold the item, we need to switch to another window before dropping it. So once we switch, it loses the item. We are using Selenium with Java. Does anyone know any possible solution for it?

Difference between clicking with Selenium and clicking with Robot

Is there a difference between testing perspective and not usability, on clicking elements with selenium like this:driver.findElement(By.id("foobar")).click();
instead of using coordinates and the robot class.
Example: If an element is not clickable because of a bug, robot will not be able to click it. Will selenium be able to click it ?
Selenium tries as much as possible to emulate what a regular user can and cannot do. When you use findElement(...).click() Selenium will raise an exception if operation cannot be performed. For instance, if an element exists in the DOM but is not displayed, an exception will be raised. And note that Selenium's .click() will try to scroll elements. If you ask it to click an element that is not visible but could be visible by scroll it, Selenium will scroll to bring it into view.
Also note that Selenium will usually take the element's coordinates and perform a click at those coordinates. So the idea that a moving element will always be hit by findElement(...).click() (expressed in this answer) is incorrect. Using findElement minimizes the window between which the coordinates are acquired and the click event is sent, but it does not completely eliminate it. The reason that Selenium works with coordinates is, again, to reproduce what a user would do. The user would see the element, move the mouse to the coordinates of the element, and click on it. If the element has an transparent overlay over it, then the overlay will get the click, rather than the element. This works because Selenium clicks at the coordinates rather than send a click directly to the element that you selected with findElement.
If you are using Robot to perform the click, the stakes are roughly the same as above, with a few caveats:
Trying to click moving elements is more of an issue because the window of time between acquiring the coordinates and performing the click is greater.
Robot does not know how the DOM is structured so won't scroll elements for you.
Rogério Peixoto pointed out that you can use JavaScript to perform the click. This will cause the event handler for click to be called for the element, irrespective of whether a user would be in fact able to access the element. This can allow clicking on elements that are not otherwise clickable but I would not do this unless there are overriding reasons to do so. I've gone over the difference between Selenium's click and the JavaScript click in this answer.
If an element is not clickable because of a bug, robot will not be able to click it. Will selenium be able to click it?
It depends on what the bug is that is preventing the click. I would suggest that you use element.click() in almost all cases because it's more maintainable. For coordinate clicks, what if the element moves? Then you have to update your script with the new position where element.click() will just work. Another issue with coordinate clicking, what if your element moves and another one is in its place? That will likely cause your script to fail but will be very hard to track down. An example might be an unexpected popup that covers the element to be clicked. Now your coordinate click will hit the popup and eventually cause an error/failure. If you do element.click() you will at least get an error that another element would receive the click. You can then track down what is getting in the way from debugging.
Selenium clicks in my experience click an element within a browser window, where as Robot click seem to click whatever is on the desktop at the time. Personally I would recommend you try to use Selenium clicks where possible as you can specify the window in which you are clicking.
Using this:
driver.findElement(By.id("foobar")).click();
Means that you will click the element with the foobar id regardless where it is.
If you use coordinates, you won't be able to click the element if they change their position.
Updating answer due to your updated question:
I'm not quite sure where you're trying to get with this.
In Selenium, if the element is visible it will receive the click event, if not you'll have an exception.
You could force click it with javascript:
WebElement element = driver.findElement(By.id("foobar"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
But it won't trigger the an eventual javascript event registered to that button, you would have to fire it manually.

Unable to select mouse over elements in Selenium Webdriver Java

I am automating a web application using selenium webdriver and java. It has few mouse over elements which I am not able to automate. Mouse over works well and newly visible elements locate properly when I record and play in Selenium IDE but same is not working when I run in eclipse. I have automated similar mouse over elements earlier but facing difficulty in automating this. I have attached screenshot of a web application and the code for mouse over elements and here I am not able to mouse over on "Configuration" menu and select "Configure Hierarchy Metadata". It would be really great if anybody could help me out in this!! Let me know if any more details needed.
Thanks in advance.
This is a very common issue. You need to make sure you are interacting with the element that has the hover listener..
In this case, it looks like it is the <a> that is receiving the hover. Try moving to element (hovering over):
By.cssSelector("ul.topmenu li:nth-child(2) > a[title='Configurations']");
In order to achieve this, you will first have to hover on element(say - element1) which makes element to be clicked(say - element2) on visible and then click on the option from list -
Actions actions = new Actions(driver);
actions.moveToElement(element1).moveToElement(element2).click(element2).build().perform();

java popup menu has no focus with VK_CONTEXTMENU

I am incorporating a popup menu in my java application.
I have added a mouse listener to show it up and I have also registed a keyboard action with the key event KeyEvent.VK_CONTEXT_MENU, that brings it up as well.
Problem is that the popup has no focus when it gets to be shown. I can't use the up and down arrows in order to walk it through. In fact, it can be operated only with the mouse (which takes the stinger from my wish to operate my system using the keyboard only...).
I could not find any useful information on that issue over the net, and have already read the java tutorials entry on menu. I also tried the following (each on its own, and cooperated), and none of them worked:
Calling popupmenu.requestFocusInWindow() before and after the show is invoked.
Calling popupmenu.setSelected(firstMenuItemObjectInstance) before and after the show is invoked.
Calling firstMenuItemObjectInstance.requestFocusInWindow() before and after the show is invoked.
any ideas ?

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.

Categories