mik*_*see 4 javascript continuous-integration automated-tests bitbucket-pipelines puppeteer
我正在尝试运行 non-headless puppeteer 来测试管道中的 chrome 扩展。
当我用谷歌搜索这个话题时,我发现很多人能够让无头木偶操纵者在管道上工作,但由于某种原因,我无法让它与非无头人偶一起工作。
Puppeteer 故障排除文档说 TravisCI 是可能的,所以管道也应该是可能的?
我尝试了许多不同的 docker 图像,但似乎没有一个起作用。这是我目前的设置:
image: node:9
pipelines:
branches:
staging:
- step:
script:
- node -v
- yarn -v
- yarn install
- apt update && apt install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
- apt-get install -y xvfb
- export DISPLAY=:99.0
- Xvfb :99 -ac &
- yarn start build.staging
- yarn start test.unit
- yarn start test.e2e.staging
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时:
export const loadBrowser = async () => {
browser = await puppeteer.launch({
headless: false,
args: [
`--disable-extensions-except=${absDistPath}`,
`--load-extension=${absDistPath}`,
"--user-agent=PuppeteerAgent",
"--no-sandbox",
"--disable-setuid-sandbox"
]
});
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
TimeoutError:尝试连接到 Chrome 时在 30000 毫秒后超时!唯一保证工作的 Chrome 修订版是 r594312
有没有人设法让非无头 Puppeteer 真正在 Bitbucket Pipelines 上工作?
circlci 的人构建了一个很好的 docker 镜像,可以帮助处理无头木偶操作。我用它来测试 circlCI 和 bitbucket 管道。
一个非常简单的 mocha/chai 测试文件,我没有为 circlCI 和 bitbucket 管道测试配置任何 puppeteer 参数。
// index.js
module.exports = {
async getLocation(page) {
return page.evaluate(() => window.location.href);
},
};
// test.js
const { expect, assert } = require('chai');
const puppeteer = require('puppeteer');
const example = require('./index');
describe('Puppeteer', () => {
before(async function () {
this.browser = await puppeteer.launch();
this.page = await this.browser.newPage();
});
after(async function () {
await this.browser.close();
process.exit(0);
});
describe('Startup', () => {
it('should start', async function () {
assert.equal('object', typeof this.browser);
});
});
describe('In Browser', () => {
it('url should be blank', async function () {
const url = await example.getLocation(this.page);
expect(url).to.include('about:blank');
});
it('url should have example.com', async function () {
await this.page.goto('http://example.com');
const url = await example.getLocation(this.page);
expect(url).to.include('example.com');
});
});
});
Run Code Online (Sandbox Code Playgroud)
image: circleci/node:8.12.0-browsers
pipelines:
default:
- step:
caches:
- node
script:
- yarn install
- yarn run lint
- yarn run test
Run Code Online (Sandbox Code Playgroud)
circleci/node:8.12.0-browsers,它们的Dockerfile。幸运的是,我上面提到的两个 dockerfile 都提供了 Xvfb。你只需要使用它们。代码还必须有沙箱参数才能工作。
添加参数:
this.browser = await puppeteer.launch({
headless: false,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
})
Run Code Online (Sandbox Code Playgroud)
用以下替换测试线,
xvfb-run -a --server-args="-screen 0 1024x768x24" yarn run test
Run Code Online (Sandbox Code Playgroud)
结果:
