Sunday, December 29, 2013

Some Example Of Mouse Event And KeyBoard Event With Selenium Webdriver


Now we know how to create and access Mouse and KeyBoard event with selenium webdriver. Here we try to learn how to use different method of Mose and KeyBoard.We try to learn this to solve some problem,there may be some other better solution,if any body have the better solution please comments about this ,ok lets start:

1.Sending Text into the textbox
2.Sending Capital Text into the textbox
3.Context Click
4.Select Item From Context Menu
5.Select multiple option from multi select dropdownlist consecutively 
6.Locate The Element Coordinates and do some mouse event on this element
7.Navigate To other Element using Tab Key


1.Sending Text into the textbox:

        Actions builder=new Actions(driver);
        builder.moveToElement(txtBoxElement).click(txtBoxElement)
                .sendKeys(txtBoxElement,"Test").build().perform();

Here  we observe that moveToElement(WebElement) method used to move to the webelement then click(WebElement) method to click on this element.At last  use sendKeys(WebElement,Text) method to send string value to the textbox.


2.Sending Capital Text into the textbox
       
        Actions builder=new Actions(driver);
        builder.moveToElement(txtBoxElement).click(txtBoxElement)
                .keyDown(Keys.SHIFT).sendKeys(txtBoxElement, "capitaltesT")
                .keyUp(Keys.SHIFT).build().perform();

Here  keyDown(Keys.SHIFT) method is use to hold the shift key logically and after sending the data into the webelement free this holed down key using keyUp(Keys.SHIFT). One thing keep in mind that keyDown() method only and only used with modifier keys like SHIFT,ALT,COMMAND.


3.Context Click

        Actions builder=new Actions(driver);
        builder.moveToElement(txtBoxElement)
                   .contextClick(txtBoxElement).build().perform();


 contextClick(webElement) method is used to open the context menu of an webelement.

4.Select Item From Context Menu

        Actions builder=new Actions(driver);
        builder.contextClick(txtBoxElement)
                  .sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN
                  ,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN
                  ,Keys.RETURN).build().perform();

 After open the context menu of an particular webelement we go down, to the option of this menu using Keys.ARROW_DOWN parameter into the sendKeys() method. We send Keys.ARROW_DOWN as much as we go down of this menu,means if we want to select the 4 th option then send 4 Keys.ARROW_DOWN as a parameter.After that select the option from this menu using sending the Keys.RETURN as a parameter into the sendKeys() method.

5.Select multiple option from multi select dropdownlist consecutively

       Actions builder=new Actions(driver);
       WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
       List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
       builder.clickAndHold(dropdownlists.get(0))
                                         .moveToElement(dropdownlists.get(6)).release().build().perform();


Select dropdownlist item from where we start to select and then send it as an parameter of clickAndHold() method. Then choose the dropdownlist where we try to finish our selection and send this into the
moveToElement() method.We need to use release() metod to release the mouse click.

6.Locate The Element Coordinates And Some Mouse Event On This Element

      Locatable locatable = (Locatable) driver.findElement(By.id("loginbutton"));
      Mouse mouse = ((HasInputDevices) driver).getMouse();
      mouse.mouseMove(locatable.getCoordinates());
      mouse.click(locatable.getCoordinates());


7.Navigate To other Element using Tab Key

       Actions builder=new Actions(driver);
       builder.moveToElement(txtBoxElement)
                                 .sendKeys(txtBoxElement, Keys.TAB)
                                 .build().perform();

Here we moveToElement() used to jump over the element then sendKeys(txtBoxElement, Keys.TAB) method is used to move cursor or select the the elemen.




No comments:

Post a Comment