-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (65 loc) · 2.39 KB
/
app.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
const puppeteer = require('puppeteer');
let contents = null
let url = "https://www.mobly.com.br"
const cookie = {
name: 'showNewsLetterThisSession',
value: 'false',
url: url
};
console.log('Starting...');
(async () => {
// const browser = await puppeteer.launch();
const browser = await puppeteer.launch({headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox']})
const page = await browser.newPage()
await page.setViewport({ width: 1024, height: 768 })
await page.setCookie(cookie)
await page.goto(url)
let productsData = await searchFor('sofa', page)
productsData = productsData.sort( comparePrices )
console.log('ordered by price ===> OK');
await addToCart(productsData[0], page)
await page.goto(url)
productsData = await searchFor('cadeira', page)
productsData = productsData.sort( comparePrices )
console.log('ordered by price ===> OK');
await addToCart(productsData[0], page)
await page.screenshot({path: 'result.png'});
console.log('Printing result ===> OK');
await browser.close();
})();
function comparePrices( a, b ) {
if ( a.last_nom < b.last_nom ){
return -1;
}
if ( a.last_nom > b.last_nom ){
return 1;
}
return 0;
}
async function addToCart(element, page) {
await page.goto(element.url)
console.log(element.url + ' ===> OK');
await page.waitForFunction('!document.querySelector(".sel-cart-add-button").disabled')
await page.click('.bt-buy')
console.log('buyed ===> OK');
await page.waitForSelector('a.close-keep-buying', {visible: true})
console.log('Closed Modal ===> OK');
}
async function searchFor(term, page) {
let productsData = [];
await page.waitForSelector('#searchInput', {visible: true})
await page.focus('#searchInput')
await page.type('#searchInput', term)
await page.click('#search-button')
console.log('starting search by '+term+' ===> OK');
await page.waitForSelector('.sel-catalog-product-list-item')
console.log('results by '+term+' ===> OK');
let products = await page.$$('.sel-catalog-product-list-item')
console.log('found '+products.length+' ===> OK');
for (let i = 0; i < products.length; i++) {
let a = await products[i].$$eval('div .itm-link', nodes => nodes.map(n => n.href))
let price = await products[i].$$eval('div .itm-price-current', nodes => nodes.map(n => n.innerText))
productsData.push( {url: a[0], price: parseFloat(price[0].replace(',','.'), 2) } );
}
return productsData;
}