无聊的Chrome自动化测试方案

发表于:2016-06-29来源:Dosk技术站作者:SpringHack点击数: 标签:自动化
为什么说无聊呢!因为我是OCD晚期!不想安装别的东西(其实是mac的容量不够了T_T)

为什么说无聊呢!因为我是OCD晚期!不想安装别的东西(其实是mac的容量不够了T_T)

只有几个接口,够用了,主要是用来测试我的AlxwVJ:
UI() 是一个选择器,返回一个类似数组的存在0.0
UI.start() UI.use() UI.work() 分别是开始测试的开关,路由(参数是url和匹配结果),工作时间轴(参数是一个对象,时间 => 操作函数的形式)
扩充了Object的原型,do方法传入参数,执行相应操作(看起来比较整齐嘛2333)
下面的insert.json后面的匿名函数里包含了一个Demo,一看就懂啦2333

简单来说吧,就是一个Chrome Extension,下面直接贴代码咯:

manifest.json:
{
     "manifest_version": 2,
     "name": "A extension for automatic UI test",
     "version": "0.0.1",
     "background": {
         "scripts": ["background.js"]
     },
     "permissions": ["tabs", "http://*/*", "https://*/*"],
     "page_action": {
          "default_icon": {
               "19": "icon.png"
          },
          "default_title": "UI Test Extension"
     }
}


background.js:

function entry(tabId, changeInfo, tab) {
    chrome.pageAction.show(tabId);
    chrome.tabs.executeScript(tabId, {
        file : './insert.js',
        runAt : 'document_end'
    });
};

chrome.tabs.onUpdated.addListener(entry);


insert.js:
(function (window, document, main, undefined) {
    if (!window.UI)
    {
        console.log('Test initial ...');
        Object.prototype.do = function (action) {
            if (this[action])
                this[action]();
        };
        var UI = window.UI = function (selector) {
            return new (function () {
                var target = document.querySelectorAll(selector);
                for (var i=0;i<target.length;++i)
                    this[i] = target[i];
                this.do = function (action) {
                    target.forEach(function (item) {
                        if(item[action])
                            item[action]();
                    });
                };
                this.length = target.length;
            })();
        };
        var CAN = false;
        UI.start = function () {
            CAN = true;
        };
        UI.use = function (reg, func) {
            if (CAN)
            {
                var arr = reg.exec(location.href);
                if (arr)
                    func(location.href, arr);
            }
        };
        UI.work = function (arr) {
            for (var i in arr)
            {
                if (Number.isInteger(parseInt(i)) && !Number.isNaN(parseInt(i)))
                    setTimeout(arr[i], i);
            }
        };
        function init()
        {
            setTimeout(main, 0);
        }
        if (typeof window.onload == 'function')
        {
            var old = window.onload;
            window.onload = function () {
                old();
                init();
            };
        } else {
            window.onload = init;
        }
    }
})(window, document, function () {

    //Start
    UI.start();

    //index.php
    UI.use(/^http:\/\/59.73.145.22[:\d]+(\/index.php||\/)$/, function (url, res) {
        UI.work({
            2000 : function () {
                UI('a')[6].do('click');
            }
        });
    });

    //view.php
    UI.use(/^http:\/\/59.73.145.22[:\d]+\/view.php\?id=1$/, function (url, res) {
        UI.work({
            2000 : function () {
                UI('a')[4].do('click');
            }
        });
    });

    //submit.php
    UI.use(/^http:\/\/59.73.145.22[:\d]+\/submit.php\?id=1$/, function (url, res) {
        UI.work({
            2000 : function () {
                UI('select')[0].value = '2';
                UI('input')[1].do('click');
                UI('textarea')[0].value = '\r\
                    #include <iostream>\r\
                    using namespace std;\r\
                    int main()\r\
                    {\r\
                        int a,b;\r\
                        cin >> a >> b;\r\
                        cout << a+b << endl;\r\
                        return 0;\r\
                    }\r\
                ';
            },
            4000 : function () {
                UI('input')[0].do('click');
            }
        });
    });

});

原文转自:http://blog.90its.cn/archives/105/?utm_source=tuicool&utm_medium=referral