Skip to main content

Test automation tutorial with Selenium WebDriver part 3: Test case creation

Test case creation

Suppose that you need to automate simple test case:
  1. Go to http://www.openstreetmap.org
  2. Click Direction icon
  3. Type 'Berlin' as starting point
  4. Type 'Munich' as end point
  5. Click 'Go' button
  6. Verify that total distance is '584km'
Now we need to use this test case and write it in code using Easy Selenium UI.
Development flow is simple:
  1. Generate page object for each logical part of the web page
  2. Create methods for page objects
  3. Create a test using page objects' methods and fields
NOTE: Python class represents page object
Let's start with class generation:
  1. Open easy_selenium_ui.py - run in cmd.exe: easy_selenium_ui.py
  2. Set root folder - folder where all data will be stored
  3. Open http://www.openstreetmap.org with Easy Selenium UI
  4. Generate class for this page
    1. Click 'Reload image' if screenshot is not loaded
    2. Now select area that should be generated - we only need left panel
    3. Enter class name - OSM
    4. Click 'Generate' to generate Python class
    5. Wait until Python class will be generated
    6. Now first page object is generated you can check it in 'Editor' tab by opening generated class
      OSM class
  5. Now go to opened browser and manually do:
    1. Click Direction icon
    2. Type 'Berlin' as starting point
    3. Type 'Munich' as end point
    4. Click 'Go' button
  6. Now generate class for this page(check Step 4 above for help) select blue area with first suggestion and use 'OSMSearch' as class name:
    OSMSearch class
Now we have all needed classes.
Let's create a test:
  1. Select 'Editor' tab
  2. Open 'OSM' class
  3. Click 'Create test file'
    1. Use 'osm_test.py' as filename
    2. Click 'Save'
    3. Now test file should be create and opened:
  4. Click 'Create new method/test case' use 'test_create_german_route' as test case name
  5. Now you should see a dummy test
  6. Use CTRL + S to save test file
  7. Now you can use right click menu on image to generate Python code and insert in into test
  8. Generate 'click' action for Direction icon:
    1. Hover over Direction icon
    2. Right click on it
    3. Select 'click' from right click menu
    4. Now test case will be updated and Python generated code of click method will be inserted
  9. Now select OSMSearch class
  10. Generate 'type' action for staring point:
    1. Hover over staring point
    2. Right click on it
    3. Select 'type' from right click menu
    4. In opened dialog type 'Berlin' (with quotes because it is a string)
    5. Click 'OK'
  11. Generate 'type' action for end point:
    1. Hover over staring point
    2. Right click on it
    3. Select 'type' from right click menu
    4. In opened dialog type 'Munich' (with quotes because it is a string)
    5. Click 'OK'
  12. Generate 'click' action for 'Go' button(Use steps in Step 8 for help)
  13. Now lets generate code for verification:
    1. Generate 'get_text' action for route summary:
      1. Hover over route summary
      2. Right click on it
      3. Select 'getter'
      4. Select 'get_text'
      5. Now you should get line like this:
        • text = self.browser.get_text(...)
        • here new variable 'text' is introduced, this variable will be you in verification step
    2.  Generate verification:
      1. Hover anywhere on image
      2. Right click on it
      3. Select 'Asserts'
      4. Select 'assert_in'
      5. Type '584km' (with quotes because it is a string) for 'member'
      6. Type 'text' (withOUT quotes because it is a variable) for 'container'
  14. You should get full test case:
    OSMTest class with test_create_german_route
 Now we can execute created test case:
  1. Open 'Test runner' tab
  2. Select test file using 'Load tests from file' button
  3. Select 'test_create_german_route' from left panel
  4. Click 'Run test cases'
    Test runner

Try to:
  1. Fail created test case by changing '584km' to '582km'
  2. Run with different browser
  3. Run with additional options
  4. Create similar test case but with different starting and end points

 Next: Continuous integration




Comments

Post a Comment