OnetapInc / chromy

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

No chain? #84

Open thezimmee opened 6 years ago

thezimmee commented 6 years ago

Hello. Thank you for chromy, I can't wait to get it running. I am trying to write a thin wrapper around it to pass some config to tell it to run certain test and/or screenshots. So I'm trying to conditionally call different chromy methods, but I don't see any examples that show using chromy not in a chain. I also can't find any documentation for what the chain() method actually does. To be clear, I would like to do something like this:

let chromy = new Chromy({port: port++});
let browser = chromy.chain()
  .emulate(deviceName)
  .goto(test.url);

if (runStartCallback) {
  browser.evaluate(() => {
    return onStartCallback(browser);
  });
}

if (multipleSelectorsConditionIsTrue) {
  browser.screenshotMultipleSelectors(selectors, callback, options);
}

if (documentScreenshotConditionIsTrue) {
  browser.screenshotDocument(options);
}

if (runEndCallback) {
  browser.evaluate(() => {
    return onEndCallback(browser);
  });
}

browser.end()
  .then(() => {
    chromy.close();
    console.log('DONE!');
  }).catch((error) => {
    chromy.close();
    console.log('ERROR', error);
  });

The reason I need to do something like this is to be able to pass config to conditionally do certain things and call certain callbacks methods which allow the user config to interact with chromy. I'm missing something because I'm getting various errors, a common one being:

Error: 'then' is not defined on a target object.

How can I accomplish what I am trying to do? I appreciate your help.

dotneet commented 6 years ago

Hi @holmescn, thank you for using chromy.

Basically your code seems to have a problem. But I'm curious of the part of the code. where are onStartCallback() and onEndCallback() defined? The function passed to .evaluate() is executed with browser context. So can't call a function or variable in nodejs context.

The chain is implemented by async-chain-proxy package. You can check the behavior of chain by accessing this package directly.

alii commented 5 years ago

@dotneet would it be possible to pass data to evaluate like this:

await chromy.evaluate((data) => {window.blah(data)});

Edit: or is there another way?

dotneet commented 5 years ago

Hi @aabbccsmith,

evaluate can receive arguments. https://github.com/OnetapInc/chromy/blob/master/test/evaluate.js#L52