Background Image
Table of Contents Table of Contents
Previous Page  417 / 478 Next Page
Information
Show Menu
Previous Page 417 / 478 Next Page
Page Background

Let’s do a commit at this point, because we’ve got at least a placeholder for our FT, we’ve

got a useful modification of the

create_pre_authenticated_session

function, and

we’re about to embark on a bit of an FT refactor:

$

git add functional_tests

$

git commit -m "New FT for sharing, move session creation stuff to base"

Implementing the Selenium Interact/Wait Pattern

Before we continue, let’s take a closer look at the interactions with the site which we

have in our FT so far:

functional_tests/test_sharing.py.

# Edith goes to the home page and starts a list

self

.

browser

=

edith_browser

self

.

browser

.

get

(

self

.

server_url

)

self

.

get_item_input_box

()

.

send_keys

(

'Get help

\n

'

)

#

# She notices a "Share this list" option

share_box

=

self

.

browser

.

find_element_by_css_selector

(

'input[name=email]'

)

#

self

.

assertEqual

(

share_box

.

get_attribute

(

'placeholder'

),

'your-friend@example.com

'

)

Interaction with site

Assumption about updated state of page

We learned in the last chapter that it’s dangerous to assume too much about the state of

the browser after we do an interaction (like

send_keys

). In theory,

implicitly_wait

will make sure that, if the call to

find_element_by_css_selector

doesn’t find our

input[name=email]

at first, it will silently retry a few times. But it can also go wrong—

imagine if there was an input on the previous page, with the same

name=email

, but a

different placeholder text? We’d get a strange failure, because Selenium could theoreti‐

cally pick up the element from the previous page while the new page is loading. That

tends to raise a

StaleElementException

.

Unexpected

StaleElementException

errors from Selenium often

mean you have some kind of race condition. You should probably

specify an explicit interaction/wait pattern.

Instead, it’s always prudent to follow the “wait-for” pattern whenever we want to check

on the effects of an interaction that we’ve just triggered. Something like this:

functional_tests/test_sharing.py.

self

.

get_item_input_box

()

.

send_keys

(

'Get help

\n

'

)

Implementing the Selenium Interact/Wait Pattern

|

389