What are mouse events?
Mouse events are actions that can be performed with mouse such as double click, drag and drop, click and hold.There may be scenarios were we need to perform mouse hover to see the calender etc..
How Selenium handles mouse events?
Selenium offers Action class in order to handle the mouse events.The Action is a user facing API for emulating complex user action events.
There are different methods that are offered by Action Class. Most commonly used for Mouse events are
Notes
1-WebElement is a class in Selenium which is currently used to store the Project button location
2-Action class offers moveToElement which is used to move the mouse to location stored in WebElement
3-Perform method is used here to execute the action.
Mouse events are actions that can be performed with mouse such as double click, drag and drop, click and hold.There may be scenarios were we need to perform mouse hover to see the calender etc..
How Selenium handles mouse events?
Selenium offers Action class in order to handle the mouse events.The Action is a user facing API for emulating complex user action events.
There are different methods that are offered by Action Class. Most commonly used for Mouse events are
- clickAndHold()- Click without releasing the current mouse location
- contentClick() - Perform a context click at current mouse location
- dobleClick()- Perform a double click at current mouse location
- dragAndDrop(source,target) -Performs click and hold at the location of the source element and moves to the location of target element then releases the mouse
Here is the code snippets for using the Actions
//Configure the Action
Action builder=new Action(driver)
//To focus on element using Webdriver
builder.moveToElement(element).perform()
//To click on the element to Focus
builder.moveToElement(element).click().perform()
The below program focus on element using Action Class
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.*;
public class MouseEvents
{
public static void main(String[] args) {
WebDriver a = new FirefoxDriver();
a.manage().window().maximize();
a.get("http://www.seleniumhq.org/");
a.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions builder = new Actions(a);
WebElement e=a.findElement(By.id("menu_projects"));
builder.moveToElement(e).perform();
}
}
1-WebElement is a class in Selenium which is currently used to store the Project button location
2-Action class offers moveToElement which is used to move the mouse to location stored in WebElement
3-Perform method is used here to execute the action.
0 comments:
Post a Comment