In 2.0.2 it is even simpler:

    from gluon.contrib.webclient import WebClient

    client = WebClient('http://127.0.0.1:8000/welcome/default/')

    client.get('user/register')

    data = dict(first_name = 'Homer',
                last_name = 'Simpson',
                email = 'ho...@web2py.com',
                password = 'test',
                password_two = 'test',
                _formname = 'register')  # (*)
    client.post('user/register',data = data)

    data = dict(email='ho...@web2py.com',
                password='test',
                _formname = 'login')
    client.post('user/login',data = data)
    
    client.get('index')

    # check registration and login were successful
    assert 'Welcome Homer' in client.text

Checks automatically for persistant sessions and raises exception if sessions 
break or if a web ticket is issued.
You can also do

    client.get('index')
    print client.headers
    print client.cookies # parsed as a dict
    print client.forms # dict of formname:formvalue
    print client.sessions # dict of appname:session_cookie_key
    print client.status
    print client.text




On Wednesday, 29 August 2012 23:37:07 UTC-5, Dave wrote:
>
> Very cool!
>
> On Wednesday, August 29, 2012 6:49:14 PM UTC-4, Massimo Di Pierro wrote:
>>
>> I think you are going to like this:
>> https://github.com/web2py/web2py/blob/master/gluon/contrib/webclient.py
>>
>> start web2py on port 8000. Then in a normal python shell:
>>
>>     from gluon.contrib.webclient import WebClient
>>     session = WebClient('http://127.0.0.1:8000/welcome/default/')
>>     session.get('user/register')
>>     session_id_welcome = session.cookies['session_id_welcome']
>>     print session.forms # tells you which forms are in page (*)
>>     data = dict(first_name = 'Homer',
>>                 last_name = 'Simpson',
>>                 email = 'ho...@web2py.com',
>>                 password = 'test',
>>                 password_two = 'test',
>>                 _formname = 'register')  # (*)
>>     session.post('user/register',data = data)
>>
>>     session.get('user/login')
>>     data = dict(email='ho...@web2py.com',
>>                 password='test',
>>                 _formname = 'login')
>>     session.post('user/login',data = data)
>>     
>>     session.get('index')
>>
>>     # check registration and login were successful
>>     assert 'Welcome Homer' in session.text
>>
>>     # check we are always in the same session
>>     assert session_id_welcome == session.cookies['session_id_welcome']
>>
>>
>> It understand sessions (not just web2py session)
>> It understands basic auth (not used in the example)
>> It understands web2py forms (*) and fills in the _formkeys.
>>
>> Suggestions for improvement?
>>
>> Massimo
>>
>>

-- 



Reply via email to