Thursday, November 3, 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. It supports many actions. 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 locator

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

 

  • 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) 


  •  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.)


  • By parent to child-tag  //parenttag[@attribute='value']/childtag[index])

driver.findElement(By.xpath(" //div[@class='forgot-pwd-btn-
conainer']/button[1]")) 

 

  •  With regular expression  //tagname[contains(@class,'value')]

driver.findElement(By.xpath("//button[contains(@class,'submit')]"))