Thursday, 11 October 2018

Working on Action Class in WebDriver - Examples to Practice

Working on Actions class:
====================
In Webdriver, We can use "Actions" Class to handle keyboard events, mouse events, mouse
hovers, Drag & Drop and click operations

Syntax:{to Configure Actions class}

Actions act = new Actions(driver);

// To click on webelement using Actions class:
Firstly, we need to specify webelement location and then specify operation that needs to perform

Syntax:
act.moveToElement(element).click().build().perform();

NOTE: We need to use perform() to execute the specified action

Exp: Write script to right click (i.e. Context click) on search edit box in www.amazon.in

Script:
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://amazon.in");

WebElement search= driver.findElement(By.id("twotabsearchtextbox"));
Actions act= new Actions(driver);
act.moveToElement(search).contextClick().build().perform();

Exp: Write script to enter "selenium" in upper case at search edit box
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://amazon.in");

WebElement search= driver.findElement(By.id("twotabsearchtextbox"));
Actions act= new Actions(driver);
Thread.sleep(3000);

act.moveToElement(search).click().keyDown(Keys.SHIFT).sendKeys("smallerletters").build().perform();

Exp: Write script to select "wish list" in amazon.co.in
WebDriver driver= new FirefoxDriver();
driver.get("http://amazon.in");
//create Actionclass
Actions ac= new Actions(driver);
WebElement signIn= driver.findElement(By.xpath("//span[text()='Hello. Sign in']"));
WebElement submenu= driver.findElement(By.xpath("//span[text()='Your Wish List']"));
ac.moveToElement(signIn).moveToElement(submenu).click().build().perform();

Exp: Write script to click "frequently asked questions" option in "Customer care" menu
WebDriver driver= new FirefoxDriver();
driver.get("http://hdfcbank.com");
driver.manage().window().maximize();
Actions act= new Actions(driver);
act.moveToElement(driver.findElement(By.linkText("Customer Care"))).build().perform();
Thread.sleep(2000);
driver.findElement(By.linkText("Frequently Asked Questions")).click();

No comments:

Post a Comment