Start the browser
- Start the browser: headless: false
- Open the developer console: devTools: true
- Custom browser width height: Page.setViewPort
- Produces two TAB pages
- The official open page: await browser.pages() produces two TAB pages, one target TAB and one blank
- After modification :(await browser.pages())[0], only open target TAB page
async init() {
await this.openPage();
await this.createCer();
}
async openPage() {
// Open the browser
browser = await puppeteer.launch({
headless: false.// Open the interface,
devtools: true.// Open the developer console
});
// Open a blank page
page = (await browser.pages())[0];
try {
// Set the browser window
await page.setViewport({
width: 1300.height: 938});// Jump to the destination page
await page.goto("http://127.0.0.1/demo.html");
} catch (error) {
await this.openPage();
throw new Error('Request page timed out, try to reconnect'); }}Copy the code
The operation page
- WaitFor (utilFun. Random (1000, 3000)) : await page.waitfor (utilFun.
- $eval() = page.$eval()
- Want to manipulate a DOM element: page.evaluate()
- To obtain dom elements accurately, you can use setTimeout to delay the dom element for about 10 seconds before performing the corresponding operation
- Let reg = new RegExp(
${username}
);
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) {
// Click on the sidebar type
await page.waitFor(utilFun.random(1000.3000));
await page.click(ele);
// Click Add to add the IOS certificate
await page.waitFor(utilFun.random(1000.3000));
await page.click(".toolbar-button.add");
// Check whether the radio can be clicked
await page.waitFor(utilFun.random(1000.3000));
const radioDisabled = await page.$eval("#type-development-0".async el => {
return el.disabled;
});
// If the number of certificates reaches the limit, delete them first and then increase them
if (radioDisabled) {
// Click on the sidebar type
await page.waitFor(utilFun.random(1000.3000));
await page.click(`${ele}`);
// Delete the IOS certificate
await page.waitFor(utilFun.random(1000.3000));
await this.deleteCer();
} else {
// Add an IOS certificate
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)) {
/ / name
let name = table.rows[i].cells[j].innerText;
/ / type
let type = table.rows[i].cells[j + 1].innerText;
/ / deadline
let expires = table.rows[i].cells[j + 2].innerText;
// Click the dropdown
table.rows[i].click();
/ / click the Revoke
setTimeout((a)= > {
document.querySelector(".button-no-padding-top.small.revoke-button").click();
}, 1000);
// Click pop-up Revoke
setTimeout((a)= > {
document.querySelector(".ui-dialog-content.ui-widget-content .button.small.red.ok").click();
}, 3000);
}
}
}
}, username);
}
Copy the code
Upload a file
// Click select file
await page.waitFor(utilFun.random(1000.3000));
const upload_file = await page.$("input[type=file]");
await upload_file.uploadFile("Your file path");
Copy the code
File download
// Download the IOS certificate
await this.downloadFile("Your file path");
await page.waitFor(utilFun.random(1000.3000));
await page.click(".button.small.blue");
Copy the code