Sunday 18 May 2014

Actions In Selenium using Selenium WebDriver

 

There are different kind of actions can be perform in selenium webdriver. So every action needs to build and then it will be ready to perform. The actions will be responsible for every action performed in browser. In most of the cases we will use .Build() and .perform() after initialization of every new action.

So following are the actions that can be perform:


1. To build multiple actions in a order and then return the result of that particular action:

new Actions(driver).Build();

2. To execute currently built action and no return:

new Actions(driver).Perform();

3. To perform particular action on the browser to be perform:

new Actions(driver).Build().Perform(); 

It is better to specify by locator using findelement.

4. To click the mouse on last know mouse co-ordinates and return the actions:

new Actions(driver).Click();

5. To click the mouse on specified element and return the actions:

new Actions(driver).Click(draggable);

6. To click and hold the mouse button on last known mouse co-ordinates and return the actions:

new Actions(driver).ClickAndHold();

7. To drag-and -drop from one element to another and that return the actions:

new Actions().DragAndDrop(draggable, droppable);

8. To drag-and-drop to specified offset on one element and that returns the actions:

new Actions(driver).DragAndDropToOffset(draggable, 25, 35);

9. To move the mouse over specified element and that return the actions:

new Actions(driver).MoveToElement(draggable);

10. To move the mouse to the specified offset of the top left corner of the specified element and that returns the actions:

new Actions(driver).MovetoElement(draggable, 25, 35);

11. To release the mouse button at the last known mouse co-oridinates and that return the actions:

new Actions(driver).Release();

12 To release the mouse button on specified element and that returns the actions:

new Actions(driver).Release(draggable);

13. To send the multiple keystrokes in specific order to the browser and that return the actions:

new Actions(driver). SendKeys(Keys.Alt) 

In this above syntax key parameter is string.

14. To send sequence of key strokes to the specified element in the browser and that return the actions:

new Actions(driver) .SendKeys(draggable, Keys.Alt);

In this above syntax key parameter as string.

15. To send a modifier key down message to the browser and that return the actions:

new Actions(driver).KeyDown(Keys.Alt)

16. To send a modifier key down message to the specified element in the browser and that return the actions:

 new Actions(driver).KeyDown(draggable, Key.Alt);

17. To send a modifier key up message to the browser and that return the actions:

new Actions(driver).KeyUp(Keys.Alt)

18. To send a modifier key up message to the specified element in the browser and that return the actions:

 new Actions(driver).KeyUp(draggable, Key.Alt);  

 

Now we will see how different actions we can use in selenium.

 

public class demoActions {

        WebDriver driver;

        @Before

        public void setup() {

            driver = new FirefoxDriver();

            driver.get("http://newtours.demoaut.com/");

            driver.manage().window().maximize();

        }

   /* @After

    public void tearDown(){

        wd.quit();

    }*/

        @Test

        public void shouldDoUnitTesting() {

            WebElement uName = driver.findElement(By.name("userName"));

            WebElement pwd = driver.findElement(By.name("password"));

            WebElement login = driver.findElement(By.name("login"));

            Actions actions = new Actions(driver);

            Action build = actions.moveToElement(uName)

                    .click(uName)

                    .keyDown(Keys.SHIFT)

                    .sendKeys("s")

                    .keyUp(Keys.SHIFT)

                    .sendKeys("oftedge100")

                    .keyDown(Keys.CONTROL)

                    .sendKeys("a" +"c")

                    .keyUp(Keys.CONTROL)

                    .click(pwd)

                    .keyDown(Keys.CONTROL)

                    .sendKeys("v")

                    .keyUp(Keys.CONTROL)

                    .click(login)

                    .build();

                    build.perform();

        }

    }

Thanks to Savita Shinde....

 

Select Language