Windows上用虚拟机、Python和按键精灵实现自动测试Shell代码

def press_shift_and_number(file, char):  
    file.write(f'KeyDown "Shift", 1\nDelay 50\nKeyDown "{char}", 1\nDelay 50\nKeyUp "{char}", 1\nDelay 50\nKeyUp "Shift", 1\nDelay 50\n')  


def write_key_presses(key_sequences):  
    special_chars = {  
        '!': '1',  
        '@': '2',  
        '#': '3',  
        '$': '4',  
        '%': '5',  
        '^': '6',  
        '&': '7',  
        '*': '8',  
        '(': '9',  
        ')': '0',
        '{': "[",
        '}': "]",
        '+': "=",
        '"': "'",
        '<': ",",
        '>': ".",
        '~': "`",
        '?': "/",
        '_': '-',
        '|': '\\'
    }  
  
    with open('script.txt', 'w') as file:  
        file.write('Delay 10000\n')  
        for sequence in key_sequences:  
            sequence = sequence.strip().rstrip('\n')
            if sequence != "":
                for char in sequence:  
                    if char in special_chars:  
                        press_shift_and_number(file, special_chars[char])
                    elif ord(char) >= ord("A") and ord(char) <= ord("Z"):
                        file.write(f'KeyDown "Shift", 1\nDelay 50\nKeyDown "{char.lower()}", 1\nDelay 50\nKeyUp "{char.lower()}", 1\nDelay 50\nKeyUp "Shift", 1\nDelay 50\n')
                    else:
                        if char == ' ':
                            char = 'Space'
                        file.write(f'KeyDown "{char}", 1\nDelay 50\nKeyUp "{char}", 1\nDelay 50\n')
                file.write('KeyDown "Enter", 1\nDelay 50\nKeyUp "Enter", 1\nDelay 50\n')

file_name = 'install_libs.txt'
with open(file_name, 'r', encoding='utf-8') as file:
    lines = file.readlines()
file = file_name.split('.')[0]
input_strings = [f"cat << EOF >{file + '.sh'}"]
input_strings.extend(lines)
check_script = f"sh {file + '.sh'}"
input_strings.extend(["EOF", check_script])
write_key_presses(input_strings)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值