Search On Google

Tuesday, February 19, 2019

Selenium mouse and keyboard event

Keyboard and mouse event?

clickAndHold()
Clicks (without releasing) at the current mouse location
contextClick()
Right click mouse action.
doubleClick()
Performs a double-click at the current mouse location.
dragAndDrop(source, target)
For drag drop action
dragAndDropBy(source, x-offset, y-offset)
here you can give offset of target for drag drop action.
keyDown(modifier_key)
This method perform to modifier key press. As we know modifier keys are ALT, SHIFT, CTRL
keyUp(modifier _key)
This method perform to modifier key release. As we know modifier keys are ALT, SHIFT, CTRL
moveToElement(toElement)
Moves the mouse to given element.
moveByOffset(x-offset, y-offset)
Moves the mouse to given offset.
release()
Release left pressed mouse button.
sendKeys(onElement, charsequence)
Sends  keystrokes onto the element.

Mouse and keyboard event example in selenium:-

"package pkg1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class MyClass910_mouse_event { 
    public static void main(String[] args) {       
   
        System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");   
        WebDriver driver = new ChromeDriver();   

        driver.get("https://www.onlinetutorial.info/p/selenium-mouse-event.html");
        WebElement element1 =driver.findElement(By.linkText(""Link-a""));
        Actions builder = new Actions(driver);
        Action actionseries = builder.moveToElement(element1).click().build();
        //The build() method is always the final method used so that all the listed actions will be compiled into a single step.
         actionseries.perform();
       
       
         WebElement element2 =driver.findElement(By.linkText(""Link-b""));
         Action actionseries2 = builder.moveToElement(element2).click().build();
         //The build() method is always the final method used so that all the listed actions will be compiled into a single step.
          actionseries2.perform();
     
    }
}
"

Mouse and keyboard event with multiple actions example in selenium:-

"package pkg1;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class MyClass910_mouse_event_Large { 
    public static void main(String[] args) {       
   
        System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");   
        WebDriver driver = new ChromeDriver();   

        driver.get(""C:\\Users\\Desk-105\\Desktop\\mouse_event.html"");
     
        WebElement username =driver.findElement(By.id(""userID""));
     
        Actions builder = new Actions(driver);
        Action actionseries = builder.moveToElement(username)
                       .click()
                       .keyDown(username, Keys.SHIFT)
                       .sendKeys(""pradeep_yadav"")
                       .keyUp(username, Keys.SHIFT)
                       .doubleClick(username)
                       .contextClick()
                       .build();
               
                actionseries.perform();
       
       
 
     
    }
}
"

No comments:

Post a Comment

About Me

My photo
Mumbai, Maharashtra, India