OnetapInc / chromy

Chromy is a library for operating headless chrome. 🍺🍺🍺
MIT License
605 stars 41 forks source link

`evaluate` not accepting args #33

Closed justin-parker closed 6 years ago

justin-parker commented 6 years ago

The test below should return 1. Running document.querySelectorAll("div.content").length in the console on google.com returns 1. It appears the Chromy does not yet support args passed in to evaluate (like nightmarejs).


let chromy = new Chromy({visible:true})

var selector = "div.content";

chromy.chain()
    .goto('https://www.google.com/')
    .evaluate( function(selector) {
        return document.querySelectorAll(selector).length;
    }, selector)
    .result((r) => console.log("selector length: " + r))
    .end()
    .then(_ => chromy.close())
garris commented 6 years ago

@justin-parker You will need to do it like this...

let chromy = new Chromy({visible:true})

var selector = "div.content";

chromy.chain()
    .goto('https://www.google.com/')
        .evaluate(`_selector = '${selector}'`)
    .evaluate( function() {
        return document.querySelectorAll(_selector).length;
    }, selector)
    .result((r) => console.log("selector length: " + r))
    .end()
    .then(_ => chromy.close())
dotneet commented 6 years ago

Thanks @garris

@justin-parker I'll add an way to make it easier.

flancer64 commented 6 years ago

This works for me:

    const cssConfirm = '#maincontent > div.page.messages > div:nth-child(2) > div > div > div';
    await chromy.wait(cssConfirm);
    await chromy.evaluate(`_cssConfirm = '${cssConfirm}'`);
    await chromy.evaluate(() => {
        console.log('CSS: ' + _cssConfirm);
    });

out:

CSS: #maincontent > div.page.messages > div:nth-child(2) > div > div > div

Thanks, @garris