20 February 2013

Cucumber Conditionals

I had an interesting experience with a GXT environment.  Since GXT generates the HTML code, I had some constraints to work around.

In this particular case, I had drop downs that were locked down to these specifications:
  - drop downs were not treated with the "select" tag
  - drop downs were read only

this meant, if I had a language drop down with values: English, Spanish, French

I had to find a way to select any of them, via a test.  Since the code in this case couldn't be modified to switch the drop downs to "selectors" I was stuck. Watir Webdriver expects drop downs to be accessed with the .select method.  In this case I had to use the .text_field method.

Since the fields were read only, I couldn't pass in values to select them.

My Solution
I ended up writing a conditional test in Cucumber itself.

The test would be a scenario outline defining the values in the drop down for the test, like this:

Scenario Outline: Testing each value in the language drop down
  Given a user at .....
  When they choose a specific language of <language>
  And Save .....
  Then the <language> will be set
  Examples:
  |language|
  |English|
  |Spanish|
  |French|

In the step definition, I wrote a conditional at the When and Thens.... the When would be like:
if arg == "English
   @browser.text_field(:id=>"the id on this field").send_keys :arrow_down, :return

elsif arg == "Spanish"
   @browser.text_field(:id=>"the id on this field").send_keys 2.times{:arrow_down)
   @browser.send_keys :return

and so forth.

For many choices I would use a case/when switch instead of a series of if's, but you get the idea.

Is it the best idea? No.

But it was optimal, as some would say. It got a solution out quickly, when I had a constraint that forced me into a specific path.

No comments:

Post a Comment