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

83
BDD To The Bone Using Behave and Selenium to Test-Drive Web Applications

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

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

BDD To The BoneUsing Behave and Selenium to Test-Drive Web

Applications

Page 2: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 3: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 4: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 5: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 6: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications

URL Shorteners?

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

https://docs.google.com/presentation/d/1iDdRQQSx7QGzVaOWI61YizvqFQMIJ5VhQEN6uR13iHs/

https://goo.gl/MyruRa

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

Requirements #1 - #3When 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.

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

Requirement 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Requirement 5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ . . . . . .Requirement 571~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

How do you want to do this?

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

That’s great!

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

But...

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

Requirements #1 - #3When 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.

Page 16: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 17: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 18: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications

Tests are meant to answer a question (they can’t prove there are no bugs)

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

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?

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

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?UNIT

TESTS

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

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?

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

Acceptance Testing

Page 23: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 24: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
Page 25: BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications

Problems?

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

I still have to figure out exactly what the customer wants

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

The customer is going to change their mind

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

I have to trace back to requirements

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

Executable Specifications

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

I want to have tests written as close to plain English requirements as possible

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

I want to run these tests often to make sure I always am giving the customer what they want

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

Gherkin

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

Gherkin ExampleFeature: 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

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

Gherkin ExampleFeature: 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

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

Gherkin ExampleFeature: 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

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

Gherkin ExampleFeature: 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

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

Gherkin ExampleFeature: 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

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

Gherkin ExampleFeature: 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

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

Let’s look at some features

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

So far, this is just looking like a test case

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

Behave

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

Behave is a Python library used to hook up Python code to Gherkin statements

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

Gherkin Features become executable specifications

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

Drive the requirements conversation up front

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

Let’s write some steps

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

So how do we implement these steps

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

HTTP

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

urllib

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

requests

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

These are HTTP libraries, not JS libraries

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

Even if they did do Javascript, do they do it the same way as a browser?

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

How do you test clicks, and mouse hovers, and all other front-end things?

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

It’s your responsibility to deliver a solution, not just a piece of code

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

selenium

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

Selenium lets you control web browsers through Python

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

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

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

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”)

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

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”)

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

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]

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

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]

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

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”

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

Example time!

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

Find elements by:NameIDClass NameTag NameLink TextPartial Link TextCSS SelectorXPath

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

Interact with the page:ClickingSending inputModify cookiesMove the mouseScreenshottingExecuting Arbitrary Javascript

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

Let’s look at the tests again

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

Other Behave featuresScenario outlinesCustomizable environment fileRegex matching

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

Selenium (rather the web browser) is slow

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

Acceptance Tests are unwieldy if you have a lot

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

If nobody reads them, you lose some value

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

The UI is typically one of the more fragile pieces

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

But…

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

There is still value to be had

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

Driving the conversations with customers is critical

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

So….

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

Don’t let your requirements go out of date

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

Keep talking with your customers

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

Build your safety net of acceptance tests

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

BDD +Gherkin +Behave +Selenium + Python =.......

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

Giving your customers what they really want

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

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

Follow me:@PatViaforever