Selenium-ActionClass - Notes By ShariqSP
Action Class Examples
Drag and Drop Elements
Volume Slider
Double Click Alert
Right Click Alert
Right click on this text
Character Alert
Click and Hold Alert
Introduction
To test an application, one needs to perform a number of user actions on it. To perform any operations on the web application such as double-click, selecting drop-down boxes, etc., the actions class is required.
What is Action Class in Selenium?
Actions class is an ability provided by Selenium for handling keyboard and mouse events. In Selenium WebDriver, handling these events includes operations such as drag and drop in Selenium, clicking on multiple elements with the control key, among others. These operations are performed using the advanced user interactions API. It mainly consists of Actions that are needed while performing these operations.
Action class is defined and invoked using the following syntax:
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
Mouse Actions
Mouse actions in Selenium are the actions that can be performed using a mouse, such as clicking, double-clicking, right-clicking, dragging and dropping, etc. These actions simulate a user’s interactions with a website through the mouse.
The Actions class in Selenium WebDriver provides the following mouse actions:
perform() :
moveToElement()
moveToElement(WebElement ele,int x,int y):
moveByOffset(int x,int y) :
click()
click(WebElement ele)
doubleClick()
doubleClick(WebElement ele)
contextClick()
contextClick(WebElement ele)
dragAndDrop(webElement src,webElement target)
dragAndDrop(webElement src,int x, int y)
clickAndHold()
clickAndHold(WebElement ele)
release()
release(WebElement ele)
build()
Keyboard Actions
Keyboard actions in Selenium are the actions that can be performed using a keyboard, such as pressing keys, holding down keys, releasing keys, etc. These actions simulate a user’s interactions with a website through the keyboard.
The Actions class in Selenium provides the following keyboard actions:
sendKeys(CharSequence… keysToSend)
sendKeys(WebElement ele,CharSequence… keysToSend)
keyUp(CharSequence… keysToSend)
keyUp(WebElement ele, CharSequence… keysToSend)
keyDown(CharSequence… keysToSend)
keyDown(WebElement ele,CharSequence… keysToSend)
Example: click()
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demowebshop.tricentis.com/");
driver.manage().window().maximize();
WebElement reg = driver.findElement(By.xpath("//a[@href='/register']"));
Actions act = new Actions(driver);
act.click(reg).perform();
Thread.sleep(5000);
driver.close();
}
Example: doubleClick()
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// using guru99
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement dclick = driver.findElement(By.xpath("//button[contains(text(),'Double-Click Me To See Alert')]"));
Actions act = new Actions(driver);
// double click with movetoelement
act.moveToElement(dclick).doubleClick().perform();
// double click without movetoelement
act.doubleClick(dclick).perform();
// using demo skillrary selenium
driver.get("https://demoapp.skillrary.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// locate selenium
WebElement selenium = driver.findElement(By.xpath("//ul[@id='trending']/li/a[text()='Selenium']"));
Actions act = new Actions(driver);
// double click with movetoelement
act.moveToElement(selenium).click().perform();
// locate plus
WebElement plus = driver.findElement(By.xpath("//i[@class='fa fa-plus']"));
// double click with movetoelement
act.moveToElement(plus).doubleClick().perform();
// double click without movetoelement
act.doubleClick(plus).perform();
Thread.sleep(5000);
driver.close();
}
Example: contextClick() / rightclick()
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// using guru99
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement Rclick = driver.findElement(By.xpath("//span[text()='right click me']"));
Actions act = new Actions(driver);
// double click with movetoelement
act.moveToElement(Rclick).contextClick().perform();
// double click without movetoelement
act.contextClick(Rclick).perform();
// Rclick on anywhere on webapp
WebElement Rclick = driver.findElement(By.xpath("//body[@id='authentication']"));
Actions act = new Actions(driver);
act.contextClick(Rclick).perform();
Thread.sleep(5000);
driver.close();
}
Example: moveByOffset(int x,int y)
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// using guru99
driver.get("https://google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys("ms dhoni");
driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys(Keys.ENTER);
Actions act = new Actions(driver);
act.moveByOffset(214, 110).click().perform();
org.openqa.selenium.Point p = driver.findElement(By.xpath("//span[text()='Images']")).getLocation();
Actions act = new Actions(driver);
act.moveByOffset(p.x,p.y).click().perform();
Thread.sleep(5000);
driver.close();
}
Example: moveToElement(WebElement ele,int x,int y)
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// using guru99
driver.get("https://demowebshop.tricentis.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement ele = driver.findElement(By.xpath("//a[@href='/login']"));
Point p = ele.getLocation();
Actions act = new Actions(driver);
act.moveToElement(ele, 60, 0).click().perform();
}
Assignment: Take screenshot of alert box
public static void main(String[] args) throws InterruptedException, IOException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.shoppersstack.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("loginBtn")).click();
driver.findElement(By.id("Email")).sendKeys("sp.shariq03@gmail.com");
driver.findElement(By.id("Password")).sendKeys("Spshariq@123");
driver.findElement(By.xpath("//span[text()='Login']")).click();
Thread.sleep(2000);
Actions act = new Actions(driver);
WebElement ele = driver.findElement(By.xpath("//div[@class='Toastify__toast-container Toastify__toast-container--top-right']"));
act.moveToElement(ele).perform();
File src = ele.getScreenshotAs(OutputType.FILE);
File trg = new File("./snapshots/shopperStackMsg.png");
FileHandler.copy(src, trg);
Thread.sleep(5000);
driver.close();
}
Example: clickAndHold(), clickAndHold(WebElement ele) and release(),release(WebElement ele)
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement washington = driver.findElement(By.id("box3"));
WebElement Us = driver.findElement(By.id("box103"));
Actions act = new Actions(driver);
act.clickAndHold(washington).release(Us).perform();
Thread.sleep(5000);
driver.close();
}
Example: dragAndDrop(webElement src,webElement target), dragAndDrop(webElement src,int x, int y)
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement madrid = driver.findElement(By.id("box7"));
WebElement spain = driver.findElement(By.id("box107"));
Actions act = new Actions(driver);
act.dragAndDrop(madrid, spain).perform();
Thread.sleep(5000);
driver.close();
}
Example: keyUp(CharSequence… keysToSend) & keyUp(WebElement ele, CharSequence… keysToSend)
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://demowebshop.tricentis.com/");
driver.manage().window().maximize();
WebElement search = driver.findElement(By.id("small-searchterms"));
Actions act = new Actions(driver);
act.keyDown(search, Keys.SHIFT).sendKeys("mobile").keyUp(Keys.SHIFT).perform();
Thread.sleep(5000);
driver.close();
}
Example: sendKeys(CharSequence… keysToSend) & sendKeys(WebElement ele,CharSequence… keysToSend)
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demowebshop.tricentis.com/");
driver.manage().window().maximize();
WebElement search=driver.findElement(By.id("small-searchterms"));
Actions act = new Actions(driver);
act.sendKeys(search, "mobile").perform();
act.sendKeys(Keys.CONTROL + "a" + Keys.DELETE).perform();
Thread.sleep(5000);
driver.close();
}
Example: Script to automate to search for a song and play in youtube
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.youtube.com/");
WebElement search = driver.findElement(By.xpath("//input[@id='search']"));
Actions act = new Actions(driver);
act.sendKeys(search, "mungarumale").sendKeys(Keys.ENTER).perform();
Thread.sleep(2000);
WebElement firstSearch = driver.findElement(By.xpath("//a[@id='video-title']"));
act.click(firstSearch).perform();
Thread.sleep(5000);
driver.close();
}
Example: Script to automate to search for a song and play in spotify
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://open.spotify.com/");
WebElement login = driver.findElement(By.xpath("//button[@data-testid='login-button']"));
Actions act = new Actions(driver);
act.click(login).perform();
Thread.sleep(2000);
WebElement loginbtm = driver.findElement(By.xpath("//span[text()='Log In']"));
WebElement email = driver.findElement(By.id("login-username"));
WebElement password = driver.findElement(By.id("login-password"));
act.sendKeys(email, "sp.s**********@gmail.com").perform();
act.sendKeys(password, "**********************").perform();
act.click(loginbtm).perform();
WebElement search = driver.findElement(By.xpath("//span[text()='Search']"));
act.click(search).perform();
Thread.sleep(2000);
WebElement searchtext = driver.findElement(By.xpath("//input[@placeholder='What do you want to play?']"));
act.sendKeys(searchtext, "mungaruale").sendKeys(Keys.ENTER).perform();
WebElement play = driver.findElement(By.xpath("(//img[@src='https://i.scdn.co/image/ab67616d00004851fc4e0e7cfa5b8549d8bb2d10'])[2]"));
act.click(play).perform();
Thread.sleep(5000);
driver.close();
}
Using shortcut ctrl+a in webElement with action class
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://demowebshop.tricentis.com/");
driver.manage().window().maximize();
driver.findElement(By.id("small-searchterms")).sendKeys("mobile");
driver.findElement(By.id("small-searchterms")).sendKeys(Keys.CONTROL + "a" + Keys.DELETE);
Thread.sleep(5000);
driver.close();
}
Using shortcut ctrl+a in sendkeys with action class
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://demowebshop.tricentis.com/");
driver.manage().window().maximize();
//using action class
WebElement search=driver.findElement(By.id("small-searchterms"));
Actions act = new Actions(driver);
//sending text mobile to search
act.sendKeys(search, "mobile").perform();
//sending ctrl+A
act.keyDown(search,Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
Thread.sleep(5000);
driver.close();
}
Example: perform()
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
WebElement element = driver.findElement(By.xpath("//span[text()='right click me']"));
Actions act = new Actions(driver);
act.moveToElement(element).contextClick().perform();
Thread.sleep(5000);
driver.close();
}
Example: build()
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
WebElement element = driver.findElement(By.xpath("//span[text()='right click me']"));
Actions act = new Actions(driver);
act.moveToElement(element).click().build().perform();
Thread.sleep(5000);
driver.close();
}
Example: Assignment to close window without any operation
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
Actions act = new Actions(driver);
act.moveByOffset(100, 100).click().build().perform();
driver.close();
}
//EMI calculator : drang and drop
@Test
public void checkEMI() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://emicalculator.net/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//loan slider
WebElement loanSlider = driver.findElement(By.xpath("//div[@id='loanamountslider']//span[@class='ui-slider-handle ui-corner-all ui-state-default']"));
//Intrest slider
WebElement intrestSlider = driver.findElement(By.xpath("//div[@id='loaninterestslider']//span[@class='ui-slider-handle ui-corner-all ui-state-default']"));
//Tenure slider
WebElement tenureSlider = driver.findElement(By.xpath("//div[@id='loantermslider']//span[@class='ui-slider-handle ui-corner-all ui-state-default']"));
Actions act = new Actions(driver);
//move sliders
act.dragAndDropBy(loanSlider, 80, 0).perform();
act.dragAndDropBy(intrestSlider, 275, 0).perform();
act.dragAndDropBy(tenureSlider, -216, 0).perform();
//find EMI and get the text
String emiAmount = driver.findElement(By.xpath("//div[@id='emiamount']//p[text()='₹']")).getText();
emiAmount =emiAmount .replaceAll(",","");
emiAmount =emiAmount.replaceAll("₹","");
System.out.println(emiAmount);
//write testcase
assertEquals(123308+"", emiAmount);
Thread.sleep(5000);
driver.quit();
}
Action Class Interview Questions
- What is the Action class in Selenium WebDriver?
- What are the common use cases of the Action class?
- How do you create an instance of the Action class in Selenium?
- Explain the difference between Actions and Action classes in Selenium.
- What are the main methods provided by the Actions class?
- How do you perform a mouse hover action using the Action class?
- Explain the clickAndHold method in the Actions class.
- What is the purpose of the contextClick method in the Actions class?
- How do you perform a double-click action using the Actions class?
- Explain the dragAndDrop method in the Actions class.
- What is the purpose of the moveToElement method in the Actions class?
- How do you perform a keyDown action using the Actions class?
- Explain the release method in the Actions class.
- What is the purpose of the sendKeys method in the Actions class?
- How do you perform a click action using the Actions class?
- Explain the pause method in the Actions class.
- What is the purpose of the keyUp method in the Actions class?
- How do you perform a contextClick action using the Actions class?
- Explain the dragAndDropBy method in the Actions class.
- How do you perform a moveToElement action with offset using the Actions class?
Multiple Choice Questions (MCQs)
- Which method is used to perform a mouse hover action using the Actions class?
- moveToElement()
- hover()
- mouseHover()
- hoverOver()
- What is the purpose of the clickAndHold method in the Actions class?
- To click and hold a WebElement.
- To perform a click action.
- To release a key.
- To pause execution.
- Which method is used to perform a double-click action using the Actions class?
- doubleClick()
- click()
- clickAndHold()
- mouseDoubleClick()
- What is the purpose of the contextClick method in the Actions class?
- To perform a right-click action.
- To perform a double-click action.
- To perform a drag and drop action.
- To move to an element.
- Which method is used to perform a drag and drop action using the Actions class?
- dragAndDrop()
- clickAndHold()
- dragAndDropBy()
- moveToElement()
- What is the purpose of the sendKeys method in the Actions class?
- To send keyboard keys.
- To send mouse actions.
- To send text to an input field.
- To send keys to an alert.
- Which method is used to perform a click action using the Actions class?
- click()
- performClick()
- perform()
- clickAction()
- What is the purpose of the pause method in the Actions class?
- To pause the execution of actions.
- To perform a pause action.
- To pause the execution of the script.
- To pause the execution of the browser.
- Which method is used to perform a contextClick action using the Actions class?
- contextClick()
- rightClick()
- performRightClick()
- clickAndHold()
- What is the purpose of the release method in the Actions class?
- To release a key.
- To release a mouse button.
- To release a WebElement.
- To release a drag and drop action.