Welcome, guest | Sign In | My Account | Store | Cart

A simple script to demonstrate web testing using Twill.

Python, 44 lines
 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

1 comment

FB36 (author) 13 years, 4 months ago  # | flag

This version uses a logger:

# Web testing using Twill
# FB - 201011254
import time
import sys
import logging
from twill import get_browser
from twill.commands import *

logging.basicConfig(filename='Twill_test.log', filemode='w',
                    level=logging.NOTSET,
                    format='%(asctime)s %(levelname)s %(message)s')
logging.info('Starting the web test...')

# Navigate to Google
try:
    logging.info(go("http://www.google.com/"))
    logging.info(code(200)) # assert page loaded fine
    # showforms()
except Exception as e:
    logging.debug(e)
    sys.exit()

# Make a search
try:
    searchStr = 'Python'
    formvalue(1, 'q', searchStr)
    submit('btnG')
    time.sleep(1)
    logging.info(code(200)) # assert page loaded fine
except Exception as e:
    logging.debug(e)
    sys.exit()

# assert the search result
try:
    find('<em>Python</em> Programming Language . Official Website')
    # logging.info(find('regex')) # will fail
except Exception as e:
    logging.debug(e)
    sys.exit()

# click the link (using regex)
try:
    follow('Python Programming Language . Official Website')
    code(200) # assert page loaded fine
except Exception as e:
    logging.debug(e)

# assert current URL
try:
    logging.info(url('http://www.python.org/')) # will pass
    logging.info(url('http://www.google.com/')) # will fail
except Exception as e:
    logging.debug(e)

logging.shutdown()