2

So I'm desperately looking for a way to delay some executions in WebDriver but I don't seem to find one.

The web app which I try to run black box test against, works with ajax calls but these ajax calls do not render anything on DOM, thus I can't use explicit wait. Also, the implicit only works for find_element statements and again won't be useful.

I had success using time.sleep() but I hope there is a nicer way of delaying the execution.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • What do these AJAX calls return? If you open the browser manually, how would you identify that the load is completed? Thanks. – alecxe Jul 23 '15 at 05:04
  • @alecxe, sometime they set cookies and sometime they just store variable on the `ng-controller`. – Sam R. Jul 23 '15 at 05:06
  • Thanks, and this is only during the page load, correct? – alecxe Jul 23 '15 at 05:07
  • @alecxe, not necessarily. Think of a form that when is submitted, send an ajax call. Before response is received if you click `next`, you're doomed but after that, it'll take you where it should. And no, the button is not disabled or enabled during this. – Sam R. Jul 23 '15 at 05:10

2 Answers2

2

From what I understand (it's 1am here, I may miss something), you need your tests to be synchronized with AngularJS, waiting for outstanding requests and angular to "settle down".

This is what, in Javascript world, protractor solves perfectly - it always knows when Angular is ready and it makes the tests much more natural, you don't even think about synchronization issues - it works smoothly and out of the box:

You no longer need to add waits and sleeps to your test. Protractor can automatically execute the next step in your test the moment the webpage finishes pending tasks, so you don’t have to worry about waiting for your test and webpage to sync.

As for Python, there is pytractor project that sounds like something you should evaluate:

pytractor is an extension to the Selenium bindings for Python. Its goal is to make testing of angular.js applications easier with Python.

It is built on some parts of protractor, the "official" Javascript E2E/Scenario testing framework for Angular.js.

As a red flag, note that the project is not actively maintained. At least, you may study the source and use the ideas introduced in the code.

Note that internally protractor and pytractor inject client-side scripts which are asynchronously executed. In order to wait for Angular to be "ready", they both use angular.getTestability(el).whenStable() (source).


See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • This sounds cool, but how does it do that? Does it listen to all ajax calls on the browser? – Sam R. Jul 23 '15 at 05:15
  • @norbertpy protractor (and pytractor) injects client side scripts. Before any action executes an async script and use `angular.getTestability(el).whenStable()` internally ([source](https://github.com/angular/protractor/blob/master/lib/clientsidescripts.js#L48)). – alecxe Jul 23 '15 at 05:17
  • @norbertpy you can actually use `driver.execute_async_script()` and make a custom wait based on this idea. – alecxe Jul 23 '15 at 05:18
  • Awesome, I'll give it a shot. Thanks. – Sam R. Jul 23 '15 at 05:19
  • @norbertpy added some more links. Let me know if you'll need help with the async script and a wait (I'll be afk for a rather long time but I hope I'll come back here). Good luck in solving it. Thanks. – alecxe Jul 23 '15 at 05:20
  • Appreciate it. I'll dig it in. Thank you and have a good night. – Sam R. Jul 23 '15 at 05:22
  • `Pytractor` was abandoned. I recommend using [python selenium](http://selenium-python.readthedocs.io/) directly. To wait for angular I used [this solution based on protractor code](https://stackoverflow.com/a/38657401/1092815) – GabLeRoux Nov 10 '17 at 21:06
0

This should work:

var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();

driver.sleep(1000);
MCMXCII
  • 1,043
  • 4
  • 13
  • 26
Shishir M
  • 29
  • 1
  • 3