forked from schedutron/CPAP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete Example 9-5: Programmatic Web Browsing, site perhaps changed…
… a bit
- Loading branch information
1 parent
46bd3e1
commit aecc18b
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
|
||
from bs4 import BeautifulSoup, SoupStrainer | ||
from mechanize import Browser | ||
|
||
br = Browser() | ||
|
||
# home page | ||
rsp = br.open('http://us.pycon.org/2011/home/') | ||
print '\n***', rsp.geturl() | ||
print "Confirm home page has 'Log in' link; click it" | ||
page = rsp.read() | ||
assert 'Log in' in page, 'Log in not in page' | ||
rsp = br.follow_link(text_regex='Log in') | ||
|
||
# login page | ||
print '\n***', rsp.geturl() | ||
print 'Confirm at least a login form; submit invalid creds' | ||
assert len(list(br.forms())) > 1, 'no forms on this page' | ||
br.select_form(nr=0) | ||
br.form['username'] = 'xxx' # wrong login | ||
br.form['password'] = 'xxx' # wrong passwd | ||
rsp = br.submit() | ||
|
||
# login page, with error | ||
print '\n***', rsp.geturl() | ||
print 'Error due to invalid creds; resubmit w/valid creds' | ||
assert rsp.geturl() == 'http://us.pycon.org/2011/account/login', rsp.geturl() | ||
page = rsp.read() | ||
err = str(BS(page).find("div", | ||
{'id': 'errorMsg'}).find('ul').find('li').string) | ||
assert err == 'The username and/or password you specified are not correct.', err | ||
br.select_form(nr=0) | ||
br.form['username'] = 'again_bad_username' | ||
br.form['password'] = 'another_bad_password' | ||
rsp = br.submit() | ||
|
||
# login successful, home page redirect | ||
print '\n***', rsp.geturl() | ||
print 'Logged in properly on home page; click Account link' | ||
assert rsp.geturl() == 'http://us.pycon.org/2011/home', rsp.geturl() | ||
page = rsp.read() | ||
assert 'Logout' in page, 'Logout not in page' | ||
rsp = br.follow_link(text_regex='Account') | ||
|
||
# account page | ||
print '\n***', rsp.geturl() | ||
print 'Email address parseable on Account page; go back' | ||
assert rsp.geturl() == 'http://us.pycon.org/2011/account/email/', rsp.geturl() | ||
page = rsp.read() | ||
assert 'Email Addresses' in page, 'Missing email addresses' | ||
print ' Primary e-mail: %r' % str( | ||
BS(page).find('table').find('tr').find('td').find('b').string) | ||
rsp = br.back() | ||
|
||
# back to home page | ||
print '\n***', rsp.geturl() | ||
print 'Back works, on home page again; click Logout link' | ||
assert rsp.geturl() == 'http://us.pycon.org/2011/home/', rsp.geturl() | ||
rsp = br.follow_link(url_regex='logout') | ||
|
||
# logout page | ||
print '\n***', rsp.geturl() | ||
print 'Confirm on Logout page and Log in link at the top' | ||
assert rsp.geturl() == 'http://us.pycon.org/2011/account/logout/', rsp.geturl() | ||
page = rsp.read() | ||
assert 'Log in' in page, 'Log in not in page' | ||
print '\n*** DONE' |