Write your first test: find artist’s video on youtube

Tutorial description

In this tutorial we will create simple UI-test on XML2Selenium. This test would contain following steps:

  • Go to youtube.com webpage
  • Enter text to video search form
  • Click to search button
  • Verify that result page contains specified text

Also we'll periodically save screenshots and snapshots (html content of the page) and record test video.

1. Creating test-case

To start we need to make a test-case and test. For now test is empty, but we'll fill it in future steps. Test case and test have name, description and tags attributes. This attributes used by reporting tools to make user-understandable reports. You can watch report examples here.

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <testcase xmlns="http://www.jazzteam.org/Routines" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. name="video on youtube" description="Find video on youtube" tags="Basic, youtube">
  4. <test coverage="100" name="01 YouTube" description="Find the video and check that the page is present 'Like'"
  5. tags="youtube, navigate, webelement, field, button" >
  6. <!-- Your test steps goes here -->
  7. </test>
  8. </testcase>

2. Navigate youtube.com and enter text to search field

For all UI test first step is usually navigating to web-page. For this task <navigate to="http://www.youtube.com/"/> command with specified url used. Also we start to record video by adding <video action="start"/> command. After test finished we'll have a recorded video. In case of test fail this video will help programmers to fix bugs.

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <testcase xmlns="http://www.jazzteam.org/Routines" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. name="video on youtube" description="Find video on youtube" tags="Basic, youtube">
  4. <test coverage="100" name="01 YouTube" description="Find the video and check that the page is present 'Like'"
  5. tags="youtube, navigate, webelement, field, button" >
  6. <!-- start video recording -->
  7. <video action="start"/>
  8. <!-- go to youtube.com -->
  9. <navigate to="http://www.youtube.com/"/>
  10. </test>
  11. </testcase>

3. Enter text and press "search" button

Then we need to enter text in search field. We use <field id='masthead-search-term' textToType="humor"/> for this. This command enters text 'humor' to field with id='masthead-search-term'. Clicking is done using <button id="search-btn"/> command where id attribute points to button id. Next we need to wait while page with search result loads using <pause time="3000"/>. This command does nothing, but waits for 3000 miliseconds. Then we save screenshot and snapshot using <screenshot/> and <snapshot/> commands.

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <testcase xmlns="http://www.jazzteam.org/Routines" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. name="video on youtube" description="Find video on youtube" tags="Basic, youtube">
  4. <test coverage="100" name="01 YouTube" description="Find the video and check that the page is present 'Like'"
  5. tags="youtube, navigate, webelement, field, button" >
  6. <!-- start video recording -->
  7. <video action="start"/>
  8. <!-- go to youtube.com -->
  9. <navigate to="http://www.youtube.com/"/>
  10.  
  11. <!-- write text "humor" in text field -->
  12. <field id='masthead-search-term' textToType="humor"/>
  13. <!-- press the button to search -->
  14. <button id="search-btn"/>
  15. <!-- put a pause to load the contents of the page -->
  16. <pause time="3000"/>
  17.  
  18. <!-- create an image of the page -->
  19. <screenshot/>
  20. <!-- create a copy of the page -->
  21. <snapshot/>
  22. </test>
  23. </testcase>

 

4. Open video clip and verify that 'Like' button appears

Now we are on a youtube page with search results. Next we click to first video using <webelement xpath="//*[@id='search-results']/li[@data-context-item-type='video'][1]" action="click"/>. Do not afraid of this strange code (//*[@id='search-results']/li[@data-context-item-type='video'][1]), this is additional possibility to find webpage elements using Xpath, standard language for web-development. For now just note that this Xpath expression points to first video on youtube search results page. You can read about Xpath here. It is not necessary to use Xpath, in XML2Selenium you can point to elements by their IDs, css class and tag name. After we select video, we wait for 3 seconds and check 'Like' button appears using <webelement xpath="//button[@id='watch-like']//span[@class='yt-uix-button-content']" action="isContainsText" text="Like"/>. This command checks that pointed element contains 'Like' text. After checking we save screenshot and snapshot.

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <testcase xmlns="http://www.jazzteam.org/Routines" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. name="video on youtube" description="Find video on youtube" tags="Basic, youtube">
  4. <test coverage="100" name="01 YouTube" description="Find the video and check that the page is present 'Like'"
  5. tags="youtube, navigate, webelement, field, button" >
  6. <!-- start video recording -->
  7. <video action="start"/>
  8. <!-- go to youtube.com -->
  9. <navigate to="http://www.youtube.com/"/>
  10.  
  11. <!-- write text "humor" in text field -->
  12. <field id='masthead-search-term' textToType="humor"/>
  13. <!-- press the button to search -->
  14. <button id="search-btn"/>
  15. <!-- put a pause to load the contents of the page -->
  16. <pause time="3000"/>
  17. <!-- stop video recording -->
  18. <video action="stop"/>
  19.  
  20. <!-- create an image of the page -->
  21. <screenshot/>
  22. <!-- create a copy of the page -->
  23. <snapshot/>
  24. <!-- Click on the first video clip from the list -->
  25. <webelement xpath="//*[@id='search-results']/li[@data-context-item-type='video'][1]"
  26. action="click"/>
  27. <!-- put a pause to load the contents of the page -->
  28. <pause time="3000"/>
  29. <!-- check that the page is present like -->
  30. <webelement xpath="//button[@id='watch-like']//span[@class='yt-uix-button-content']"
  31. action="isContainsText" text="Like"/>
  32. <screenshot/>
  33. <snapshot/>
  34. </test>
  35. </testcase>

 

Conclusion

Congratulations, we completed first test on XML2Selenium! After executing this test we'll get following aftifacts:




You are ready for reading more advanced tutorial with twitter testing example.

Leave a Comment