美文网首页
Puppeteer自动化测试框架(二)----环境搭建

Puppeteer自动化测试框架(二)----环境搭建

作者: MR_Hanjc | 来源:发表于2018-04-05 22:28 被阅读518次

    服务器:centos7.4
    docker:1.12.6
    puppeteer:1.20
    nodejs:9.10
    本篇介绍基于docker进行puppeteer环境搭建,与centos搭建基本一样

    1.下载centos7基础镜像

    [hjc@localhost ~]$ docker pull centos
    Using default tag: latest
    Trying to pull repository docker.io/library/centos ...
    latest: Pulling from docker.io/library/centos
    5e35d10a3eba: Pull complete
    Digest: sha256:dcbc4e5e7052ea2306eed59563da1fec09196f2ecacbe042acbdcd2b44b05270
    

    2.下载成功后,查看一下镜像信息,确认下载成功

    [hjc@localhost ~]$ docker images
    REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
    docker.io/centos                      latest              2d194b392dd1        4 weeks ago         195.4 MB
    

    3.安装nodejs

    创建容器并以命令行进入容器
    [hjc@localhost ~]$ docker run -it centos
    
    安装epel源
    [root@402c33974f57 /]# yum -y install epel-release
    
    安装nodejs
    [root@402c33974f57 /]# yum install -y nodejs
    
    安装完成后验证一下是否安装成功与版本号
    [root@402c33974f57 /]# node -v
    v6.12.3
    可以看到安装成功了,但是由于puppeteer需要大于6.4版本,所以我们需要升级node
    
    执行下面两条命令
    [root@402c33974f57 /]# npm install -g n
    [root@402c33974f57 /]# n stable
    再次验证nodejs版本(需要重新进入容器)
    [root@402c33974f57 /]# node -v
    v9.10.1
    

    4.puppeteer最终需要下载chromium,直接下载很可能下载失败,所以这里先安装cnpm

    [root@402c33974f57 /]# npm install -g cnpm --registry=https://registry.npm.taobao.org
    

    5.安装puppeteer

    [root@402c33974f57 /]# cnpm install -g puppeteer
    

    6.将node_modules加入环境变量

    export NODE_PATH=/usr/local/lib/node_modules
    

    7.退出容器,并将容器内容提交到新得镜像mypuppeteer

    [root@402c33974f57 node_modules]# exit
    exit
    [hjc@localhost ~]$ docker commit -m "first" 402c33974f57 mypuppeteer
    

    8.查看镜像列表,可以看到镜像列表多了一个mypuppeteer得镜像

    [hjc@localhost ~]$ docker images
    REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
    mypuppeteer                           latest              25a309186531        11 seconds ago      846.5 MB
    

    9.这时候我们的环境就算完成了,编写测试代码

    //index.js
    const puppeteer = require('puppeteer');
    
    async function run()  {
        const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
        const page = await browser.newPage();
        await page.on("request",(resource)=>{
           let url=resource.url();
            console.log(url);
        });
        await page.goto('http://www.so.com');
    
        await browser.close();
    };
    run();
    

    10.这时候看似很美好,但是运行时却给了我们当头一棒

    [root@388c2b859931 /]# node index.js
    (node:32) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
    /usr/local/lib/node_modules/puppeteer/.local-chromium/linux-543305/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory
    
    
    TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
    
        at onClose (/usr/local/lib/node_modules/puppeteer/lib/Launcher.js:246:14)
        at Interface.helper.addEventListener (/usr/local/lib/node_modules/puppeteer/lib/Launcher.js:235:50)
        at Interface.emit (events.js:185:15)
        at Interface.close (readline.js:379:8)
        at Socket.onend (readline.js:152:10)
        at Socket.emit (events.js:185:15)
        at endReadableNT (_stream_readable.js:1106:12)
        at process._tickCallback (internal/process/next_tick.js:114:19)
    (node:32) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
    (node:32) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    

    chromium运行不起来,提示缺少依赖
    11.解决所有依赖问题,安装chromium

    [root@388c2b859931 /]# yum install chromium
    

    12.再次运行脚本

    [root@388c2b859931 /]# node index.js
    http://www.so.com/
    https://www.so.com/
    https://s.ssl.qhimg.com/ssl/cbd979a8ccca7f4a.css
    https://p.ssl.qhimg.com/t019476167c0ee1a803.png
    https://p.ssl.qhimg.com/t01f12091d80d748c4c.png
    https://p.ssl.qhimg.com/t01512497e6e7151b1f.png
    https://s.qhupdate.com/sou/home.gif?guid=A792D197047F2931498490DBEC2813CC.1522938220632&pro=so&pid=home&sid=&mod=&q=&abv=&src=&ref=&gid=&hid=&qid=&dpi=800_600&dm=www.so.com&ls=&p1=1&t=1522938220929
    data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAgA0JaQAA3AA/vv9UAA=
    https://s.qhupdate.com/so/click.gif?guid=A792D197047F2931498490DBEC2813CC.1522938220632&pro=so&pid=home&sid=&mod=newSkin&q=&abv=&src=&type=loadSkin&p1=2018qmj&p2=0&t=1522938220935
    https://p.ssl.qhimg.com/t016b9a582dc0da5403.png
    https://socm.dmp.360.cn/HUID.set?_=1522938220968
    https://notify.ssl.so.com/v1/report?callback=jQuery18307115173881404568_1522938220894&tmp=1522938220974&action=normal&device_type=pc&guid=15484592.4110425610372361000.1522938220908.2725&token=5d9ce92a1a92081dcc36797ecc2ba4a0cfbea5c5&_=1522938220980
    https://open.onebox.so.com/Dataapi?callback=jQuery18307115173881404568_1522938220895&query=%E5%A4%A9%E6%B0%94&type=weather&ip=123.118.74.137&src=soindex&d=pc&url=weather&_=1522938221021
    

    搞定,收工

    相关文章

      网友评论

          本文标题:Puppeteer自动化测试框架(二)----环境搭建

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