zoukankan      html  css  js  c++  java
  • 【自动化__GUI自动化】__java__模拟功能操作__猴子测试

    一、代码如下

    package www.woniu.auto;
    
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import java.io.IOException;
    import java.util.Random;
    
    public class MonkeyTest {
    	Robot robot= null;
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		MonkeyTest monkey= new MonkeyTest();
    		try {
    			monkey.robot= new Robot();
    			Runtime.getRuntime().exec("calc.exe");//IOException
    			
    			for(int i=0; i<100; i++) {
    				monkey.move();
    				Thread.sleep(1000); //InterruptedException
    				monkey.mouse();
    				Thread.sleep(1000);
    				monkey.keyBoard();
    				Thread.sleep(1000);
    			}
    			
    		} catch (AWTException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    	
    	
    	//键盘随机输入
    	private void keyBoard() {
    		// TODO Auto-generated method stub
    		int[] keys= {0x30, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
    		int index= (int)(Math.random()*keys.length);
    		this.robot.keyPress(keys[index]);
    		this.robot.keyRelease(keys[index]);
    	}
    	
    	private void mouse() {
    		// TODO Auto-generated method stub
    		int random= (int)(Math.random()*10);
    		if(random >= 5) {
    			this.robot.mousePress(InputEvent.BUTTON1_MASK);
    			this.robot.mouseRelease(InputEvent.BUTTON1_MASK);
    		}else {
    			this.robot.mousePress(InputEvent.BUTTON3_MASK);
    			this.robot.mouseRelease(InputEvent.BUTTON3_MASK);
    		}
    
    	}
    	
    	
    	private void move() {
    		// TODO Auto-generated method stub
    		Random myrandom= new Random();
    		int x= myrandom.nextInt(40);
    		int y= myrandom.nextInt(40);
    		robot.mouseMove(x, y);
    	}
    	
    
    }
    
  • 相关阅读:
    17111 Football team
    Train Problem I (HDU 100题纪念)
    迷宫问题
    图形点扫描
    看病要排队(stl)
    水果
    Prime Ring Problem
    N皇后问题
    2^x mod n = 1
    Queuing
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7468092.html
Copyright © 2011-2022 走看看