测试框架和自动化测试(jest和 puppeteer)

夏星阑
2023-12-01

测试框架和自动化测试(jest和 puppeteer)

//安装对应工具包
npm install puppeteer-core
//安装Carlo
npm i carlo
//或 yarn add carlo
npm i jest -D
npm i puppeteer -D

测试用例(test文件夹):
XX.spec.js

const puppeteer = require('puppeteer-core');
//find_chrome模块来源于GoogleChromeLabs的Carlo,可以查看本机安装Chrome目录,详细请查看底部博客,

const findChrome = require('/carlo/lib/find_chrome');

(async () => {
  let findChromePath = await findChrome({});
  let executablePath = findChromePath.executablePath;
 
  console.log(executablePath)
  const browser = await puppeteer.launch({
    executablePath,
    headless: false
  });

  const page = await browser.newPage();
  await page.goto('http://www.baidu.com');
  /*
  	dosomeThing
  */

  await browser.close();
})();


// setup.js
const puppeteer = require('puppeteer');
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const os = require('os');
const findChrome = require('/carlo/lib/find_chrome');
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
module.exports = async function() {
  let findChromePath = await findChrome({});
  let executablePath = findChromePath.executablePath;
  console.log(executablePath)
  const browser = await puppeteer.launch({
    executablePath,
    headless: false
  });
  
  // this global is only available in the teardown but not in TestEnvironments
  global.__BROWSER_GLOBAL__ = browser;
  // use the file system to expose the wsEndpoint for TestEnvironments
  mkdirp.sync(DIR);
  fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
};
// puppeteer_environment.js
const NodeEnvironment = require('jest-environment-node');
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const os = require('os');
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
class PuppeteerEnvironment extends NodeEnvironment {
  constructor(config) {
    super(config);
  }
  async setup() {
    await super.setup();
    // get the wsEndpoint
    const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8');
    if (!wsEndpoint) {
      throw new Error('wsEndpoint not found');
    }
    // connect to puppeteer
    this.global.__BROWSER__ = await puppeteer.connect({
      browserWSEndpoint: wsEndpoint,
    });
  }
  async teardown() {
    await super.teardown();
  }
  runScript(script) {
    return super.runScript(script);
  }
}
module.exports = PuppeteerEnvironment;
//devices.js
export default  {
    "name":"mate 10",
    "userAgent": "Mozilla/5.0 (Linux; Android 8.1.0; ALP-AL00 Build/HUAWEIALP-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/63.0.3239.83 Mobile Safari/537.36 T7/10.13 baiduboxapp/10.13.0.11 (Baidu; P1 8.1.0",
    
}

启动(调用浏览器的窗口,打开相应页面):
npm run test (package.json中配置启动命令 script:{
test: "})

可以参考文档:
https://www.bookstack.cn/read/jest-v24.1/17.md
https://pptr.dev/#?product=Puppeteer&version=v3.0.4&show=api-pagetitle

https://blog.csdn.net/w20101310/article/details/95491180

 类似资料: