無聊的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

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97