Friday, November 4, 2022

Selenium - Composite Actions

Amazing piece that I learnt today; Composite actions. Its a concept in which you carry several action commands within one line of Selenium code.

For an example, un the below syntax I perform these actions;

move to the search bar, press it, hold down shift key and enter a text and then double-clicking on the text.

So how is it done. 

We have used the "Actions" class within org.openqa.selenium.interactions.Actions.

Actions a = new Actions(driver);
WebElement caps = driver.findElement(By.xpath("//input[@id=\"twotabsearchtextbox\"]"));
a.moveToElement(caps).clickAndHold().keyDown(Keys.SHIFT).sendKeys("iphoneX").doubleClick().build().perform()

It supports many actions such as below 

clickAndHold().

keyDown(Keys).

sendKeys("iphoneX")

doubleClick()

Once you have selected the action classes required for your test, in Selenium Java you need to end the command with .build().perform(); 

Complete code is as below;

Selenium - Xpath Locators

1. By Tagname
 //tagname[@attribute='value'] 
driver.findElement(By.xpath("//input[@placeholder='Name']")) 

2. By index of the attribute 
 //tagname[@attribute='value'][index] 
 driver.findElement(By.xpath("//input[@type='text'][2]")) 
Used when your attribute and the value are same for multiple elements 

3. By tagnames when there are many instances of the childtag. 
 //parenttag/childtag[index]) 
driver.findElement(By.xpath("//form/input[3]")) 
Use index when there are many instances of child tag and you have to locate one of them.) 

 4. By parent to child-tag 
//parenttag[@attribute='value']/childtag[index]) 
driver.findElement(By.xpath(" //div[@class='forgot-pwd-btn-conainer']/button[1]")) 

 5. With regular expression 
 //tagname[contains(@class,'value')] driver.findElement(By.xpath("//button[contains(@class,'submit')]"))

Featured

Selenium - Page Object Model and Action Methods

  How we change this code to PageObjectModel and action classes. 1 2 3 driver . findElement ( By . id ( "userEmail" )). sendKeys (...

Popular Posts