Building an Interactive Prompt
Intro
Working with Selenium straight in code has its advantages. However, if you run into a complicated scenario to automate, it becomes challenging to work through it to completion.
The typical approach is to walk through the scenario manually and inspect its markup, identify the parts you want to interact with, write up the test script, run it, and watch it. If it passes, then pat yourself on the back. If it fails, make some tweaks and run it again -- repeating until it passes.
This is a real pain, and it's even more frustrating when debugging tests that fail intermittently.
A Solution
One way to simplify things, and make it easier for testers to transition from Selenium IDE, is to build an interactive prompt -- also known as a read-eval-print-loop (REPL).
Upon loading the prompt it will launch a browser and accept Selenium commands just the same as they are written in test code. And if a command is issued that errors (or that it doesn't recognize) the prompt will provide that information and continue to function.
This will enable us to continue through specific Selenium actions and locators to verify that they work before putting them into test code.
Example
The prompt is a simple loop. It prints >> , reads a command, and evaluates it with the driver available as
driver (or Driver in C#). If the command returns something, the prompt prints it. If the command errors, the
prompt prints the error and keeps going. When we type q, it says goodbye and ends, and the browser is closed.
To make sure the prompt keeps working, the input and output are passed in to the loop. That way the same code reads from the keyboard when we use it, and from a list of commands in a test that runs in CI.
Expected Behavior
- Firefox loads
- An interactive prompt is presented
- Issue a Selenium command and see it execute in the browser
- Issue a command that it does not recognize (or that errors), and see a message stating the issue in the prompt
- Issue q, the browser will close and the prompt will terminate
Summary
Hopefully this REPL will help save you some time and frustration -- making it easier to write and debug your tests.
Happy Testing!
Special Thanks

Authors

