Selenium via Eclipse: is it possible to move the cursor position? - selenium

I am using the selenium plug in for eclipse to automate the testing of newly created websites. I am trying to click a button that is in a menu and only visible when the cursor is located over the menu.
Is it possible to move the cursor so that this button can be clicked ?

It will depend a little on how the menu has been implemented (i.e. the event that will trigger your button to appear) but you should look at the focus and mouseOver methods for selenium.
I.e. do something like
this.selenium.mouseOver(element);
where element refers to the menu and then do a click on the button. If mouseOver does not work (i.e. the button does not become available) try focus instead.

It's unclear if you're using Selenium RC or Selenium 2 and WebDriver.
I can only speak to the latter, but you can use Actions to move the mouse and click. The basic idea is you define an object that is a series of actions, and then you perform those actions.
An introduction on how to use these is at http://code.google.com/p/selenium/wiki/AdvancedUserInteractions, and a good writeup with Python examples is http://www.theautomatedtester.co.uk/blog/2011/selenium-advanced-user-interactions.html
It sounds like in your case, you would have something like:
Actions menuClick = new Actions(driver);
builder.MoveToElement(menuElement)
.MoveToElement(buttonElement)
.click(buttonElement)
Action menuClick = builder.build();
menuClick.perform();

Related

Is the Selenium click() recognized as human or automation?

My scenario which produces the question goes something like below:
I enter a webpage via normal means, next I press on a button, to start a HTML5 application on this webpage, this application is inside an iFrame. On application start I'm being prompted to either turn the sound on or off. At this point there are two possible outcomes:
1. When I answer this prompt manually, new buttons appear in the application window, as expected.
2. When I answer this prompt through automation via Appium, new buttons do not appear.
Now to the question:
To answer the prompt I use the click() method from Selenium. Is it possible that this click() is not considered to be executed by a human and therefore doesn't trigger necessary things? And since I don't have access to the source of the application can I force the Selenium click() to look exactly like a human click?
Here is the code I use to execute the mentioned click:
//Application loading up, hence the sleep
Thread.sleep(5000);
AppiumTestBase.getDriver().switchTo().frame("e_iframe");
Thread.sleep(5000);
WebElement soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
AppiumTestBase.getStandardWaitTime().until(elementToBeClickable(soundOff));
soundOff.click();
The program is able to find and switch in to the iFrame, there are no cross-origin issues either. The AppiumTestBase is just there for initializing the driver, setup capabilities etc. I also tried clicking the element via Actions and JavaScript, but there was no change in behavior.
In C# a workaround I've found to actually take control of the mouse and move it/click with it is to use "Microsoft.VisualStudio.TestTools.UITesting" for the Keyboard/Mouse libraries. From there, you can tell it "Mouse.Click(new Point(X, Y));"and it will move your mouse to that location and click.
Sample Code:
using Microsoft.VisualStudio.TestTools.UITesting;
var soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
Mouse.Click(new Point(soundOff.Bounds.X, soundOff.Bounds.Y));

how to use "mouseDown" command in selenium?

How to use mouseDown and mouseOver etc in selenium, with the websites I am working click is working but how to use mouse related commands?
You should use Action Builder
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
(for the Mouse Over)
More info here
With Selenium IDE, you need to execute some javascript code. See here

Selenium Replicating mouse Movement & clicking

I am relatively new to Selenium and it's capabilities So far I have found it's just the tool to use for automated regression testing for a web application. And it's for this regression testing suite that I am in the process of building that I have come to a problem. In the web app I have a page that displays a Jquery Datatable with some rows in it. Each row is bound with a context menu. That pop's up as a DIV floating on the top most Z-order, with absolute positioning all controlled with Jquery.
What I am attempting to do in selenium is to physically use this menu so that I am able to warp it with the appropriate regression tests. I have has some success with using the Actions class to call the context menu up. What I am not able to do is to have the mouse move to the menu Items can click them. I have been trying to use the MoveByOffset method but I don't seem to be having any luck in getting it to click on anything in the menu. I also have had no luck in working out where on the page the mouse is currently located thus not able to prove is anything is actually happening.
any help or advise on this will be gratefully received.
Thanking you all in-advance.
This is the code that I am currently using to attempt to drive this context menu. the values in the MoveByOffset have literally been every combination of 10 & -10. to try and hit the first menu item.
public void ClickAction_myPOTSIcontextmenu(IWebElement Row)
{
Actions builder = new Actions(this._driver);
IAction action = builder.MoveToElement(Row).ContextClick().MoveByOffset(-10,-10).Click().Build();
action.Perform();
}
Hope this helps,
WebElement element = driver.findElement(By.cssSelector("selector_for_element"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.click(hoverItem.getCoordinates());

Use of 'ClickAt ' selenium command

I'm confused about the difference between the Click and ClickAt commands in selenium. Where can I use the ClickAt command?
Here are what Selenium IDE says about those two commands :
click(locator) Arguments:
locator : an element locator
Clicks on a link, button, checkbox or
radio button. If the click action
causes a new page to load (like a link
usually does), call waitForPageToLoad.
And :
clickAt(locator, coordString) Arguments:
locator : an element locator
coordString : specifies the x,y position (i.e. - 10,20) of the mouse
event relative to the element returned
by the locator.
Clicks on a link, button, checkbox or
radio button. If the click action
causes a new page to load (like a link
usually does), call waitForPageToLoad.
click is used when you just want to "click" on an element, like a button, a link, ...
And clickAt is used when you want to "click" on a position designated by mouse coordinates.
I suppose the second one can be useful for some "rich" applications -- I've actually never used it... On the other hand, I use click like all the time.
If you have a page with form elements, links, buttons, and stuff like that, you'll probably generally use click : it's way easier to find an element using it's id or classname than having to find it's position in pixels on the page ^^
I noticed some differences between click() and clickAt() when testing a ExtJS app.
For example, if I try to click a tab in a Ext.TabPanel, click() command does not work, although I provide it with an correct xpath, and clickAt() works fine.
Code looks like this:
click("//li[#id='tab-panel-id__second-tab-id']/a[2]/em/span/span")
doesn't work, but
clickAt("//li[#id='tab-panel-id__second-tab-id']/a[2]/em/span/span","0,0")
works.
Notice that coordinates are (0,0)
I can't figure out why this happens...
I'm testing a GWT application and it seems like I have to use clickAt if I want to click on a node in a tree widget.
Be careful when testing clickAt. Sometimes double clicking the command will cause it to show up red. You will change the line to try other alternatives but nothing will work. But then run your script and the clickAt line will be fine with whatever you type in.
There is a dojo widget at our application which only works with clickAt("//span[#id='mastheadIconBar']/span[1]/span/span","0,0").
Don't know why, but only click("//span[#id='mastheadIconBar']/span[1]/span/span") does not work.

selenium.windowfocus() what is this command used for?

what is this command for?
You might think that Selenium.selectWindow() would be all you need. But that simply tells Selenium which window you want all the Selenium commands to go to. One of the commands you can send to it is "give this (currently selected) window focus".
It's a bit confusing, because Windows (and other systems) sometimes refer to the "selected window" - the one that's on top of the others, or the "active" window. Here, we call it the window that "has focus". It's the window where keyboard events will be directed. Inside a window, individual widgets (text fields, scroll bars, buttons) can have focus too.
So windowFocus() is like clicking on the title bar of the window that Selenium is currently working with.
From the Selenium Documentation
windowFocus()
Gives focus to the currently selected
window
Unless or until, you are using windowHandles to switch between multiple windows, ur focus will be default on first windows launched by selenium. widnowFocus does the same thing
In my experience, getting window focus using the Selenium windowFocus() method is sometimes not effective. I find myself sometimes using a JavascriptExecutor, then use the Selenium switchTo() method to switch to the handle that needs focus and then execute :
public static void getWindowFocus( String windowHandle ) {
driver.switchTo( windowHandle );
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript( "window.focus();" );
js = null;
}