zoukankan      html  css  js  c++  java
  • win_diy_monkey demo

    import os, time, random, threading, subprocess, uiautomation
    from PIL import ImageGrab
    from pymouse import PyMouse
    from pykeyboard import PyKeyboard


    class Monkey_test:

    def __init__(self):
    self.mouse = PyMouse()
    self.keyboard = PyKeyboard()
    self.waitime = 0.1
    self.left_x = 0
    self.left_y = 0
    self.right_x = 0
    self.right_y = 0
    self.calc_window = None
    self.sum = 0

    def random_pos(self):
    x = random.randint(self.left_x, self.right_x)
    y = random.randint(self.left_y + 50, self.right_y - 50)
    self.mouse.move(x, y)
    time.sleep(self.waitime)
    return x, y

    def random_click(self):
    x, y = self.random_pos()
    self.mouse.click(x=x, y=y, button=1, n=1)
    time.sleep(self.waitime)

    def random_double_click(self):
    x, y = self.random_pos()
    self.mouse.click(x=x, y=y, button=1, n=2)
    time.sleep(self.waitime)

    def random_right_click(self):
    x, y = self.random_pos()
    self.mouse.click(x=x, y=y, button=2, n=1)
    time.sleep(self.waitime)

    def random_input(self):
    content = ['A', 'B', 'a', 'b', '1', '2', '%', '']
    index = random.randint(0, len(content) - 1)
    self.keyboard.type_string(content[index])
    time.sleep(self.waitime)

    def random_key(self):
    key_list = [self.keyboard.enter_key, self.keyboard.backspace_key, self.keyboard.alt_key,
    self.keyboard.shift_key, self.keyboard.up_key, self.keyboard.right_key]
    key_lists = [[self.keyboard.control_key, 'c'], [self.keyboard.shift_key, 's'], [self.keyboard.alt_key, 'a'],
    [self.keyboard.function_keys[4]], [self.keyboard.control_key, self.keyboard.alt_key, 'a']]
    index = random.randint(0, len(key_list) - 1)
    indexs = random.randint(0, len(key_lists) - 1)
    num = random.randint(0, 20)
    if num <= 10:
    self.keyboard.press_key(key_list[index])
    self.keyboard.release_key(key_list[index])
    else:
    self.keyboard.press_keys(key_lists[indexs])
    self.keyboard.release_key(key_lists[indexs][0])
    # self.keyboard.release_key(key_lists[indexs][1])

    def start_app(self, cmd):
    os.system('start /b ' + cmd)
    time.sleep(self.waitime)
    self.calc_window = uiautomation.WindowControl(ClassName='ApplocationFrameWindow', searchDepth=1)
    self.calc_window.Maximize()
    self.left_x, self.left_y, self.right_x, self.right_y = self.calc_window.BoundingRectangle
    time.sleep(5)

    def start_test(self, count):
    self.start_app('calc.exe')
    for i in range(count):
    random_num = random.randint(1, 100)
    if random_num < 80:
    self.random_click()
    elif random_num < 70:
    self.random_input()
    elif random_num < 10:
    self.random_double_click()
    elif random_num < 5:
    self.random_right_click()
    else:
    self.random_key()
    self.sum += 1
    time.sleep(3)
    subprocess.check_output('taskkill /f /IM calc.exe')

    def chek_run(self):
    time.sleep(10) # 等待应用启动后线程再启动
    while True:
    # 监控进程
    tasklist = subprocess.check_output('tasklist') # 输入命令查进程
    print(tasklist)
    if "calc.exe" in tasklist:
    print('进程存在')
    elif self.sum == count:
    break
    else:
    print('崩溃了')
    break
    # 截图
    now_time = time.strftime('%Y-%m-%d %H:%M:%S')
    path = os.path.abspath('.') + '\images\' + now_time + '_.png'
    ImageGrab.grab().save(path)
    # 窗口置顶
    uiautomation.SetForegroundWindow(self.calc_window.Handle)
    time.sleep(3) # 隔三秒


    if __name__ == '__main__':
    count = 100
    monkey = Monkey_test()
    t1 = threading.Thread(target=monkey.start_test(count,))
    t2 = threading.Thread(target=monkey.chek_run())
    t1.start()
    t2.start()
  • 相关阅读:
    【杭电】[4883]TIANKENG’s restaurant
    【杭电】[4883]TIANKENG’s restaurant
    【POJ】[1423]Big Number
    【POJ】[1423]Big Number
    【杭电】[1280]前m大的数
    【杭电】[1280]前m大的数
    【杭电】[1576]A/B
    【杭电】[1576]A/B
    [leetcode]346. Moving Average from Data Stream滑动窗口平均值
    [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
  • 原文地址:https://www.cnblogs.com/yaohu/p/12593324.html
Copyright © 2011-2022 走看看