Skip to content

Commit

Permalink
fix: 修复puppeteer的API变动而失效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Oct 24, 2017
1 parent a701785 commit 0ef0493
Show file tree
Hide file tree
Showing 38 changed files with 303 additions and 621 deletions.
72 changes: 35 additions & 37 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const pTimeout = require('p-timeout');
const chalk = require('chalk');
const config = require('./config');
const util = require('./utils');

Expand All @@ -20,7 +22,10 @@ class App {

while (files.length) {
const file = files.shift();
this.provider(require(path.join(dir, file)));
const absFilePath = path.join(dir, file);
const Provider = require(absFilePath);
Provider.file = absFilePath;
this.provider(Provider);
}

return this;
Expand All @@ -29,6 +34,7 @@ class App {
// open the browser
this.browser = await puppeteer.launch({
headless: config.isProduction
// devtools: true
});

// create a new tab
Expand Down Expand Up @@ -59,39 +65,20 @@ class App {

await this.page.evaluate(() => {
const title = document.title;
window.addEventListener('mousemove', e => {
document.title = `(${e.x},${e.y})${title}`;
});
window.addEventListener('mousemove', e => (document.title = `(${e.x},${e.y})${title}`));
});

await this.page.deleteCookie();

// 如果resolve超过xxx秒,则认为是超时,不会无限等待
await new Promise(async (resolve, reject) => {
let haveResponse = false;
entity
.resolve(this)
.then(() => {
if (haveResponse === false) {
haveResponse = true;
this.__timer__ && clearTimeout(this.__timer__);
resolve();
}
})
.catch(err => {
if (haveResponse === false) {
haveResponse = true;
this.__timer__ && clearTimeout(this.__timer__);
reject(err);
}
});
this.__timer__ = setTimeout(() => {
if (haveResponse === false) {
reject(new Error(`Resolve time out...`));
}
this.__timer__ && clearTimeout(this.__timer__);
}, 1000 * 60);
});
// 60s超时用于处理发送短信,不会导致无线等待的情况...
await pTimeout(entity.resolve(this), 1000 * 60)
.then(() => {
util.log(chalk.green('[Success]:'), entity.name);
})
.catch(err => {
util.log(chalk.red('[Fail]:'), entity.name);
if (err) console.error(err);
});
} catch (err) {
console.error(err);
} finally {
Expand All @@ -105,24 +92,35 @@ class App {
}
async bootstrap() {
const entities = this.providers
.map(Provider => new Provider())
.map(Provider => {
// 实例化服务提供者
const entity = new Provider(this);
const fileInfo = path.parse(Provider.file);
// 设置每个服务提供者的文件和名字
entity.name = fileInfo.name;
entity.file = Provider.file;
return entity;
})
.filter(entity => entity.active === true);

const aloneEntity = entities.find(entity => entity.alone);

// 如果找到设置alone属性的provider,则单独运行,方便调试
if (aloneEntity && !this.options.isProduction) {
this.entities = [aloneEntity];
} else {
this.entities = entities;
}

if (this.options.once === true) {
if (this.options.once) {
return await this.run();
}

// run forever
while (process) {
await this.run();
} else {
while (true) {
await this.run();
await util.sleep(1000 * 10);
}
// take a rest then let's go...
await util.sleep(1000 * 10);
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions app/provider.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
class Provider {
constructor(name) {
this.name = name;
constructor(ctx) {
// 服务者提供的名字
this.name = 'Provider';
// 执行上下文
this.ctx = ctx;
// 该provider是否激活
this.active = true;
// 是否单独运行,用于调试
this.alone = false;
// 服务提供者的文件绝对路径
this.file = __filename;
}
async resolve(phone) {}
}
Expand Down
19 changes: 8 additions & 11 deletions app/providers/lofter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ module.exports = class extends Provider {
const options = ctx.options;
const page = ctx.page;

const [$mobile, $code, $submit] = await Promise.all([
page.$('#phone-num'),
page.$('#auth-code'),
page.$('#btn-auth')
]);

await $mobile.click();
await page.type(options.phone, { delay: 300 });
await page.type('#phone-num', options.phone, { delay: 100 });

await utils.sleep(1000);

await $code.click();
await page.type(options.phone, { delay: 100 });
await page.type('#auth-code', options.phone, { delay: 50 });

await utils.sleep(1000);

await $submit.click();
throw new Error(`Ivano`);

await page.click('#btn-auth');

// 检验是否发送成功
await page.waitForSelector('#btn-auth.btn-disabled', { timeout: 1000 * 3 });
}
};
22 changes: 7 additions & 15 deletions app/providers/qq.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Provider = require('../provider');
const utils = require('../utils');

module.exports = class extends Provider {
constructor() {
Expand All @@ -9,21 +10,12 @@ module.exports = class extends Provider {
const options = ctx.options;
const page = ctx.page;

const [$nickname, $password, $phone, $send] = await Promise.all([
page.$('#nickname'),
page.$('#password'),
page.$('#phone'),
page.$('#send-sms')
]);
await page.type('#nickname', options.username, { delay: 100 });
await page.type('#password', options.password, { delay: 100 });
await page.type('#phone', options.phone, { delay: 100 });
await page.click('#send-sms', { button: 'left' });

await $nickname.click({ button: 'left' });
await page.type(options.username, { delay: 100 });

await $password.click({ button: 'left' });
await page.type(options.password, { delay: 100 });

await $phone.click({ button: 'left' });
await page.type(options.phone, { delay: 100 });
await $send.click({ button: 'left' });
// 检验是否发送成功
await page.waitForSelector('.send-sms.disabled', { timeout: 1000 * 3 });
}
};
9 changes: 5 additions & 4 deletions app/providers/segmentfault.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Provider = require('../provider');
const utils = require('../utils');

module.exports = class extends Provider {
constructor() {
Expand All @@ -10,11 +11,11 @@ module.exports = class extends Provider {

const page = ctx.page;

const [$mobile, $submit] = await Promise.all([page.$('[name="phone"]'), page.$('.getCode')]);
await page.type('[name="phone"]', options.phone, { delay: 100 });

await $mobile.click();
await page.type(options.phone, { delay: 100 });
await page.click('.getCode');

await $submit.click();
// 检验是否发送成功
await page.waitForSelector('.getCode[disabled]', { timeout: 1000 * 3 });
}
};
27 changes: 8 additions & 19 deletions app/providers/久其格格.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,17 @@ module.exports = class extends Provider {

const page = ctx.page;

const $phoneRegister = await page.$('#phoneRegistTab');
await page.click('#phoneRegistTab');

await $phoneRegister.click();
await page.waitForSelector('#chkCodeSendBtn');

await utils.sleep(1000);
await page.type('#UserName', options.phone, { delay: 100 });
await page.type('#Password', options.password, { delay: 100 });
await page.type('#ConfirmPassword', options.password, { delay: 100 });

const [$mobile, $password, $repassword, $submit] = await Promise.all([
page.$('#UserName'),
page.$('#Password'),
page.$('#ConfirmPassword'),
page.$('#chkCodeSendBtn')
]);
await page.click('#chkCodeSendBtn');

await $mobile.click();
await page.type(options.phone, { delay: 100 });

await $password.click();
await page.type(options.password, { delay: 100 });

await $repassword.click();
await page.type(options.password, { delay: 100 });

await $submit.click();
// 检验是否发送成功
await page.waitForSelector('#chkCodeSendBtn[disabled]', { timeout: 1000 * 3 });
}
};
43 changes: 13 additions & 30 deletions app/providers/优视.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,24 @@ module.exports = class extends Provider {
const options = ctx.options;
const page = ctx.page;

const [
$mobile,
$password,
$rePassword,
$submit
] = await Promise.all([
page.$('#registerName'),
page.$('#password'),
page.$('#confirmPassword'),
page.$('#getCodeBtn')
]);

await $mobile.click();
await page.type(options.phone, { delay: 100 });

await $password.click();
await page.type(options.password, { delay: 100 });

await $rePassword.click();
await page.type(options.password, { delay: 100 });
await page.type('#registerName', options.phone, { delay: 50 });
await page.type('#password', options.password, { delay: 50 });
await page.type('#confirmPassword', options.password, { delay: 50 });

// 按下鼠标,拖动滚动条
await page.mouse.move(515, 400);
await page.mouse.click(515, 400);
await page.mouse.down({
button: 'left'
});

await page.mouse.move(800, 400, { steps: 50 });

await page.mouse.up({ button: 'left' });
await page.mouse.move(515, 400, { steps: 10 });
await page.mouse.down();
await page.mouse.move(546, 410, { steps: 20 });
await page.mouse.move(648, 408, { steps: 30 });
await page.mouse.move(768, 406, { steps: 40 });
await page.mouse.move(800, 406, { steps: 50 });
await page.mouse.up();
// 松开鼠标

await utils.sleep(1000);

await $submit.click();
await page.click('#getCodeBtn');

await page.waitForSelector('#getCodeBtn.btn_code');
}
};
23 changes: 6 additions & 17 deletions app/providers/优酷.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,11 @@ module.exports = class extends Provider {
const options = ctx.options;
const page = ctx.page;

const [$mobile, $password, $repassword, $submit] = await Promise.all([
page.$('#passport'),
page.$('#password'),
page.$('#repeatPsd'),
page.$('#getMobileCode')
]);
await page.type('#passport', options.phone, { delay: 30 });
await page.type('#password', options.password, { delay: 30 });
await page.type('#repeatPsd', options.password, { delay: 30 });
await page.click('#getMobileCode');

await $mobile.click();
await page.type(options.phone, { delay: 100 });

await $password.click();
await page.type(options.password, { delay: 100 });

await $repassword.click();
await page.type(options.password, { delay: 100 });

await $submit.click();
await page.waitForSelector('#getMobileCode[disable]');
}
}
};
13 changes: 4 additions & 9 deletions app/providers/六月游戏.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Provider = require('../provider');
const utils = require('../utils');

module.exports = class extends Provider {
constructor() {
Expand All @@ -7,17 +8,11 @@ module.exports = class extends Provider {
}
async resolve(ctx) {
const options = ctx.options;

const page = ctx.page;

const [$mobile, $submit] = await Promise.all([
page.$('[name="phone"]'),
page.$('#send_sms')
]);

await $mobile.click();
await page.type(options.phone, { delay: 100 });
await page.type('[name="phone"]', options.phone, { delay: 100 });
await page.click('#send_sms');

await $submit.click();
await utils.sleep(1000 * 600);
}
};
18 changes: 4 additions & 14 deletions app/providers/向日葵.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@ module.exports = class extends Provider {
const options = ctx.options;
const page = ctx.page;

const [$name, $mobile, $submit] = await Promise.all([
page.$('#mobilebox_name'),
page.$('#mobilebox_phone'),
page.$('.contact-submit')
]);

await $name.click({ button: 'left' });
await page.type(options.name, { delay: 100 });

await $mobile.click();
await page.type(options.phone, { delay: 100 });

await $submit.click({ button: 'left' });
await page.type('#mobilebox_name', options.name, { delay: 30 });
await page.type('#mobilebox_phone', options.phone, { delay: 30 });
await page.click('.contact-submit');
}
}
};
Loading

0 comments on commit 0ef0493

Please sign in to comment.