source: main/waeup.sirp/branches/multimechanize/trunk/examples/test_scripts/example_mechanize_wikipedia.py @ 7541

Last change on this file since 7541 was 7541, checked in by uli, 13 years ago

Add sources copied from current multi-mechanize GIT repos.

File size: 1.8 KB
Line 
1#
2#  Copyright (c) 2010 Corey Goldberg (corey@goldb.org)
3#  License: GNU LGPLv3
4#
5#  This file is part of Multi-Mechanize
6#
7
8
9import mechanize
10import time
11
12
13
14class Transaction(object):
15    def __init__(self):
16        self.custom_timers = {}
17
18    def run(self):
19        # create a Browser instance
20        br = mechanize.Browser()
21        # don't bother with robots.txt
22        br.set_handle_robots(False)
23        # add a custom header so wikipedia allows our requests
24        br.addheaders = [('User-agent', 'Mozilla/5.0 Compatible')]
25
26        # start the timer
27        start_timer = time.time()
28        # submit the request
29        resp = br.open('http://www.wikipedia.org/')
30        resp.read()
31        # stop the timer
32        latency = time.time() - start_timer
33
34        # store the custom timer
35        self.custom_timers['Load_Front_Page'] = latency
36
37        # verify responses are valid
38        assert (resp.code == 200), 'Bad HTTP Response'
39        assert ('Wikipedia, the free encyclopedia' in resp.get_data()), 'Text Assertion Failed'
40
41        # think-time
42        time.sleep(2)
43
44        # select first (zero-based) form on page
45        br.select_form(nr=0)
46        # set form field
47        br.form['search'] = 'foo'
48
49        # start the timer
50        start_timer = time.time()
51        # submit the form
52        resp = br.submit()
53        resp.read()
54        # stop the timer
55        latency = time.time() - start_timer
56
57        # store the custom timer
58        self.custom_timers['Search'] = latency
59
60        # verify responses are valid
61        assert (resp.code == 200), 'Bad HTTP Response'
62        assert ('foobar' in resp.get_data()), 'Text Assertion Failed'
63
64        # think-time
65        time.sleep(2)
66
67
68
69if __name__ == '__main__':
70    trans = Transaction()
71    trans.run()
72    print trans.custom_timers
Note: See TracBrowser for help on using the repository browser.