美文网首页
[Airtest]6.[win]Record screen wh

[Airtest]6.[win]Record screen wh

作者: elf_fff | 来源:发表于2023-03-28 16:13 被阅读0次

    Airtest provides recording screen API only for Android, that's why we need to write recording tool ourselves.

    1.Basic recording function

    I find a recording screen tool with python. In this way, I can get a record.avi file with the whole screen activities.

    import numpy as np
    import cv2
    from PIL import ImageGrab
    import pyautogui as pyag
    
    fourcc = cv2.VideoWriter_fourcc("X", "V", "I", "D")
    out = cv2.VideoWriter('record.avi', fourcc, 50, pyag.size())
    while True:
        img = ImageGrab.grab()
        img_np = np.array(img)
        frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
        out.write(frame)
        cv2.imshow('screen', frame)
        if cv2.waitKey(1) == 27:   # press Esc to end recording
             break
    out.release()
    cv2.destroyAllWindows()
    

    Hint:
    pip install opencv-python for cv2
    pip install Pillow for PIL, which is built-in in Python3

    2.Multi threading

    We can use a dedicated threading for recording when testing proceeds.

    import threading
    
    class RecordingTool:
        @staticmethod
        def start_recording():
            # above recording codes...
    
    threading.Thread(target=RecordingTool.start_recording).start()
    # call testing...
    

    3.How to end recording

    Recording can be ended by pressing Esc key, which means we can't feel free to press Esc key during testing. We need to find another way——thread event.
    threading.Event() will create a event flag, which is False in default.
    event.set() will set the flag to True
    event.clear() will set the flag to False
    event.isSet() will tell you if the flag is True
    OK, the code can be changed to:

    class RecordingTool:
        @staticmethod
        def start_recording():
            # recording codes...
    
    event = threading.Event()
    threading.Thread(target=RecordingTool.start_recording).start()
    # call testing...
    event.set()
    

    4.Avoid Exception effect

    The ending recording code couldn't be executed when testing code throw an exception. We can use try...except...finally... module to avoid the effect.
    And in order to make the code more readable, we add a end_recording method. The code will finally look like:

    event = threading.Event()
    
    class RecordingTool:
        @staticmethod
        def start_recording():
            # recording codes...
        
        @staticmethod
        def end_recording():
            event.set()
    
    try:
        threading.Thread(target=RecordingTool.start_recording).start()
        # call testing...
    except Exception as e:
        raise Exception(str(e))
    finally:
        threading.Thread(target=RecordingTool.stop_recording).start()
    

    相关文章

      网友评论

          本文标题:[Airtest]6.[win]Record screen wh

          本文链接:https://www.haomeiwen.com/subject/wvqxddtx.html