Puppeteer自動化測試框架(二)----環(huán)境搭建

服務(wù)器:centos7.4
docker:1.12.6
puppeteer:1.20
nodejs:9.10
本篇介紹基于docker進(jìn)行puppeteer環(huán)境搭建,與centos搭建基本一樣

1.下載centos7基礎(chǔ)鏡像

[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.下載成功后,查看一下鏡像信息,確認(rèn)下載成功

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

3.安裝nodejs

創(chuàng)建容器并以命令行進(jìn)入容器
[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
執(zhí)行下面兩條命令
[root@402c33974f57 /]# npm install -g n
[root@402c33974f57 /]# n stable
再次驗證nodejs版本(需要重新進(jìn)入容器)
[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加入環(huán)境變量

export NODE_PATH=/usr/local/lib/node_modules

7.退出容器,并將容器內(nèi)容提交到新得鏡像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.這時候我們的環(huán)境就算完成了,編寫測試代碼

//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.這時候看似很美好,但是運(yùn)行時卻給了我們當(dāng)頭一棒

[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運(yùn)行不起來,提示缺少依賴
11.解決所有依賴問題,安裝chromium

[root@388c2b859931 /]# yum install chromium

12.再次運(yùn)行腳本

[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.#/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

搞定,收工

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容