Test automation tutorial with Selenium WebDriver part 3: Test case creation
Test case creation
4 min readMar 28, 2016
Suppose that you need to automate simple test case:
- Go to http://www.openstreetmap.org
- Click Direction icon
- Type ‘Berlin’ as starting point
- Type ‘Munich’ as end point
- Click ‘Go’ button
- 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:
- Generate page object for each logical part of the web page
- Create methods for page objects
- Create a test using page objects’ methods and fields
NOTE: Python class represents page object
Let’s start with class generation:
- Open easy_selenium_ui.py — run in cmd.exe: easy_selenium_ui.py
- Set root folder — folder where all data will be stored
- Open http://www.openstreetmap.org with Easy Selenium UI
- Generate class for this page
- Click ‘Reload image’ if screenshot is not loaded
- Now select area that should be generated — we only need left panel
- Enter class name — OSM
- Click ‘Generate’ to generate Python class
- Wait until Python class will be generated
- Now first page object is generated you can check it in ‘Editor’ tab by opening generated class
- OSM class
- Now go to opened browser and manually do:
- Click Direction icon
- Type ‘Berlin’ as starting point
- Type ‘Munich’ as end point
- Click ‘Go’ button
- 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:
- Select ‘Editor’ tab
- Open ‘OSM’ class
- Click ‘Create test file’
- Use ‘osm_test.py’ as filename
- Click ‘Save’
- Now test file should be create and opened:
- Click ‘Create new method/test case’ use ‘test_create_german_route’ as test case name
- Now you should see a dummy test
- Use CTRL + S to save test file
- Now you can use right click menu on image to generate Python code and insert in into test
- Generate ‘click’ action for Direction icon:
- Hover over Direction icon
- Right click on it
- Select ‘click’ from right click menu
- Now test case will be updated and Python generated code of click method will be inserted
- Now select OSMSearch class
- Generate ‘type’ action for staring point:
- Hover over staring point
- Right click on it
- Select ‘type’ from right click menu
- In opened dialog type ‘Berlin’ (with quotes because it is a string)
- Click ‘OK’
- Generate ‘type’ action for end point:
- Hover over staring point
- Right click on it
- Select ‘type’ from right click menu
- In opened dialog type ‘Munich’ (with quotes because it is a string)
- Click ‘OK’
- Generate ‘click’ action for ‘Go’ button(Use steps in Step 8 for help)
- Now lets generate code for verification:
- Generate ‘get_text’ action for route summary:
- Hover over route summary
- Right click on it
- Select ‘getter’
- Select ‘get_text’
- 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
- Generate verification:
- Hover anywhere on image
- Right click on it
- Select ‘Asserts’
- Select ‘assert_in’
- Type ‘584km’ (with quotes because it is a string) for ‘member’
- Type ‘text’ (withOUT quotes because it is a variable) for ‘container’
- You should get full test case:
- OSMTest class with test_create_german_route
Now we can execute created test case:
- Open ‘Test runner’ tab
- Select test file using ‘Load tests from file’ button
- Select ‘test_create_german_route’ from left panel
- Click ‘Run test cases’
- Test runner
Try to:
- Fail created test case by changing ‘584km’ to ‘582km’
- Run with different browser
- Run with additional options
- Create similar test case but with different starting and end points
Next: Continuous integration