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
Related
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));
We have problem that doubleClick in selenium isn't working.
We are using Actions for perform doubleClick like appear here: Selenium webdriver Java code using web driver for double click a record in a grid.
After investigation and several attempts we found that it is related to the speed of the doubleClick. As you may know, speed of doubleClick can be change. Is there any option to set the speed of doubleClick in selenium?
Code used to perform the double click:
Actions actions = new Actions(webDriver);
actions.doubleClick(element).build().perform();
And we try without 'build' too:
Actions actions = new Actions(webDriver);
actions.doubleClick(element).perform();
As seems you are using customized timing for double-click in your application, i suggest to try use 2 separated clicks with wait same between them of same customized timing. i think it's easiest way to control timing between two clicks.
Alternative workaround described here
Simplified answer (change to needed element):
((JavascriptExecutor) driver).executeScript("document.getElementById('map_container').dispatchEvent(new Event('dblclick'));");
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");
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();
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;
}