1. import puppeteer from 'puppeteer';
    2. /**
    3. * 自动登录
    4. * -----
    5. * @param {*} page
    6. */
    7. const autoLogin = async (page) => {
    8. try {
    9. await page.waitForSelector('.ant-modal-confirm-btns .ant-btn-primary');
    10. } catch (error) {
    11. console.log(error, "没有等到弹窗")
    12. }
    13. page.waitFor(320)
    14. await page.click('.ant-modal-confirm-btns .ant-btn-primary');
    15. await page.waitForSelector('.ant-input-affix-wrapper #contact_phone');
    16. try {
    17. await page.type('.ant-input-affix-wrapper #contact_phone', '17700000000');
    18. await page.type('.ant-input-affix-wrapper #password', 'qingxie');
    19. await page.click('.ant-form-item-children .ant-btn');
    20. } catch (error) {
    21. // console.log(error)
    22. }
    23. }
    24. const addProduct = async (page, index) => {
    25. try {
    26. await page.waitForSelector('.ant-form-item-control > .ant-form-item-children > .ant-select > .ant-select-selection > .ant-select-selection__rendered')
    27. const selectBtn = await page.$('.ant-form-item-control > .ant-form-item-children > .ant-select > .ant-select-selection > .ant-select-selection__rendered')
    28. await selectBtn.click()
    29. await page.waitForSelector('div > .ant-select-dropdown .ant-select-dropdown-menu > .ant-select-dropdown-menu-item')
    30. const list = await page.$$('div > .ant-select-dropdown .ant-select-dropdown-menu > .ant-select-dropdown-menu-item')
    31. await list[(index % list.length)].click()
    32. await page.waitFor(200)
    33. await page.type('.ant-form-item-children #b_product_name', `${index}___自动测试产品名称${index}`);
    34. await page.type('.ant-form-item-children #b_product_intro', `${index}___自动测试产品介绍content:loremloremloremloremloremloremlorem`);
    35. await page.type('.ant-form-item-children .ant-input-number-input', "100");
    36. await page.click('.ant-form-item-children .ant-btn')
    37. } catch (error) {
    38. // console.log(error)
    39. }
    40. }
    41. jest.retryTimes(1200000);
    42. describe('产品线', () => {
    43. it('添加产品~~~', async () => {
    44. const browser = await puppeteer.launch({
    45. // 是否不显示浏览器, 为true则不显示
    46. // headless: false,
    47. headless: true,
    48. defaultViewport: null,
    49. timeout: 0,
    50. });
    51. const page = await browser.newPage();
    52. await page.goto('http://localhost:8000/productlinelist/addproduct');
    53. await autoLogin(page)
    54. for (let i = 0; i < 1000; i++) {
    55. await addProduct(page, i)
    56. await page.waitFor(300)
    57. }
    58. // await page.close();
    59. // browser.close();
    60. }, 1200000);
    61. });

    image.png

    https://zhaoqize.github.io/puppeteer-api-zh_CN/
    https://blog.csdn.net/weixin_30273501/article/details/96225521
    https://www.cnblogs.com/paris-test/p/9713782.html