Python+MoneyRunner+Java实现计算器功能自动化

来源:互联网 发布:js onclick传多个参数 编辑:程序博客网 时间:2024/06/07 14:41

春节回来后,一直想写点脚本,刚好在项目测试中遇到一个小小的需求:就是要验证输入法中计算器的逻辑运算功能
针对这个需求,初步是想利用模拟按键点击的方法,来进行点击按键,按键布局可以提前算好放置到一个字典中,所以就选用了MonkeyRunner这个测试框架,那必然脚本就要使用Python了,下面是具体的步骤,以及在脚本编写中遇到的一些问题

    一.实现模拟点击已经准备好的需要验证的算式

    # coding:utf-8# 模拟点击计算器进行功能回归测试from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImageimport randomimport timeimport stringimport sysimport osfrom random import randintprint 'start:calculate regression test 'splitTime = 0.01basewidth = 720baseheight = 1280#deviceList = []def ClickIme():    keyMap = {        '7': [98,1479],    '8': [295,1479],    '9': [492,1479],    '+': [688,1479],        'delte': [885,1479],    '4': [98,1605],    '5': [295,1605],    '6': [492,1605],        '-': [688,1605],    '()': [885,1605],    '1': [98,1731],    '2': [295,1731],        '3': [492,1731],    'x': [688,1731],    'pop': [885,1731],    '0': [98, 1857],        '00': [295, 1857],    '.': [492, 1857],    '÷': [688, 1857],    '=': [885, 1857]    };    #唤醒屏幕    device=MonkeyRunner.waitForConnection()    device.wake()    device.startActivity("com.example.calctest/com.example.calctest.MainActivity")    print "start"    device.touch(300,200,"DOWN_AND_UP")    #MonkeyRunner.sleep(2)    dw = device.getProperty("display.width")    dy = device.getProperty("display.height")    screenWidth  =  int(str(dw))    screenHeight =  int(str(dy))    MonkeyRunner.sleep(1)    #device.touch(screenWidth/2,screenHeight/2,"DOWN_AND_UP")    #MonkeyRunner.sleep(1)    device.touch(50,70 + (screenHeight-(550*screenWidth/basewidth)),"DOWN_AND_UP")    MonkeyRunner.sleep(2)    device.touch(1000,600+(screenHeight-(550*screenWidth/basewidth)),"DOWN_AND_UP")    MonkeyRunner.sleep(2)    fp = open("\\calctest.txt", "rb").readlines()    for line in fp:        items = line.split()        for char in items[0]:            if char in keyMap:                pos = keyMap[char]                device.touch(pos[0],pos[1],"DOWN_AND_UP")                MonkeyRunner.sleep(0.05)        device.touch(422,1300,"DOWN_AND_UP")        MonkeyRunner.sleep(0.05)if __name__ == '__main__':    ClickIme()

    二.在Edittext中监听获取结果,并打印到sdcard/log
    编写一个apk,用来监听edittext结果区域的变化,并记录下来,关键代码片段

    // 初始化控件,添加监听    private void initView() {        edite = (EditText) findViewById(R.id.editText1);        edite.addTextChangedListener(new TextWatcher() {            @Override            public void afterTextChanged(Editable s) {                String content = s.toString();                if (content != null && content.length() > 0) {                    saveToFile(content);                    s.clear();                }            }            @Override            public void beforeTextChanged(CharSequence s, int start, int count,                    int after) {                // TODO Auto-generated method stub            }            @Override            public void onTextChanged(CharSequence s, int start, int before,                    int count) {                // TODO Auto-generated method stub            }        });    }    // 保存结果到文件    private void saveToFile(String content) {        String filePath = Environment.getExternalStorageDirectory().toString()                + "/log/log.txt";        File file = new File(filePath);        if (!file.exists()) {            file.getParentFile().mkdirs();            // file.mkdirs();        }        PrintStream fout = null;        try {            fout = new PrintStream(new FileOutputStream(file, true));            fout.println(content);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (fout != null) {                try {                    fout.close();                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }

    三. 比较log中的结果与期望结果对比

    # coding:utf-8# 计算器结果比对f1 = open("log.txt",'r')f2 = open("exec.txt",'r')f3 = open("final_result.txt",'w')for (line1,line2) in zip(f1,f2):    if line1 == line2:        continue    else:        f3.write("\t".join(line1.split()) + "\tfailed\n")           f1.close()f2.close()f3.close()

    经过上面三个过程,就可以完成刚开始说的需求了,由于自己之前写脚本少些,所以事先起来中间遇到不少问题,不过通过跟相关同学的徐诶请教,最终得以成形,个人感觉还是要多做,并加以坚持就可以搞定.

    0 0
    原创粉丝点击