让前端飞Front-End

puppeteer踩坑经验之谈

2019-02-19  本文已影响0人  谭瞎

启动浏览器

async init() {
    await this.openPage();
    await this.createCer();
}

async openPage() {

    // 打开浏览器
    browser = await puppeteer.launch({
        headless: false, // 开启界面,
        devtools: true,  // 开启开发者控制台   
    });

    // 打开一个空白页
    page = (await browser.pages())[0];

    try {

        // 设置 浏览器视窗
        await page.setViewport({
            width: 1300,
            height: 938,
        });

        // 跳转 目的页
        await page.goto("http://127.0.0.1/demo.html");

    } catch (error) {
        await this.openPage();
        throw new Error('请求页面超时,尝试重新连接');
    }
}

操作页面

async createCer() {
   
    const type = this.type;

    const Development = "#ios-nav > li:nth-child(1) ul > li:nth-child(3)";
    const Production = await page.$("#ios-nav > li:nth-child(1) ul > li:nth-child(4)");

    switch (type) {
        case "dev":
            await this.addIosCertificates(Development);
            break;
        case "dis":
            await this.addIosCertificates(Production);
            break;
        default:
            break
    }
}

async addIosCertificates(ele) {

    // 点击 侧边栏 类型
    await page.waitFor(utilFun.random(1000, 3000));
    await page.click(ele);

    // 点击 add 添加IOS证书
    await page.waitFor(utilFun.random(1000, 3000));
    await page.click(".toolbar-button.add");

    // 判断 radio 是否能点击
    await page.waitFor(utilFun.random(1000, 3000));
    const radioDisabled = await page.$eval("#type-development-0", async el => {
        return el.disabled;
    });

    // 如果证书数量满额,先删除,后增加
    if (radioDisabled) {

        // 点击 侧边栏 类型
        await page.waitFor(utilFun.random(1000, 3000));
        await page.click(`${ele}`);

        // 删除 IOS证书
        await page.waitFor(utilFun.random(1000, 3000));
        await this.deleteCer();
    } else {
        // 增加 IOS证书
        await this.addCer();
    }
    
}

async deleteCer() {
    await page.evaluate(async (username) => {
        let tableInfo = "";
        let reg = new RegExp(`${username}`);
        const table = document.querySelectorAll(".data-table")[1].querySelector("tbody");

        for (let i = 0; i < table.rows.length; i++) {
            for (let j = 0; j < table.rows[i].cells.length; j++) {
                tableInfo = table.rows[i].cells[j].innerText;
                if (reg.test(tableInfo) && (i % 2 == 0)) {
                    // 名字
                    let name = table.rows[i].cells[j].innerText;
                    // 类型
                    let type = table.rows[i].cells[j + 1].innerText;
                    // 期限
                    let expires = table.rows[i].cells[j + 2].innerText;

                    // 点击 下拉
                    table.rows[i].click();

                    // 点击 Revoke
                    setTimeout(() => {
                        document.querySelector(".button-no-padding-top.small.revoke-button").click();
                    }, 1000);

                    // 点击 弹窗 Revoke
                    setTimeout(() => {
                        document.querySelector(".ui-dialog-content.ui-widget-content .button.small.red.ok").click();
                    }, 3000);

                }
            }
        }

    }, username);
}

上传文件

// 点击 选择文件
await page.waitFor(utilFun.random(1000, 3000));
const upload_file = await page.$("input[type=file]");
await upload_file.uploadFile("你的文件路径");

文件下载

// 下载 IOS 证书
await this.downloadFile("你的文件路径");

await page.waitFor(utilFun.random(1000, 3000));
await page.click(".button.small.blue");
上一篇下一篇

猜你喜欢

热点阅读