Automation Using Selenium Webdriver

Tuesday, 25 October 2016

Multi Select Action Drop Down

Keyboard Actions::

     Given below are the methods to perform keyboard actions:
 sendKeys - Sends keys to the keyboard representation in the browser.
     Special keys that are not text, represented as Keys are recognized both aspart of sequences of              characters, or individually.
 pressKey - Press a key on the keyboard that is NOT text. The keys suchas function keys "F1", "F2", "Tab", "Control", etc. If keyToPress is asequence of characters, different driverimplementations      may choose tothrow an exception or to read only the first character in the sequence.
 releaseKey - Release a key on the keyboard after executing the keypressevent. It usually holds        good for non-text characters.Here are the syntax to call keyboard functions using Selenium WebDriver.

void sendKeys(java.lang.CharSequence keysToSend)
void pressKey(java.lang.CharSequence keyToPress)
void releaseKey(java.lang.CharSequence keyToRelease)

Mouse Actions
Listed below are some of the key mouse actions that one would come across inmost of the applications:

 Click - Performs a Click. We can also perform a click based oncoordinates.
 contextClick - Performs a context click/right-click on an element orbased on the coordinates.
 doubleClick - Performs a double-click on the webelement or based on thecoordinates. If left empty,      it performs double-click on the current location.
 mouseDown - Performs a mouse-down action on an element or based oncoordinates.
 mouseMove - Performs a mouse-move action on an element or based oncoordinates.
 mouseUp - Releases the mouse usually followed by mouse-down andacts based on coordinates.

Here are the syntax to call mouse actions using Selenium WebDriver:
void click(WebElement onElement)
void contextClick(WebElement onElement)
void doubleClick(WebElement onElement)
void mouseDown(WebElement onElement)
void mouseUp(WebElement onElement)
void mouseMove(WebElement toElement)
void mouseMove(WebElement toElement, long xOffset, long yOffset)

Multi Select Action
Sometimes we would be in a situation to select two or more items in a list box ortext area. To understand the same, we would demonstrate multiple selectionfrom the list using
'http://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx'.
Example
Let us say, we want to select 3 items from this list as shown below:




Let us see how to code for this functionality:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;
public class webdriverdemo
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.navigate().to("http://demos.devexpress.com
/aspxeditorsdemos/ListEditors/MultiSelect.aspx");
//driver.manage().window().maximize();
driver.findElement(By.id("ContentHolder_lbSelectionMode_I")).click();
driver.findElement(By.id("ContentHolder_lbSelectionModeSelenium100_DDD_L_LBI1T0")).click();
Thread.sleep(5000);
// Perform Multiple Select
Actions builder = new Actions(driver);
WebElement select =
driver.findElement(By.id("ContentHolder_lbFeatures_LBT"));
List<WebElement> options = select.findElements(By.tagName("td"));
System.out.println(options.size());
Action multipleSelect = builder.keyDown(Keys.CONTROL)
.click(options.get(2))
.click(options.get(4))
.click(options.get(6))
.build();
multipleSelect.perform();
driver.close();
         }
}

Output
Upon executing the script, the items would be selected as displayed above andthe size of the list box would also be printed in the console.






No comments:

Post a Comment