Select jQuery Selectable Items Using Actions Class Of Selenium WebDriver

We have already learnt how to drag and drop drag-able web element on drop-able web element In THIS POST and drag and drop element for specific X Y pixel offset In THIS POST using selenium webdriver's Advanced User Interactions API (Actions class). Now supposing you wants to select multiple jQuery select-able Items then also you can use WebDriver's action command. Till now, I have not get chance to use this thing practically In any of my project but still let me share It with you because you can do It using WebDriver's Action class and might It helps you In any application's test case creation.


Selecting multiple Items means performing sequence of actions. In this case, We have to use clickAndHold method of Actions class to select multiple Items from jQuery select-able Items. I have prepared full practical example to perform this task as bellow.

package Testing_Pack;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DragAndDrop {
 
 WebDriver driver = null;
 
 @BeforeTest
    public void setup() throws Exception {  
  driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.com/2014/09/selectable.html");
    } 
 
 @Test
 public void selectItems() throws InterruptedException {  

        //Prepare list of selectable Items
        List<WebElement> selectableItems = driver.findElements(By.xpath("//ol[@id='selectable']/*"));           

        //Build the select Item action.
        Actions toSelect = new Actions(driver);
        toSelect.clickAndHold(selectableItems.get(2)).clickAndHold(selectableItems.get(5)).click();

        //Perform action.
        Action selectItems = toSelect.build();
        selectItems.perform();
 }
}

So this Is another example of WebDriver's advanced user Interaction. Here my point Is not how to select jQuery Selectable Items but my point Is how and when to use Actions class and Its method clickAndHold.

3 comments:

  1. Hi Thanks for the post :

    toSelect.clickAndHold(selectableItems.get(2)).clickAndHold(selectableItems.get(5)).click();

    how to find if the select able items are dynamic and without using indexes.

    regards
    sree

    ReplyDelete
  2. Aravind, Thanks for your tutorials. They are really helpful. I would like to highlight that we can use below lines to achieve the desired actions.

    Actions toSelect = new Actions(driver);
    toSelect.
    keyDown(Keys.CONTROL).
    click(selectableItems.get(2)).
    click(selectableItems.get(5)).
    keyUp(Keys.CONTROL).perform();

    ReplyDelete
  3. I got error of type: Cannot press more then one button or an already pressed button.
    Solution: toSelect.clickAndHold(selectableItems.get(2)).moveToElement(selectableItems.get(5)).click();

    Thank you for great tutorial.

    ReplyDelete