Web testing is a common style of acceptance testing, because the customer wants to know if the system is acceptable, and this is a perfect way to demonstrate it.
In previous recipes, we have explored writing tests against non-web applications. In this recipe, let's see how to use a third-party Robot Framework plugin to use Selenium to test a shopping cart web application.
virtualenv
setup.satchmo
shopping cart web application. To start it, switch to the store
directory and type python
manage.py
runserver
. You can explore it by visiting http://localhost:8000
.With the following steps, we will see how to get going with using some of the basic Robot commands for driving a web application.
recipe42.txt
, with an opening description of the story.As a store customer I want to put things into my cart So that I can verify the store's functionality.
***Test Cases*** Inspect empty cart in detail Click link Cart Page Should Contain Your cart is empty Page Should Contain 0 - $0.00 Capture Page Screenshot recipe42-scenario1-1.png
Load up a cart with 2 of the same Click link Science Fiction don't wait Capture Page Screenshot recipe42-scenario2-1.png Click link Robots Attack! Capture Page Screenshot recipe42-scenario2-2.png Input text quantity 2 Capture Page Screenshot recipe42-scenario2-3.png Click button Add to cart Click link Cart Capture Page Screenshot recipe42-scenario2-4.png Textfield Value Should Be quantity 2 Page Should Contain Robots Attack! (Hard cover) Html Should Contain <td align="center">$7.99</td> Html Should Contain <td align="center">$15.98</td> Html Should Contain <td>$15.98</td>
***Keywords*** Html Should Contain [Arguments] ${expected} ${html}= Get Source Should Contain ${html} ${expected} Startup Start Selenium Server Sleep 3s
Get
Source
is a Selenium Library keyword that fetches the raw HTML of the entire page. Start
Selenium
Server
is another keyword to launch the selenium server. A built-in Sleep
call is included to avoid startup/shutdown timing issues, if this test happens before or after another selenium-based test suite.
***Settings*** Library SeleniumLibrary Test Setup Open Browser http://localhost:8000 Test Teardown Close All Browsers Suite Setup Startup Suite Teardown Stop Selenium Server
Test
Setup
is a built-in keyword that defines steps executed before each test case. In this case, it uses the Selenium Library keyword Open
Browser
to launch a browser pointed at the satchmo
application. Test
Teardown
is a built-in keyword that executes at the end of each test and closes the browsers launched by this test.
Suite
Setup
is a built-in keyword that is only run before any tests are executed, and Suite
Teardown
is only run after all the tests in this suite. In this case, we use it to start and stop the Selenium library.
pybot recipe42.txt
.log.html
, and observe the details including the captured screenshots in each scenario. The following screenshot is just one of the many captured screenshots. Feel free to inspect the rest of the screenshots as well as the logs.Robot Framework provides a powerful environment to define tests through keywords. The Selenium plugin interfaces with selenium and provides a whole set of keywords that are focused on manipulating web applications and reading and confirming their outputs.
An important part of web application testing is getting hold of an element to manipulate it or test values. The most common way of doing this is by checking key attributes of the element like id
, name
, or href
. For example, in our scenario, there is a button we need to click to add the book to the cart. It can be identified by either the ID addcart
or the displayed text Add
to
cart
.
While Robot Framework is free compared to other commercial front end test solutions, it is important to realize that the effort in writing automated tests isn't free and effortless. It takes effort to make this an active part of front end design.
Incorporating tools like Robot and SeleniumLibrary early in the process of screen design will encourage good practices like tagging frames and elements, so that they'll be testable early on. This is no different than attempting to write automated tests for a backend server system after it's already built. Both situations are much more costly if they are introduced later. Making automated testing a part of backend systems early on encourages similar coding to support testability.
In case we are looking at embracing acceptance testing late in our development cycle, or perhaps trying to test a system we inherited from another team, we need to include time to make changes to the web interface in order to add tags and identifiers to support writing the tests.
While the satchmo shopping cart application didn't have any significant delays in the tests we wrote, it doesn't mean other applications won't. If your web application has certain parts that are noticeably slower, it is valuable to read the online documentation (http://robotframework-seleniumlibrary.googlecode.com/hg/doc/SeleniumLibrary.html?r=2.5) about configuring how long Selenium should wait for a response from your application.
18.118.24.106