BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications

68 views

Published on

Talk about Behavior-driven Development, Behave, Selenium and Python

Project is found at https://github.com/pviafore/BddToTheBone

Presented at PyTennessee 2017

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
68
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications

  1. 1. BDD To The Bone Using Behave and Selenium to Test-Drive Web Applications
  2. 2. URL Shorteners?
  3. 3. https://docs.google.com/presentation/d/1iDdRQQSx7Q GzVaOWI61YizvqFQMIJ5VhQEN6uR13iHs/ https://goo.gl/MyruRa
  4. 4. Requirements #1 - #3 When a user enters in a URL, they are provided a shortened URL. When a user navigates to a shortened URL, they are redirected to the original URL. This should be as fast as possible. There is a way to see how many people have been redirected through this URL.
  5. 5. Requirement 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Requirement 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~ . . . . . . Requirement 571 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. 6. How do you want to do this?
  7. 7. That’s great!
  8. 8. But...
  9. 9. Requirements #1 - #3 When a user enters in a URL, they are provided a shortened URL. When a user navigates to a shortened URL, they are redirected to the original URL. This should be as fast as possible. There is a way to see how many people have been redirected through this URL.
  10. 10. Tests are meant to answer a question (they can’t prove there are no bugs)
  11. 11. Do I have confidence that I can ship my code? Does my code do what I want it to do? How does my code work with 10,000 users? Can my code run for weeks on end? Does my code do what the customer wants?
  12. 12. Do I have confidence that I can ship my code? Does my code do what I want it to do? How does my code work with 10,000 users? Can my code run for weeks on end? Does my code do what the customer wants?
  13. 13. Do I have confidence that I can ship my code? Does my code do what I want it to do? How does my code work with 10,000 users? Can my code run for weeks on end? Does my code do what the customer wants?
  14. 14. Acceptance Testing
  15. 15. Problems?
  16. 16. I still have to figure out exactly what the customer wants
  17. 17. The customer is going to change their mind
  18. 18. I have to trace back to requirements
  19. 19. Executable Specifications
  20. 20. I want to have tests written as close to plain English requirements as possible
  21. 21. I want to run these tests often to make sure I always am giving the customer what they want
  22. 22. Gherkin
  23. 23. Gherkin Example Feature: Our service makes short URLs out of long URLs All URLs will start with http://patl.ly:8080/ and end in a number representing the lookup index Scenario: Shortening a URL Given a url http://www.python.org When we shorten it through our service Then we should receive a shortened URL
  24. 24. Gherkin Example Feature: Our service makes short URLs out of long URLs All URLs will start with http://patl.ly:8080/ and end in a number representing the lookup index Scenario: Shortening a URL Given a url http://www.python.org When we shorten it through our service Then we should receive a shortened URL
  25. 25. Gherkin Example Feature: Our service makes short URLs out of long URLs All URLs will start with http://patl.ly:8080/ and end in a number representing the lookup index Scenario: Shortening a URL Given a url http://www.python.org When we shorten it through our service Then we should receive a shortened URL
  26. 26. Gherkin Example Feature: Our service makes short URLs out of long URLs All URLs will start with http://patl.ly:8080/ and end in a number representing the lookup index Scenario: Shortening a URL Given a url http://www.python.org When we shorten it through our service Then we should receive a shortened URL
  27. 27. Gherkin Example Feature: Our service makes short URLs out of long URLs All URLs will start with http://patl.ly:8080/ and end in a number representing the lookup index Scenario: Shortening a URL Given a url http://www.python.org When we shorten it through our service Then we should receive a shortened URL
  28. 28. Gherkin Example Feature: Our service makes short URLs out of long URLs All URLs will start with http://patl.ly:8080/ and end in a number representing the lookup index Scenario: Shortening a URL Given a url http://www.python.org When we shorten it through our service Then we should receive a shortened URL
  29. 29. Let’s look at some features
  30. 30. So far, this is just looking like a test case
  31. 31. Behave
  32. 32. Behave is a Python library used to hook up Python code to Gherkin statements
  33. 33. Gherkin Features become executable specifications
  34. 34. Drive the requirements conversation up front
  35. 35. Let’s write some steps
  36. 36. So how do we implement these steps
  37. 37. HTTP
  38. 38. urllib
  39. 39. requests
  40. 40. These are HTTP libraries, not JS libraries
  41. 41. Even if they did do Javascript, do they do it the same way as a browser?
  42. 42. How do you test clicks, and mouse hovers, and all other front-end things?
  43. 43. It’s your responsibility to deliver a solution, not just a piece of code
  44. 44. selenium
  45. 45. Selenium lets you control web browsers through Python
  46. 46. html head body …… input id=”input” span id=”return-link” table id=”stats” tr tr tr tr tr tr tr td td http://pat.ly:8080/1 3
  47. 47. html head body …… input id=”input” span id=”return-link” table id=”stats” tr tr tr tr tr tr tr td td http://pat.ly:8080/1 3 browser.find_element_by_id(“input”)
  48. 48. html head body …… input id=”input” span id=”return-link” table id=”stats” tr tr tr tr tr tr tr td td http://pat.ly:8080/1 3 table = browser.find_element_by_id(“stats”)
  49. 49. html head body …… input id=”input” span id=”return-link” table id=”stats” tr tr tr tr tr tr tr td td http://pat.ly:8080/1 3 table = browser.find_element_by_id(“stats”) rows = table.find_elements_by_tag_name(“tr”) row = rows[3]
  50. 50. html head body …… input id=”input” span id=”return-link” table id=”stats” tr tr tr tr tr tr tr td td http://pat.ly:8080/1 3 table = browser.find_element_by_id(“stats”) rows = table.find_elements_by_tag_name(“tr”) row = rows[3] cell = row.find_elements_by_tag_name(“td”)[0]
  51. 51. html head body …… input id=”input” span id=”return-link” table id=”stats” tr tr tr tr tr tr tr td td http://pat.ly:8080/1 3 table = browser.find_element_by_id(“stats”) rows = table.find_elements_by_tag_name(“tr”) row = rows[3] cell = row.find_elements_by_tag_name(“td”)[0] print cell.text # prints out “http://pat.ly:8080/1”
  52. 52. Example time!
  53. 53. Find elements by: Name ID Class Name Tag Name Link Text Partial Link Text
  54. 54. Interact with the page: Clicking Sending input Modify cookies Move the mouse Screenshotting Executing Arbitrary Javascript
  55. 55. Let’s look at the tests again
  56. 56. Other Behave features Scenario outlines Customizable environment file Regex matching
  57. 57. Selenium (rather the web browser) is slow
  58. 58. Acceptance Tests are unwieldy if you have a lot
  59. 59. If nobody reads them, you lose some value
  60. 60. The UI is typically one of the more fragile pieces
  61. 61. But…
  62. 62. There is still value to be had
  63. 63. Driving the conversations with customers is critical
  64. 64. So….
  65. 65. Don’t let your requirements go out of date
  66. 66. Keep talking with your customers
  67. 67. Build your safety net of acceptance tests
  68. 68. BDD + Gherkin + Behave + Selenium + Python =.......
  69. 69. Giving your customers what they really want
  70. 70. Project at https://github.com/pviafore/BddToTheBone Follow me: @PatViaforever

×