For a couple weeks, I have been using cucumber.js and selenium webdriver for acceptance testing.
For the most part, I’ve been very happy with tools. However, the pervasent use of promises in selenium webdriver annoys me.
For example, consider the following script that opens up a login page, and tests that you can successfully login.
I personally don’t like the 2 levels of indirection from using promises.
Further, the number of promises will scale with each page you visit.
This leads to very painful reading.
Just recently, I found a way to increase the understandability of selenium webdriver based scripts using es6 generators.
Since node.js does not support es6 yet, you will have to use the babel-node executable to make it work. You can installing babel-node with the following script from the command line:
The following code does the exact same thing as the previous script, but instead uses es6.
The code uses lazy evaluation to stop and continue execution of the script. If you are familiar with functional languages like scala, clojure, or haskell then this type of approach should be second nature to you.
I will give you a high level overview of what is happening:
Step 1: We define a generator function called main.
However, after it stops execution, it will call the function visitLoginPage.
Step 4: visitLoginPage will use the variable m to get a reference to the main function, and either tell it to continue or to throw and exception.
Step 5: The main function will continue executing until it reaches the second yield statement.
Step 6: getUrl will continue execution and return a value that we store in currentUrl in the main function.
Now we can print the url to the console.
I’m sure that there are ways alternative ways to simplify selenium webdriver scripts.
I’m not saying that this is the best way. It is just something that I personally like, and I hope that it helps you.