Skip to content

Commit

Permalink
chalenge acepted
Browse files Browse the repository at this point in the history
  • Loading branch information
migueljatinha committed May 7, 2019
1 parent 9c93deb commit b4fbba3
Showing 1 changed file with 59 additions and 51 deletions.
110 changes: 59 additions & 51 deletions adding-to-cart/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const puppeteer = require('puppeteer');

let contents = null;
let contents = null
let url = "https://www.mobly.com.br"

const cookie = {
Expand All @@ -13,61 +13,69 @@ 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.setCookie(cookie);
await page.goto(url);

await page.waitForSelector('#searchInput');

await page.focus('#searchInput');
await page.type('#searchInput', 'sofa');
await page.click('#search-button');

await page.waitFor(2000);
await page.waitForSelector('.sel-catalog-product-list-item')

let productClass = await page.$('.sel-catalog-product-list-item')
let a = await productClass.$$eval('div .itm-link', nodes => nodes.map(n => n.href));
let price = await productClass.$$eval('div .itm-price-current', nodes => nodes.map(n => n.innerText));


await page.goto(a[0]);
await page.waitForFunction('!document.querySelector(".sel-cart-add-button").disabled')

await page.click('.bt-buy');
await page.waitForSelector('a.close-keep-buying');
await page.waitFor(3000);
await page.click('a.close-keep-buying');
await page.focus('#searchInput');
await page.type('#searchInput', 'cadeira');
await page.click('#search-button');
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();

await page.waitFor(2000);
await page.waitForSelector('.sel-catalog-product-list-item')
})();

productClass = await page.$('.sel-catalog-product-list-item')
a = await productClass.$$eval('div .itm-link', nodes => nodes.map(n => n.href));
price = await productClass.$$eval('div .itm-price-current', nodes => nodes.map(n => n.innerText));
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.goto(a[0]);
await page.waitForFunction('!document.querySelector(".sel-cart-add-button").disabled')
await page.click('.bt-buy')
console.log('buyed ===> OK');

await page.click('.bt-buy');
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.click("#radioTypeCompany");
// await page.click("#RegistrationForm_state_tax_number_free");

// await page.type("#RegistrationForm_legal_name", 'Legal name');
// await page.type("#RegistrationForm_fantasy_name", 'Legal name');
// await page.type(".company-email", '[email protected]');
// await page.type("#RegistrationForm_password", '123456');
// await page.type("#RegistrationForm_password2", '123456');
//
// await page.type("#RegistrationForm_company_tax_identification","00000000000191");
// await page.select("#RegistrationForm_fk_customer_address_region", '41');
// await page.select("#RegistrationForm_fk_customer_segment", '13');
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;
}

0 comments on commit b4fbba3

Please sign in to comment.