Testing web basics with Robot

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.

Getting ready

  1. We first need to activate our virtualenv setup.
  2. For this recipe, we are using the 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.
  3. Next, install the Robot Framework and the third-party Selenium plugin, as shown in the recipe Installing the Robot Framework.

How to do it...

With the following steps, we will see how to get going with using some of the basic Robot commands for driving a web application.

  1. Create a plain text story file called 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.
  2. Create a section for test cases, and add a scenario that verifies there is an empty shopping cart and captures a screenshot.
    ***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
  3. Add another scenario that picks a book, adds two copies of the cart, and confirms the total cart value.
    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>
  4. Add a section of keywords and define a keyword for inspecting the raw HTML of the page.
    ***Keywords***
    Html Should Contain
      [Arguments]     ${expected}
      ${html}=        Get Source
      Should Contain  ${html}  ${expected}
    
    Startup
      Start Selenium Server
      Sleep  3s

    Note

    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.

  5. Add a section that imports the Selenium Library, and also defines a setup and teardown process for launching and shutting down the browser for each test case.
    ***Settings***
    Library         SeleniumLibrary
    Test Setup      Open Browser  http://localhost:8000
    Test Teardown   Close All Browsers
    
    Suite Setup     Startup
    Suite Teardown  Stop Selenium Server

    Note

    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.

  6. Run the test suite by typing pybot recipe42.txt.
    How to do it...
  7. Open 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.
    How to do it...

How it works...

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.

There's more...

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.

Learn about timing configurations—they may be important!

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.

See also

  • Installing the Robot Framework
  • Creating a data-driven test suite with Robot
  • Writing a testable story using Robot
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.106