Created
January 4, 2019 09:33
-
-
Save pragmatictesters/312dbe4c8612979b1497442fcaa30643 to your computer and use it in GitHub Desktop.
Selenium WebDriver Examples : Demonstrate use of Actions class to simulate user gestures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pragmatic.selenium.examples; | |
import io.github.bonigarcia.wdm.WebDriverManager; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.Keys; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.interactions.Actions; | |
import org.testng.Assert; | |
import org.testng.annotations.AfterMethod; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* This class use of Actions class for emulating user gestures using Keyboard and Mouse | |
**/ | |
public class ActionsClassExample { | |
private static final String BASE_URL = "http://hrm.pragmatictestlabs.com"; | |
private WebDriver driver; | |
@BeforeClass | |
public void beforeClass() { | |
WebDriverManager.chromedriver().setup(); | |
} | |
@BeforeMethod | |
public void beforeMethod() { | |
driver = new ChromeDriver(); | |
driver.get(BASE_URL); | |
driver.manage().window().maximize(); | |
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); | |
} | |
@AfterMethod | |
public void afterMethod() { | |
driver.quit(); | |
} | |
@Test | |
public void testLoginWithActionsClass() { | |
Actions actions = new Actions(driver); | |
WebElement txtUsername = driver.findElement(By.id("txtUsername")); | |
WebElement txtPassword = driver.findElement(By.id("txtPassword")); | |
WebElement btnLogin = driver.findElement(By.id("btnLogin")); | |
actions.click(txtUsername) | |
.sendKeys("Admin") | |
.sendKeys(Keys.TAB) | |
.sendKeys("Ptl@#321") | |
.sendKeys(Keys.RETURN) | |
.perform(); | |
String welcomeMessage = driver.findElement(By.id("welcome")).getText(); | |
Assert.assertEquals(welcomeMessage, "Welcome Admin"); | |
} | |
/** | |
* Demonstrate pressing SHIFT key down while typing and input text in uppercase | |
*/ | |
@Test | |
public void testLoginWithActionsClassUsernameInALLCAPS() { | |
Actions actions = new Actions(driver); | |
WebElement txtUsername = driver.findElement(By.id("txtUsername")); | |
WebElement txtPassword = driver.findElement(By.id("txtPassword")); | |
WebElement btnLogin = driver.findElement(By.id("btnLogin")); | |
actions.click(txtUsername) | |
.keyDown(txtUsername, Keys.SHIFT) | |
.sendKeys("admin") | |
.keyUp(txtUsername, Keys.SHIFT) | |
.sendKeys(Keys.TAB) | |
.sendKeys("Ptl@#321") | |
.sendKeys(Keys.RETURN) | |
.perform(); | |
String welcomeMessage = driver.findElement(By.id("welcome")).getText(); | |
Assert.assertEquals(welcomeMessage, "Welcome Admin"); | |
} | |
/** | |
* Demonstrate use of drag and drop using Actions class | |
*/ | |
@Test | |
public void testDragAndDrop() { | |
WebDriverManager.chromedriver().setup(); | |
WebDriver webDriver = new ChromeDriver(); | |
webDriver.get("http://demosite.pragmatictestlabs.com/Droppable.html"); | |
WebElement sourceElement = webDriver.findElement(By.id("draggableview")); | |
WebElement targetElement = webDriver.findElement(By.id("droppableview")); | |
//Dropping an element to a target element | |
Actions actions = new Actions(webDriver); | |
actions.dragAndDrop(sourceElement, targetElement) | |
.build() | |
.perform(); | |
String message = webDriver.findElement(By.id("droppableview")).getText(); | |
Assert.assertEquals(message, "Dropped!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment