[Selenium+Java] https://www.guru99.com/using-robot-api-selenium.html

Original URL: https://www.guru99.com/using-robot-api-selenium.html

Robot Class in Selenium Webdriver

Why Robot Class?

In certain Selenium Automation Tests, there is a need to control keyboard or mouse to interact with OS windows like Download pop-up, Alerts, Print Pop-ups, etc. or native Operation System applications like Notepad, Skype, Calculator, etc.

Selenium Webdriver cannot handle these OS pop-ups/applications.

InJavaversion 1.3 Robot Class was introduced. Robot Class can handle OS pop-ups/applications.

In this tutorial, you will learn,

Benefits of Robot Class

  1. Robot Class can simulate Keyboard and Mouse Event
  2. Robot Class can help in upload/download of files when using selenium web driver
  3. Robot Class can easily be integrated with current automation framework (keyword, data-driven or hybrid)

Documentation of Robot Class

Robot Class documentation will help you to understand the basic definition, syntax and usage of all methods, and functions available in Robot Class . You can view the documentation on Official Oracle website, or you can create the documentation on your local machine.

To create the documentation on local machine, follow the steps below-

Step 1) You will find the src.zip file in JDK folder. Copy src.zip and extract the same in some other folder or directory (say D: or E: )

Step 2) Extract src folder and Navigate to (path till src folder)/src/java/awt

Step 3) Copy the current location of awt folder and open command prompt.

Step 4) In cmd, change your current directory location to awt folder and type ‘javadoc *.java‘ as shown below

Wait a while for the system to process, once completed you will see few HTML files in awt folder.

Step 5) Open index.html

Step 6) Here‘s you have full documentation of awt package, from the left navigation bar click on ‘Robot‘ hyperlink (See 1 marked in below image).

Here you can also see all the methods and interfaces of Robot Class (See 2 marked in above image).

Understanding Robot Class internal methods and usage

Robot Class methods can be used to interact with keyboard/mouse events while doing browser automation. Alternatively AutoIT can be used, but its drawback is that it generates an executable file (exe) which will only work on windows, so it is not a good option to use.

Some commonly and popular used methods of Robot Class during web automation:

  • keyPress(): Example: robot.keyPress(KeyEvent.VK_DOWN) : This method with press down arrow key of Keyboard
  • mousePress() : Example : robot.mousePress(InputEvent.BUTTON3_DOWN_MASK) : This method will press the right click of your mouse.
  • mouseMove() : Example: robot.mouseMove(point.getX(), point.getY()) : This will move mouse pointer to the specified X and Y coordinates.
  • keyRelease() : Example: robot.keyRelease(KeyEvent.VK_DOWN) : This method with release down arrow key of Keyboard
  • mouseRelease() : Example: robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK) : This method will release the right click of your mouse

Sample code to automate common use cases using Robot Class

  • Lets take example of web site http://spreadsheetpage.com/index.php/file/C35/P10/ wherein after you click on a web element (.//a[@href=contains(text(),‘yearly-calendar.xls‘]) a O.S download pop-up appears.
  • To handle this we use Robot class (by creating an instance of Robot Class in your code say Robot robot = new Robot()) . Robot class us present in AWT package of JDK.
  • To press down arrow key of Keyboard we use (robot.keyPress(KeyEvent.VK_DOWN))
  • To press TAB key of keyboard (we use robot.keyPress(KeyEvent.VK_TAB))
  • To press Enter key we use (robot.keyPress(KeyEvent.VK_ENTER)).

Here is a sample code

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;	

class Excercise1 {	

      public static void main(String[] args) throws AWTException, InterruptedException {
           WebDriver driver = new FirefoxDriver();
           driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/"); // sample url
           driver.findElement(By.xpath(".//a[@href=contains(text(),‘yearly-calendar.xls‘)]")).click();
           Robot robot = new Robot();  // Robot class throws AWT Exception
           Thread.sleep(2000); // Thread.sleep throws InterruptedException
           robot.keyPress(KeyEvent.VK_DOWN);  // press arrow down key of keyboard to navigate and select Save radio button	

           Thread.sleep(2000);  // sleep has only been used to showcase each event separately
           robot.keyPress(KeyEvent.VK_TAB);
           Thread.sleep(2000);
           robot.keyPress(KeyEvent.VK_TAB);
           Thread.sleep(2000);
           robot.keyPress(KeyEvent.VK_TAB);
           Thread.sleep(2000);
           robot.keyPress(KeyEvent.VK_ENTER);
       // press enter key of keyboard to perform above selected action
     }
 }	

Check this video to see it in action

How to execute Robot Class code using TestNG

Since, now you aware of basic methods of Robot Class so let‘s understand few more complex methods -

Suppose you do not want to use the click method for clicking at web element.

In such cases, you can use mouseMove method of the Robot class.

Step 1) mouseMove method takes x and y coordinates as parameters like robot.mouseMove(630, 420) where 630 indicates x-axis and 420 indicate y-axis. So, this method will move your mouse pointer from the current location to mentioned x and y intersection point.

Step 2) Next, we need to press the mouse button. We can use the method mousePress like robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) .

Step 3) After press, the mouse needs to be released. We can use robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK) in order to release left click of a mouse.

Running code using testNG:

Running code usingTestngrequires maven dependency of testNG or referenced library of TestNG jar file.

TestNG maven dependency:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.1.1</version>
</dependency>	

After adding maven dependency or jar file. You need to import Test annotation of testNG. Once it is all done, just Right click on the program code and click on Run As then click on TestNG… and you will find that code will start its execution using testNG API.

Here is the code

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;	

public class Excersise1 {	

    @Test
    public static void  execution() throws InterruptedException, AWTException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/"); // sample url
        Robot robot = new Robot();
        robot.mouseMove(630, 420); // move mouse point to specific location
        robot.delay(1500);        // delay is to make code wait for mentioned milliseconds before executing next step
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); // press left click
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); // release left click
        robot.delay(1500);
        robot.keyPress(KeyEvent.VK_DOWN); // press keyboard arrow key to select Save radio button
        Thread.sleep(2000);
        robot.keyPress(KeyEvent.VK_ENTER);
        // press enter key of keyboard to perform above selected action
    }
}

Check this video to see it in action

Disadvantages of Robot Class

Robot framwork has few disadvantages mentioned below:

  1. Keyword/mouse event will only works on current instance of Window. E.g. suppose a code is performing any robot class event, and during the code execution user has moved to some other screen then keyword/mouse event will occur on that screen.
  2. Most of the methods like mouseMove is screen resolution dependent so there might be a chance that code working on one machine might not work on other.

Summary

Robot class in AWT package is used to generate keyboard/mouse events to interact with OS windows and native apps.

The primary purpose of Robot is to support selenium automated tests project build in Java platform

This article is contributed by Ramandeep Singh, who is a test automation engineer at a leading MNC.

原文地址:https://www.cnblogs.com/alicegu2009/p/9098674.html

时间: 2024-10-28 21:38:26

[Selenium+Java] https://www.guru99.com/using-robot-api-selenium.html的相关文章

selenium java版本的安装方法与注意事项

Selenium driver Java版本的安装方法 下载selenium-java-2.44.0.zip压缩文件,并解压到当前文件夹,解压后打开 下载selenium-java-client-driver-1.0.2.jar 下载selenium-server-standalone-2.39.0.jar 下载并安装火狐浏览器 在火狐浏览器中安装两个插件:selenium IDE和firebug 下载并安装Eclipse(因为Eclipse安装是会自带安装jdk的所以这就不用另外再安装jdk了

[Selenium+Java] Implicit Wait &amp; Explicit Wait in Selenium

https://www.guru99.com/handling-dynamic-selenium-webdriver.html here are two types of HTML tables published on the web- Static tables: Data is static i.e. Number of rows and columns are fixed. Dynamic tables: Data is dynamic i.e. Number of rows and c

[Selenium+Java] Selenium Grid Tutorial: Command Line and JSON Example

Original URL: https://www.guru99.com/introduction-to-selenium-grid.html What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in para

[Selenium+Java] Selenium Framework: Keyword Driven &amp; Hybrid

Original from: https://www.guru99.com/creating-keyword-hybrid-frameworks-with-selenium.html What is Selenium Framework? Selenium Framework is a code structure that helps to make code maintenance easy. Without frameworks, we will place the “code” as w

[Selenium+Java] Cross Browser Testing using Selenium WebDriver

Original URL: https://www.guru99.com/cross-browser-testing-using-selenium.html What is Cross Browser Testing? Cross Browser Testing is a type of functional test to check that your web application works as expected in different browsers. Why do we nee

[Selenium+Java] Parallel Execution &amp; Session Handling in Selenium

Source URL: https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html To understand how to run scripts in parallel, let's first understand Why do we need Session Handling? During test execution, the Selenium WebDriver has to intera

[Selenium+Java] TestNG: Execute multiple Test Suites

Original URL: https://www.guru99.com/testng-execute-multiple-test-suites.html TestNG: Execute multiple Test Suites TestNG enables you to run test methods, test classes and test cases in parallel inside your project. By performing parallel execution,

[Selenium+Java] How to Use Selenium with Python: Complete Tutorial

Original URL: https://www.guru99.com/selenium-python.html How to Use Selenium with Python: Complete Tutorial Selenium supportsPythonand thus can be utilized with Selenium for testing. Python is easy compared to other programming languages, having far

[Selenium+Java] Listeners and their use in Selenium WebDriver

Original URL: https://www.guru99.com/listeners-selenium-webdriver.html TestNG Listeners in Selenium WebDriver There are two main listeners. WebDriver Listeners TestNG Listeners In this tutorial, we will discuss onTestngListeners. Here is what you wil