-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
89 lines (76 loc) · 2.52 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const PageObject = require('./steps/pupInit');
const debug = require('debug')('spider:init');
const sleep = require('util').promisify(setTimeout);
/**
* 1.打开浏览器
* 2.打开页面
* 3.跳转到某个网址
* 4.Do Something
* 5.关闭浏览器
* **/
const baseUrl ="http://192.168.2.61:88/";
const entryUrl = baseUrl + "customer/account/login/";
const productUrl = baseUrl + "simple-silver-stackable-promise-ring-carve-heart.html";
const checkoutUrl = baseUrl + "checkout/onepage/";
const ShippingUrl = baseUrl + "checkout/onepage/saveShippingMethod/";
const uerInfo = {
email: "[email protected]",
passwd: "yourpass"
};
const pupPage = new PageObject({
headless : false,//是否显示浏览器
mobile: false,//是否以手机模式运行
viewport:{width: 1200, height: 720}//浏览器窗体大小
});
(async function(){
debug('任务开始');
await pupPage.init();
let page = pupPage.page;
await page.goto(entryUrl,{
waitUntil: 'networkidle0' // 网络空闲说明已加载完毕
});
//login 输入用户名和密码
await page.type('#email', uerInfo.email);
await page.type('#password', uerInfo.passwd);
//提交表单
await Promise.all([
page.$eval('#login-form', form => form.submit()),
page.waitForNavigation({ waitUntil: 'networkidle0'})
]);
//勾选要买的产品
await page.goto(productUrl, {
waitUntil: 'networkidle0' // 网络空闲说明已加载完毕
});
//提交购物车
await Promise.all([
page.select('#attribute380', '1515'),//选择select
page.$eval('#product_addtocart_form', form => form.submit()),
page.waitForNavigation({ waitUntil: 'networkidle0'})
]);
//结算
await page.goto(checkoutUrl, {
waitUntil: 'networkidle0' // 网络空闲说明已加载完毕
});
//选择运输方式
await sleep(1500);
const shipRd = (await page.$$('input[name="shipping_method"]'))[0];
await Promise.all([
page.waitForResponse(ShippingUrl),//等待ajax执行完毕
shipRd.click(),
page.click('#shippingSave'),
page.waitForResponse(ShippingUrl)//等待ajax执行完毕
]);
//选择支付方式,下单
await sleep(1000);
await Promise.all([
page.waitForResponse(ShippingUrl),
page.click('#p_method_cashondelivery'),
]);
await page.screenshot({
path: 'oliviaso.png',
type:'png'
});
browser.close();
debug('任务结束');
process.exit(0);//结束任务
})()