Friday, 4 April 2014

Java AWT Robot | Selenium Uses


Note|  Text in Blue [#PYTHON] and Orange [#JAVA] can be edited or mentioned important for the entire blog. All the posts are practically done by me.


Java Robot

This is the continuation of one my previous topics, link here.  Selenium doesn't give support on automating or handling Browser pop-ups and Native OS pop-ups.

There are 3 ways to make it work.
     1| Java.awt.Robot Toolkit
     2| 3rd party tools like AutoIT [check link here]
     3| Browser Profile [check link here]




Fig. Browser PopUp

Java Robot handles Mouse Actions and Keyboard Actions. For example, some of the most important functions are as follows:

      1| keyPress()
      2| keyRelease()
      3| mousePress()
      4| mouseRelease()
      5| Mousemove()




Handle Browser Pop-up


Note| 
1| The below snippet is used to save a file from Download Browser Pop-up shown above in Fig.
2| The below co-ordinates are set for Screen resolution | 1920 x 1080
3| Window status | Maximized
4| Customize the xy co-ordinates in-case if you are using different screen resolution


Robot r = new Robot();
// click Save File 
r.mouseMove(787, 544); // move to co-ordinate Location
r.mousePress(InputEvent.BUTTON1_MASK); // Left Mouse click - Press
r.mouseRelease(InputEvent.BUTTON1_MASK); // Left Mouse click - Release 
r.delay(5); // wait for 5 millisecs
// click ok
r.mouseMove(1032, 641); // move to co-ordinate Location
r.mousePress(InputEvent.BUTTON1_MASK); // Left Mouse click - Press
r.mouseRelease(InputEvent.BUTTON1_MASK); // Left Mouse click - Release





Robot Functions


InputEvent.BUTTON1_MASK // Left button
InputEvent.BUTTON2_MASK // Middle button
InputEvent.BUTTON3_MASK // Right button



Middle click | Press & Release
Robot r = new Robot();
r.mousePress(InputEvent.BUTTON2_MASK); 
r.mouseRelease(InputEvent.BUTTON2_MASK);


Right click | Press & Release
r.mousePress(InputEvent.BUTTON3_MASK); 
r.mouseRelease(InputEvent.BUTTON3_MASK);


Scroll Mouse
r.mouseWheel(7);


Pixel Color | RBG
System.out.println(r.getPixelColor(923, 87));
OUTPUT|
java.awt.Color[r=142,g=209,b=224]


Get Current Mouse position [location]
System.out.println(MouseInfo.getPointerInfo().getLocation());
OUTPUT|
java.awt.Point[x=735,y=633]


Get Screen Resolution | Dimension
System.out.println(Toolkit.getDefaultToolkit().getScreenSize());
OUTPUT|
java.awt.Dimension[width=1920,height=1080]


Screen Capture
java.awt.Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot r = new Robot();
BufferedImage img = r.createScreenCapture(new Rectangle(size));
File path = new File("C://screen.jpg");
ImageIO.write(img, "JPG", path);
or|
Robot r = new Robot();
BufferedImage img = r.createScreenCapture(new Rectangle(0, 0, 100, 100));
File path = new File("C://screen.jpg");
ImageIO.write(img, "JPG", path);


Enter/Type Text
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;

Robot r = new Robot();     
driver.findElement(By.id("Value")).click();  
typeKeys("Prashanth Sams", r);

public static void typeKeys(String str,Robot r)
{
 for(int i=0;i<str.length();i++)
{
  typeCharacter(r, ""+str.charAt(i));
}
}
public static void typeCharacter(Robot robot, String letter)
{
 try
{
  boolean upperCase = Character.isUpperCase( letter.charAt(0) );
  String variableName = "VK_" + letter.toUpperCase();
  Class clazz = KeyEvent.class;
  Field field = clazz.getField( variableName );
  int keyCode = field.getInt(null);

  robot.delay(1000);

  if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );

  robot.keyPress( keyCode );
  robot.keyRelease( keyCode );

  if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
 }
 catch(Exception e)
 {
  System.out.println(e);
 }
 }


Keyboard Actions
r.keyPress(KeyEvent.VK_ENTER); // Press Enter Key

Find all/rest of the Java Robot keyboard actions here
Refer| Robot


PS|
Mofiki's Coordinate Finder will help you to get the screen coordinates.

19 comments:

  1. It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.

    ReplyDelete

  2. There is a great blog about selenium and thanks for sharing about selenium this information is used for to now the selenium commands and sevices seleniumonlinetraining

    ReplyDelete
  3. Thanks for nice topic. Very useful information.
    IthubOnlineTraining are good in giving the selenium onlineTraining

    ReplyDelete
  4. Thanks for the great information in your blog Selenium Training in Chennai

    ReplyDelete

  5. Thank u for your information. I read your shared information on selnium Topic.
    Selenium Online Training

    ReplyDelete
  6. Robot doesn't work while handling download popup in IE browser .Any idea ?

    ReplyDelete
  7. Well explained. Got to learn new things from your Blog on Appium.
    Appium training in chennai

    ReplyDelete
  8. I cant wait to check out some of these blogs! I’ve really wanted to start learning more about cars and auto repairs lately and I think this will help a lot. I think it can save my family some money if we knew how to do some repairs at home.! Thanks again for all the options.

    bike spa services in mumbai
    house cleaning services in mumbai
    car wash services in mumbai

    ReplyDelete
  9. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.selenium training in chennai

    ReplyDelete

  10. Thanks for posting this useful content, Good to know about new things here, Let me share this, . VMWare training in pune

    ReplyDelete
  11. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete
  12. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  13. I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.

    Digital Marketing Company in Chennai

    ReplyDelete