A simple script to demonstrate web testing using Twill.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | # Web testing using Twill
# FB - 201011254
import time
import sys
from twill import get_browser
from twill.commands import *
# Navigate to Google
# b = get_browser()
# b.go("http://www.google.com/")
go("http://www.google.com/")
code(200) # assert page loaded fine
# showforms()
# Make a search
searchStr = 'Python'
formvalue(1, 'q', searchStr)
submit('btnG')
time.sleep(1)
code(200) # assert page loaded fine
##links = showlinks()
##for link in links:
## print link
# assert the search result
try:
find('<em>Python</em> Programming Language . Official Website') # will pass
# find('regex') # will fail
except Exception as e:
print e
sys.exit() # if the link is not found then must not try to continue
# click the link (using regex)
follow('Python Programming Language . Official Website')
code(200) # assert page loaded fine
# assert current URL
try:
print url('http://www.python.org/') # will pass
print
url('http://www.google.com/') # will fail
except Exception as e:
print e
|
This version uses a logger: