selenium demo(四)
Robot类处理方法
1 package test; 2 3 import java.awt.*; 4 import java.awt.event.KeyEvent; 5 import java.io.IOException; 6 7 8 public class AutoTestFour { 9 10 11 public static void pressKeyWithShift(Robot robot,int keyvalue){ 12 robot.keyPress(KeyEvent.VK_SHIFT); 13 robot.keyPress(keyvalue); 14 robot.keyRelease(keyvalue); 15 robot.keyRelease(KeyEvent.VK_SHIFT); 16 } 17 public static void closeApplication(Robot robot){ 18 robot.keyPress(KeyEvent.VK_ALT); 19 robot.keyPress(KeyEvent.VK_F4); 20 robot.keyRelease(KeyEvent.VK_F4); 21 robot.keyRelease(KeyEvent.VK_ALT); 22 } 23 public static void pressKey(Robot robot,int keyvalue){ 24 robot.keyPress(keyvalue); 25 robot.keyRelease(keyvalue); 26 } 27 public static void main(String[] args) throws AWTException, IOException { 28 try { 29 Robot robot = new Robot(); 30 Runtime.getRuntime().exec("notepad"); 31 robot.delay(2000); 32 for (int i = 0; i < 100; i++) { 33 pressKeyWithShift(robot, KeyEvent.VK_I); 34 pressKey(robot, KeyEvent.VK_SPACE); 35 pressKey(robot, KeyEvent.VK_A); 36 pressKey(robot, KeyEvent.VK_M); 37 pressKey(robot, KeyEvent.VK_SPACE); 38 pressKey(robot, KeyEvent.VK_T); 39 pressKey(robot, KeyEvent.VK_H); 40 pressKey(robot, KeyEvent.VK_E); 41 pressKey(robot, KeyEvent.VK_SPACE); 42 pressKeyWithShift(robot, KeyEvent.VK_J); 43 pressKey(robot, KeyEvent.VK_A); 44 pressKey(robot, KeyEvent.VK_V); 45 pressKey(robot, KeyEvent.VK_A); 46 pressKeyWithShift(robot, KeyEvent.VK_R); 47 pressKey(robot, KeyEvent.VK_O); 48 pressKey(robot, KeyEvent.VK_B); 49 pressKey(robot, KeyEvent.VK_O); 50 pressKey(robot, KeyEvent.VK_T); 51 pressKey(robot, KeyEvent.VK_ENTER); 52 } 53 closeApplication(robot); 54 } 55 catch (AWTException e){ 56 e.printStackTrace(); 57 } 58 } 59 }