TA Basics: Adjust scenario's and steps

You now have this beautifull generated test automation suite and you want to adjust it to your needs, but have no clue where to start. Don't worry, because that is exactly what we're going to cover in the next couple of posts. In this post we'll add a new scenario, make some small adjustments to a step, add a new step and run only that one test to check the results.

Cucumber

Let's start with a test run with Cucumber. Navigate to the folder in which the tests are stored. So in this case in `C:\TA\testproject`. In this folder type `cucumber` and hit the enter key to run all tests from your generated test suite. But you can also run a limited set of tests. To do so, you should make use of tags in your scenario's. To run only one test you can run it like this `cucumber -t @whatever`. It will now only run tests that are matching this specific tag and ignore all others. 

Add a scenario and run it

Let's start with adding a new scenario. For example in the file `1_basic.feature` we add a scenario:
@my_tag Scenario: example_05 - My first added scenario Given the user navigates to "https://mark0203.blogspot.com" When the user searches for "bla" Then the text "No results found" should display
Save the changes run cucumber like mentioned in the cucumber section.
Now what do we see? In blue you see the tag names. In green you see the precondition step of the scenario in green (this one passed). In red we see the interactio step of the scenario that failed. In orange we see the validation step of the scenario that does not exist yet. I particularly  like the part at the bottom, a snippet of the missing validation step. You can just copy paste this into your feature file so there is no need for writing it manually.

Element inspector 

For website test automation you should make yourself familiar with the development tools of the browser and especially the element inspector. Some online documentation about the dev tools and inspector can be found for Chrome and Firefox. In our examples we use Chrome and just rightclick on the search icon and select `Inspect`. In the next screenshot you see that I've inspected the search icon (highlighted in blue). But in the test below I'll click on the div with a class named search (see red arrow).
There are many ways to interact with elements (see the links to guides in the next paragraph). Finding elements by `id` or `class` are the most used ones. An `id` should only appear one time on the website, so that is the most unique one. The `class` is usually also unique enough to find the element, but I'll show some more complex examples later in a different blogpost. I always try to find part of the element that is the easiest and unique enough to find in the HTML, in this case I take the div instead of the icon. Just keep in mind that not all elements might be clickable. I've seen this with styled checkboxes, which I'll cover in a lessons learned post. 

Fix the failing step 

The step `When the user searches for "bla"` failed because it cannot find the search element. Which makes sense since this step is generated with the lapis_lazuli gem/tools and is trying to find the search element from the lapis_lazuli test page. So we need to adjust this first to work for our scenario. The error from the run after we added the scenario mentions that the problem should be in ./features/step_definitions/basic_steps.rb:19 (line 19). Change this line into
browser.div(:class => "search").click #this opens the search searchbox = browser.find(:text_field => {:name => "q"}) #changed "s" into "q"
I added one extra line that will click on the search icon first since my blog is slightly different and need to open the search bar first. And the input field name is different so I had to adjust that too. You can use the elements guide from watir or the elements wiki from lapis_lazuli to interact with the browser elements. Save the changes and run cucumber again to see if this fixed anything.
 As you can see the failing step now passes. Just keep in mind that the changes we just made to the step for searching will screw up the generated tests related to search. But that does not matter since this is just an example how easy it is to alter the generated framework and change it to your needs.

Add the undefined test and make it work

So now we copy paste the unfined step from the command prompt to `.\features\step_definitions\basic_steps.rb`. Add a new line in which we're matching the string we searched for with what is shown on the blog seach
Then("the text {string} should display") do |string| # Find the text of a div element with the id "Blog1" # Check if the text is equal to the search word from the scenario browser.div(:id => "Blog1").text == string end
Save this change and run cucumber again to see if this fixed anything.
And voila! You just created a new scenario, adjusted/fixed a step, added a missing step and made the test run pass. So you're now ready to create some new scenario's, make a small POC for your project so you know you can use this suite for your project. Have fun!
And if you have any questions, feel free to ask.

Comments

Popular posts from this blog

PowerShell - How to overcome Azure VM's fixed resolution limitation

TA Basics: Website Test Automation on mobile devices via Appium server

TA: Who doesn't like proxies? Me!