Wednesday, June 26, 2024

Actions class in Selenium

 

Actions :

                        // Actions :  is predefined class in Selenium

                        // can be used to perform mouse movement operations and also keyboard related operations

 

Some common actions include moving the mouse over an element, performing drag-and-drop operations, right-clicking, double-clicking, and keyboard interactions like pressing the Shift, Ctrl, or Space keys.

 

package ActionsBasics;

 

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.Actions;

 

public class ActionsMouseOver {

 

            public static void main(String[] args) throws InterruptedException {

                        // TODO Auto-generated method stub

 

                                                //open chrome browser                    

                                                System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                                                WebDriver  driver = new ChromeDriver();

 

                                                Thread.sleep(3000);

                                                // get("url ") - to open the url in chrome browser

                                                driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/MenuDesciption.html");

 

                                                // Move mouse over  to 'W3Schools ' ele --> Actions - class

                                               

                                                Actions act  = new Actions(driver);    // 1 PC

                                                WebElement w3schoolsEle=  driver.findElement(By.id("id1"));

                                                act.moveToElement(w3schoolsEle).perform();        

                                               

//                                             act.moveToElement(w3schoolsEle);// not working

                                                // at end we have to use perform();.

                                                //if we don t use perform() at end,  mouse movement operations will not be performed

                                               

 

                                                // get text from column -2

                                                // Col2Text =W3Schools is the best Web Developers resource on the Web

                                                //                                             <a>  my gmail </a>

                                                //                                             <td> W3Schools is the best Web Developers resource on the Web          </td>

 

                                                String Col2Msg = driver.findElement(By.id("tip")).getText();

                                               

                                                System.out.println("Col2Msg="+Col2Msg);

                                                //Col2Msg=W3Schools is the best Web Developers resource on the Web             

 

                                                Thread.sleep(3000);

 

                                                //HW  Move mouse over  to 'Internet Explorer ' ele   and get column2 text

                                                ///  Col2Text =Internet Explorer is winning the browser war

 

 

                                                // HW  Move mouse over  to 'Netscape Navigator' ele  and get column2 text

 

 

            }

 

}

 

 

 

 

HW open 'https://www.amazon.com/gp/goldbox?ref_=nav_cs_gb' and  move the mouse over on 'Hello sign in 'Link in right side of the page?

 

click(): This method is used to perform a click at the current mouse location.

click(WebElement element): This method is used to perform a click on the specified web element.

In Selenium, the click() method is used to perform click actions on web elements such as buttons, links, checkboxes, and radio buttons

 

package ActionsBasics;

 

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.Actions;

 

public class ClickUsingActions {

 

            public static void main(String[] args) throws InterruptedException {

                        //open chrome browser                    

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                        Thread.sleep(3000);

                        // get("url ") - to open the url in chrome browser

               driver.get("file:///C:/brahma/Practise/SelniumPractiseNew/SampleWebpage.html");

 

                        // Click  'Male'  radio button using "actions" class

                        Actions act  = new Actions(driver);

 

                        WebElement  maleRadioBtnEle = driver.findElement(By.id("maleid"));

                        act.click(maleRadioBtnEle).perform();

 

 

                        // HW Click Female  radio btn

 

                        // click 'electronics' checkbox  using "actions"

                        WebElement electronicsCheckbox =  driver.findElement(By.id("eleId"));

                        act.click(electronicsCheckbox).perform();

 

                        // HW click ' Computers ' checkbox using actions

 

                        // HW click on ' Civil' check box using actions

 

                        //*************************************************

                        //  2 nd way   click() -- no args

                        // Clicks at the current mouse location.

                        //  move mouse over on 'FeMale'  radio btn  and click

                        WebElement  femaleRadioBtnEle = driver.findElement(By.id("femaleId"));

 

                        act.moveToElement(femaleRadioBtnEle).click().perform();

 

                        .                       // In one line , We can perform multiple actions.--  composite actions

 

 

 

                        ////HW   move mouse over on 'Civil''  checkbox   and click

 

 

 

                        // HW Click Female  radio btn using click()  without args

 

                        // HW click ''electronics' ' using click()  without args

 

 

                        //                     HW Click 'My gmail'  link using action class -  click() no args

                        //                     HW Click 'My gmail'  link using action class - click(ele)

 

                        //                                 maleRadioBtnEle.click();

                        //                                             maleRadioBtnEle.doubleclick();

                        //  we dont have doubleclick in Webelement interface

                        //                                                maleRadioBtnEle.rightclick(); //

                        //  we dont have rightclick() in Webelement interface

 

 

            }

 

}

 

·  doubleClick(): This method is used to perform a double-click at the current mouse location. It can be combined with other actions, such as moving to an element first.

 

·  doubleClick(WebElement element): This method is used to perform a double-click on the specified web element.

 

package ActionsBasics;

 

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.Actions;

 

public class DoubleClickUsingActions {

 

            public static void main(String[] args) throws InterruptedException {

                        // TODO Auto-generated method stub

                        //open chrome browser                    

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                        Thread.sleep(3000);

                        // get("url ") - to open the url in chrome browser

               driver.get("file:///C:/brahma/Practise/SelniumPractiseNew/SampleWebpage.html");

 

                        //  doubleclick  -"male" radio btn

//                     driver.findElement(By.id("femaleId")).doubleClick();

                        Actions act  = new Actions(driver);

                        WebElement femaleRadioBtnEle =  driver.findElement(By.id("femaleId"));

                       

                        act.doubleClick(femaleRadioBtnEle).perform();

 

                        // Double click 'Electronics ' checkbox

                        //                                             electronics

                        WebElement electronicsChkbBox=  driver.findElement(By.id("eleId"));

                         act.doubleClick(electronicsChkbBox).perform();

 

                        Thread.sleep(5000);

 

 

 

                        //HW  double click 'My gmail' link using actions class

 

 

            }

 

}

 

 

                       

------------------------------------------

 

HW  Move the Mouse Over the 'Female' Radio Button and Double Click?

HW Move the Mouse Over the "Open Facebook" Link and Double Click

 

Note: 

In the WebElement interface, there is only a click() method available. Methods like doubleClick() and rightClick() are not part of the WebElement interface but are available through the Actions class in Selenium

 

DragAndDrop(srcEle, targetEle) :

The Actions class in Selenium WebDriver provides a method called dragAndDrop to handle drag-and-drop operations.

This method can be used to drag an element from one location and drop it onto another element.

 

 

package ActionsBasics;

 

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.Actions;

 

public class DragAndDropBasics {

 

            public static void main(String[] args) throws InterruptedException {

                        // TODO Auto-generated method stub

                        //open chrome browser                    

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                       

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/dragdrop.html");

                        Thread.sleep(3000);

 

                        //drag and drop         

                        Actions act  = new Actions(driver);

                        WebElement srcEle=  driver.findElement(By.id("drag1"));              

                        WebElement targetEle=  driver.findElement(By.id("div1"));

//                     act.dragAndDrop(srcEle, targetEle).perform();// workin

                       

                        //                                             act.dragAndDrop(srcEle, targetEle).perform(); // not working in chrome

                        //  try in "Edge" browser -check it 

 

 

                        //  2nd way  clickAndHold(srcEle).moveToElement(targetEle)

                        //                                             act.clickAndHold(srcEle).moveToElement(targetEle).release().perform();// not ok

//                     act.clickAndHold(srcEle).moveToElement(targetEle).release().perform();

 

                        //3 --clickAndHold(srcEle).release(targetEle).

                        act.clickAndHold(srcEle).release(targetEle).perform();//   working

 

                        System.out.println("ends");

 

 

 

 

            }

 

}

 

 

----------------------------

 

HW check  draganddrop()  in Edgebrowser

 

contextClick() in Selenium WebDriver

The Actions class in Selenium WebDriver provides a method called contextClick to handle right-click actions. This method can be used to right-click on a specific element or at the current mouse location.

Method Signatures

  1. contextClick(WebElement element)
    • Right-clicks on the specified element.
  2. contextClick()
    • Right-clicks at the current mouse location

 

 

package ActionsBasics;

 

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.Actions;

 

public class ContextClickBasics {

 

            public static void main(String[] args) throws InterruptedException {

                        // TODO Auto-generated method stub

                        //open chrome browser                    

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                        Thread.sleep(3000);

                        // get("url ") - to open the url in chrome browser

               driver.get("file:///C:/brahma/Practise/SelniumPractiseNew/SampleWebpage.html");

 

                        // Right click on 'Open gmail'  link

//                     driver.findElement(By.xpath("//a[text()='My gmail']")).rightClikc();

                        Actions act = new Actions(driver);

                       

                        WebElement myGmailLinkEle=  driver.findElement(By.xpath("//a[text()='My gmail']"));

                        act.contextClick(myGmailLinkEle).perform();

 

                        // HW Right click on 'Open Facebook'  link               

 

                        // Move the mouse over on 'Open gmail'  link  and right click

 

 

 

                        System.out.println("ends");

 

 

 

            }

 

}

 

 

           

-----------------------------------------         

 // Hw right click on 'Penguins' image

 

-------------------------------------

FAQ  How can you perform right click operation ?

What is the use of contextClick()?

 

 

dragAndDropBy(WebElement source, int xOffset, int yOffset)

  • Drags the source element from its current position by the specified x and y offset.

 

package ActionsBasics;

 

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.Actions;

 

public class DragAndDropByBasics {

 

            public static void main(String[] args) throws InterruptedException {

                        //open chrome browser                    

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                        Thread.sleep(3000);

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/dragdrop2.html");

 

                        // Drag and drop image ele by x - axis 50,0

                        Actions act  = new Actions(driver);

                        WebElement srcEle = driver.findElement(By.id("moveMe"));

                        act.dragAndDropBy(srcEle, 50, 0).perform();

                        // moves the imag to x-axis i.e right side

 

                        Thread.sleep(2000);

 

                        // Drag and drop image ele by x - axis 100,0

                        act.dragAndDropBy(srcEle,100, 0).perform();

 

                        Thread.sleep(2000);

 

                        // Drag and drop image ele by x - axis 150,0

                        act.dragAndDropBy(srcEle,150, 0).perform();

 

                        Thread.sleep(2000);

 

                        // Drag and drop image ele by  y- axis 0,50

 

 

                        Thread.sleep(2000);

 

                        // Drag and drop image ele by  y- axis 0,100

                        act.dragAndDropBy(srcEle, 0, 100).perform();

 

                        Thread.sleep(2000);

 

                        // Drag and drop image ele by  y- axis 0,150

                        act.dragAndDropBy(srcEle, 0, 150).perform();

                        Thread.sleep(2000);

 

                        // Drag and drop image ele by  y- axis 0,200

                        act.dragAndDropBy(srcEle, 0, 200).perform();

 

                        Thread.sleep(2000);

 

 

                        // Hw  Drag and drop image ele  (top side)

 

                        // Hw  Drag and drop image ele left side

 

                        System.out.println("ends");

 

 

 

 

            }

 

}

 

 

                                               

 

ScrollToelement():

------------------

Introduced in Selenium 4 version  but not in Selenium-3 version

 

package ActionsBasics;

 

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.Actions;

 

public class ScrollToElementBasics {

 

            public static void main(String[] args) throws InterruptedException {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

 

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");                  

 

                        Thread.sleep(4000);

 

                        // Scroll to 'Choose File ' btn

                        Actions act =  new Actions(driver);

                        WebElement  chooseFileBtnEle =  driver.findElement(By.id("fileid"));

                        act.scrollToElement(chooseFileBtnEle).perform();

 

 

                        System.out.println("ends");

 

 

 

            }

 

}

 

 

 Mouse movement operations +  keyboard related operations

 

  movetoeElemnt(), click(), Doubleclick(), ContextClick(), draganddrop(), draganddropby(), scrollToElemnt()

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...