Selenium Replicating mouse Movement & clicking - selenium

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());

Related

Tooltip covers the button so I get org.openqa.selenium.ElementClickInterceptedException

I'm using Selenium for integration testing of our web app, and it's been working great up until I got far enough down the sidebar menu. Suddently the Create test would fail with org.openqa.selenium.ElementClickInterceptedException: element click intercepted. Other element would receive the click:
The error was very sporadic - order of tests would affect which test class failed - but finally I figured out the reason is that the tooltip from the most recently clicked menu option covers the button I'm trying to click. Not really sure why sending input to form elements (using WebElement:sendKeys() and org.openqa.selenium.support.ui.Select) does not count as moving the mouse away.
Is there a way to make Selenium work around this, or do we have to turn off tooltips in the test version (for obvious reasons I don't like the idea of running our tests on a custom version of the web app)?

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));

Element not visible as both the menu and submenu hidden

I want to open cric info and then click on 'live score'menu when the submenu opens, click on 'Desktop scoreboard'.
But the problem is live score menu is under a div which is hidden.
ie and this div is under td
"You can check the structure of the page to get detailed info"
so when i try to click the menu element using driver.findElementBy("xpath") i got the element not visible exception.
So i directly used the javascript used by the developer mopen('m2') which does the job of opening the menu but after this when i execute the command to find the submenu element again get the same error"Element not visible exception".
Tried making div visible by executing jscript.
PFB the code i used:
FirefoxDriver d1=new FirefoxDriver();
d1.get("http://www.cricinfo.com");
((JavascriptExecutor) d1).executeScript("mopen('m2')");
((JavascriptExecutor) d1).executeScript("document.getElementById('m2').style.visibility='visible';");
((JavascriptExecutor) d1).executeScript("document.getElementById('m2').style.display='block';");
d1.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
d1.findElementByXPath("//*[#id='mgDdRht']/tbody/tr[3]/td/a").click();
Also tried using the Actions class but everything in vain ,help is really appreciated
Thanks in advance.
You should be able to do this with an Action chain:
Actions builder = new Actions(d1);
Action clickSubMenu = builder.moveToElement(d1.findElement(By.cssSelector(".Nav td:nth-child(2).navLinks")))
.moveToElement(d1.findElement(By.cssSelector("#m0 td:nth-child(1)#mgDd>table:nth-child(1)>tbody:nth-child(1) td:nth-child(2).PopupTabs")))
.click(d1.findElement(By.xpath("//*[#id='mgDdRht']/tbody/tr[3]/td/a"))).build();
clickSubMenu.perform();
I tested this in c#, and it worked for me. I translated it to Java, but I may have made a syntactical error. Apologies if I have.
What I found is that I needed to move to Series. Then I had to move to Series - the dropdown version, as the dropdown version of Series is a different element than the non-dropdown version. Then I was able to move to the West Indies link, and click it.

Unable to switch among the windows

I am automating using selenium 2.0, my application launches the login page by default in a new window, hence my application has by default two windows. These two windows will remain open always. In this case I could switch between the windows without any problem. The below code is executed without any errors.
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
The problem starts while clicking the menu options a pop up window launches to search the records. Here, I need to switch between these three windows. I tried the below piece of code. It returns only the first two window handles.
Set availableWindows = driver.getWindowHandles();
This popup window is coded in such a way that, "In a .jsp file it is parameterised as window.open()".
Please let me know, if some one could help me on this?
If you're only seeing 2 window in getWindowHandles(), then the popup is probably a iframe. In this event, use driver.switchTo().frame() to switch focus to that frame instead of looking for an entirely new window.
Here's the documentation on the switch method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#switchTo()
One probable solution is to use JavascriptExecutor.executeScript() method to run a javascript code and handle the pop up window without switching into pop up window.
For example, from parent window of pop up window, run a javascript code which is something like this.
JavascriptExecutor exec = (JavascriptExecutor)driver;exec.executeScript("var popup = <<popupopener function>>; //operate on popup object to manipulate the markup of pop up window");

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

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();