chromy icon indicating copy to clipboard operation
chromy copied to clipboard

No chain?

Open thezimmee opened this issue 8 years ago • 3 comments

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.

thezimmee avatar Nov 09 '17 21:11 thezimmee

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.

dotneet avatar Nov 10 '17 22:11 dotneet

@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?

alii avatar Feb 21 '19 10:02 alii

Hi @aabbccsmith,

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

dotneet avatar Feb 22 '19 02:02 dotneet