Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chromy.wait(fn) in script context #20

Closed
garris opened this issue Jun 30, 2017 · 6 comments
Closed

chromy.wait(fn) in script context #20

garris opened this issue Jun 30, 2017 · 6 comments

Comments

@garris
Copy link
Contributor

garris commented Jun 30, 2017

Hi @dotneet, I discovered that .wait(fn) runs in the chrome window context. Is there a way to run this in the chromy script context?

@garris
Copy link
Contributor Author

garris commented Jul 1, 2017

Ok, I have worked around this one. This option may be useful to some devs later on -- but probably not an important feature. Closing issue. Thanks.

@garris garris closed this as completed Jul 1, 2017
@AkA84
Copy link

AkA84 commented Mar 12, 2018

This option may be useful to some devs

Here's one of them. I'd like to implement casperJS' waitwhilevisible method, so I thought of doing something like this

chromy.click('.some-button');
chromy.wait(function () {
  return !chromy.visible('.loading-spinner'); // wait for the loading spinner to disappear
});
chromy.click('.somewhere-else');

but of course it doesn't work. Is there anyway to achieve the above?

@dotneet
Copy link
Contributor

dotneet commented Mar 13, 2018

chromy object can not be accessed in the function passed in .wait()
here is example.

chromy.click('.some-button')
            .wait(function () {
              let dom = document.querySelector('.loading-spinner')
              return dom !== null && dom.offsetWidth > 0 && dom.offsetHeight > 0
            })
            .click('.somewhere-else')
            .end()

@AkA84
Copy link

AkA84 commented Mar 13, 2018

thank you very much @dotneet for your reply!

Yes i figured that out eventually, but now the problem is that given that the .wait() function works in a different context, I could not even abstract the "until visible" bit into a separate reusable function (or at least, i couldn't figure out how)

var selector = // some selector that you get programmatically

chromy
  .click('.some-button')
  .wait(whileVisible(selector))
  .click('.somewhere-else')
  .end();

function whileVisible(selector) {
  return function () {
    var dom = document.querySelector(selector); // can't access `selector`

    return dom === null || (dom.offsetWidth <= 0 && dom.offsetHeight <= 0);
  };
}

I'm trying to migrate from CasperJS to Chromy to maintain a BackstopJS suite of more than 120 tests, and it's not really feasible to write something like that over and over across all tests. The same applies if I wanted to implement something similar to waituntilvisible, given that currently wait just checks if the element is in the tree, not if it's visible

Is there any way to do something like that ^ currently in Chromy? And are there any plans to either allow .wait() to run in the script context or to add different waitFor/waitUntil variations?

Thanks a lot!

EDIT: also it's not really just a matter of avoiding copy-pasting, I have some code like this that I can't figure out how to make it work

/**
 * Opens a tab on the page, and waits for the content of the tab to be on the DOM and visible
 */
function openTab (tabId) {
  var tab = require('./tabs/' + tabId);

  chromy.click('#' + tab.id);

  // this doesn't work, because i need the element to also be visible, not just in the DOM tree
  chromy.wait(tab.readySelector); 

  // this doesn't work, because the `wait()` function can't access `tab.readySelector`
  chromy.wait(function () {
    var dom = document.querySelector(tab.readySelector);
    return dom !== null && dom.offsetWidth > 0 && dom.offsetHeight > 0
  });
}

@dotneet
Copy link
Contributor

dotneet commented Mar 13, 2018

@AkA84
v0.5.11 is the present for you!

@AkA84
Copy link

AkA84 commented Mar 14, 2018

@dotneet amazing! thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants