Previously..
We have now created our test, refactored it into a test that uses a user-defined keyword so we can alter the input of the test for better coverage. What if we wanted to perform multiple searches with the same validation? We could copy and paste the test multiple times... or we could use a more data driven approach.
Our Goal
Take the current test we have, create 5 individual searches each with a screenshot.
Let's begin.
In order to start writing our dataload, we first should create a brand new suite for this (create a new file in the directory where your tests are, name it dataload.txt)
The reason we need to do this is because a data driven test requires a template, suite setup and a suite teardown.
The template is the keyword that will be repeated per test, the suite set up is the keyword used when the test starts, and the teardown is the final keyword run when all tests have completed.
The reason we need to do this is because a data driven test requires a template, suite setup and a suite teardown.
The template is the keyword that will be repeated per test, the suite set up is the keyword used when the test starts, and the teardown is the final keyword run when all tests have completed.
SEtup the dataload file
Now that we have our new file, we can begin writing the setups.
*** Settings ***
Test Template
Suite Setup
Suite Teardown
Library
Resource
Test Template
Suite Setup
Suite Teardown
Library
Resource
The new things you should see here are "Test Template", "suite setup" and "suite teardown".
The template is your keyword that will execute on every iteration
The setup is the keyword run only once at the start of your test.
The tear down is the keyword that runs when all iterations have completed.
The template is your keyword that will execute on every iteration
The setup is the keyword run only once at the start of your test.
The tear down is the keyword that runs when all iterations have completed.
Understanding the problem
Currently we have a keyword we need to data drive but in it's current form it cannot be done - this is a very common problem that occurs when enhancing a test so I will break it down as much as possible. The test we need to datadrive looks like this:
*** Test Cases ***
Performing a basic google search
Open browser http://www.google.com browser=chrome
Perform Search "thing we want to search"
Page Should Contain id=resultStats
Performing a basic google search
Open browser http://www.google.com browser=chrome
Perform Search "thing we want to search"
Page Should Contain id=resultStats
*** Keywords ***
Perform Search
[Arguments] ${search_text}
Input Text ${search_text} ${search_field}
Click Button ${search_button}
Perform Search
[Arguments] ${search_text}
Input Text ${search_text} ${search_field}
Click Button ${search_button}
Baby Steps.
We need to identify what needs to be run on every iteration and extract that for the template of the data load. This is the "perform search" key word we have created.
We then need to make any enhancements to that keyword - in this case, make a screenshot. (sometimes you might not want this keyword to always generate a screenshot, there are 2 ways around this - enhancing the current keyword to manage a yes/no for screenshots, or create a new keyword entirely.)
Finally we can then set up what our test needs to do to start and stop itself.
We then need to make any enhancements to that keyword - in this case, make a screenshot. (sometimes you might not want this keyword to always generate a screenshot, there are 2 ways around this - enhancing the current keyword to manage a yes/no for screenshots, or create a new keyword entirely.)
Finally we can then set up what our test needs to do to start and stop itself.
Comfortable? Now Do it.
Make a copy of the "Perform Search" keyword and call it "Perform Search Dataload". We need to do this because we will be editing it in such a way that you must perform multiple searches, and sometimes that is nto what you want to do in a functional test.
Thanks to RIDE, we already have a Take screenshot keyword we can use, it will do a nice full page screenshot in firefox however we need to give it a name. Another cool thing? we can use variables which already have our string, and easily append anything do it.
Capture Page Screenshot ${search_text}.png
This will name our screenshot with the search text we used, which should be good enough to identify each iteration. You can get clever and a prefix variable that counts up from 1 to keep track of your iterations.
Finally we need a way for our test to go back to the google page to make a new search. You could close the browser and reopen a new one - or just go to the google url.
Our keywords file should now look something like this:
*** Keywords ***
Perform Search Dataload
[Arguments] ${search_text}
Input Text ${search_text} ${search_field}
Click Button ${search_button}
Capture Page Screenshot ${search_text}.png
Go To http://www.google.com
Thanks to RIDE, we already have a Take screenshot keyword we can use, it will do a nice full page screenshot in firefox however we need to give it a name. Another cool thing? we can use variables which already have our string, and easily append anything do it.
Capture Page Screenshot ${search_text}.png
This will name our screenshot with the search text we used, which should be good enough to identify each iteration. You can get clever and a prefix variable that counts up from 1 to keep track of your iterations.
Finally we need a way for our test to go back to the google page to make a new search. You could close the browser and reopen a new one - or just go to the google url.
Our keywords file should now look something like this:
*** Keywords ***
Perform Search Dataload
[Arguments] ${search_text}
Input Text ${search_text} ${search_field}
Click Button ${search_button}
Capture Page Screenshot ${search_text}.png
Go To http://www.google.com
The Dataload file should look like this
*** Settings ***
Test Template Perform Search Dataload
Suite Setup Open Browser http://www.google.com browser=firefox
Suite Teardown Close Browser
Library Selenium2Library
Resource _Keywords.txt
***Test Cases ***
Search Dataload
<search1>
<search2>
etc etc
The test case in your dataload file should just be the arguments you are passing into "perform search dataload" which is your search text.
The reason why I like to seperate this test into a new file is so that I can use RIDE to run them independantly to my other tests.
*** Settings ***
Test Template Perform Search Dataload
Suite Setup Open Browser http://www.google.com browser=firefox
Suite Teardown Close Browser
Library Selenium2Library
Resource _Keywords.txt
***Test Cases ***
Search Dataload
<search1>
<search2>
etc etc
The test case in your dataload file should just be the arguments you are passing into "perform search dataload" which is your search text.
The reason why I like to seperate this test into a new file is so that I can use RIDE to run them independantly to my other tests.
END. But not really
If you have managed to reach the end of this tutorial flawlessly - well done! However your journey doesn't end here - there are many problems to face when writing tests for much much larger products, which may include having to write clever tests/having some sort of structure to your test suites. There are no solid answers to these common problems in automated testing, which is why a variety of methods must be explored.
Next up, I will be covering a BDD-style of automated testing with Behat
Next up, I will be covering a BDD-style of automated testing with Behat