A new and easier way to test your apps

566 views
Skip to first unread message

Massimo Di Pierro

unread,
Aug 29, 2012, 6:49:14 PM8/29/12
to web...@googlegroups.com
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.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

Massimo Di Pierro

unread,
Aug 29, 2012, 7:16:34 PM8/29/12
to web...@googlegroups.com
Some simplifications:

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.get('index')
    session_id_welcome = session.cookies['session_id_welcome']

 session.get('user/register')
   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)

Massimo Di Pierro

unread,
Aug 29, 2012, 7:23:51 PM8/29/12
to web...@googlegroups.com
added:

session.time (the time of last request)
session.history (a log of all previous requests)

Dave

unread,
Aug 30, 2012, 12:37:07 AM8/30/12
to web...@googlegroups.com
Very cool!

Michael Toomim

unread,
Aug 30, 2012, 1:33:02 AM8/30/12
to web...@googlegroups.com
Sometimes you write things that are just .... really exciting.

Massimo Di Pierro

unread,
Aug 30, 2012, 8:16:45 AM8/30/12
to web...@googlegroups.com
In 2.0.2 it is even simpler:

from gluon.contrib.webclient import WebClient


    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




David Ripplinger

unread,
Jun 25, 2016, 5:39:29 AM6/25/16
to web2py-users
How come when I try `from gluon.contrib.webclient import WebClient` I get `No module named gluon.contrib.webclient`?

Anthony

unread,
Jun 25, 2016, 8:48:42 AM6/25/16
to web2py-users
Is there a webclient.py file in your gluon/contrib folder?

David Ripplinger

unread,
Jun 25, 2016, 10:45:42 AM6/25/16
to web2py-users
It exists in ~/web2py/gluon/contrib/. Should this be copied to a system directory? Should I have done another step to put the web2py folder on the python PATH?

Anthony

unread,
Jun 25, 2016, 1:20:59 PM6/25/16
to web2py-users
If you're running a script from somewhere outside the web2py directory, then of course you must ensure web2py is in the Python path.

Anthony

Marlysson Silva

unread,
Jun 27, 2016, 6:59:01 AM6/27/16
to web2py-users
Cool, but I was no longer in gluon?
Or it's a change?

Anthony

unread,
Jun 27, 2016, 1:03:08 PM6/27/16
to web2py-users
On Monday, June 27, 2016 at 6:59:01 AM UTC-4, Marlysson Silva wrote:
Cool, but I was no longer in gluon?
Or it's a change?

Are you asking whether the webclient.py file is still in gluon? Yes, inside /contrib, exactly at the link shown below.

Anthony
 

Marlysson Silva

unread,
Jun 27, 2016, 8:26:57 PM6/27/16
to web2py-users
More or less, Is that I saw the webclient.py at web2py documentation, and long times ago, but anyway great funcionality.

David Ripplinger

unread,
Jun 28, 2016, 12:10:58 PM6/28/16
to web2py-users
Thanks. I realized that I was assuming that when testing it out in the python shell the working directory would be translated, which apparently it was not. When instead I ran it as a script from within the web2py directory, it worked fine.

Pierre

unread,
Oct 15, 2016, 12:37:18 PM10/15/16
to web2py-users
Hi,

i am testing my app with Massimo's webclient. I did some requests and everything went fine but I can't login users either with their username or email
I use the same code as below :
I get a client.status=200 but the assert Welcome fails ?
I printed client.text and searched manually for the 'Welcome ....'. Indeed no Welcome



In 2.0.2 it is even simpler:

from gluon.contrib.webclient import WebClient


    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

黄祥

unread,
Oct 15, 2016, 5:05:54 PM10/15/16
to web2py-users
perhaps its about the email value : ho...@web2py.com change it into another correct email format

best regards,
stifan

Pierre

unread,
Oct 21, 2016, 10:28:32 AM10/21/16
to web2py-users
I managed to register Homer Simpson but he can't login......it is sad.......:(

did you or someone else manage to login anybody with webclient  and a correct email address ?


Homer's "resurrection" seems tricky so I leave it for now

Daniela Festi

unread,
Aug 1, 2020, 1:02:28 AM8/1/20
to web2py-users

did you or someone else manage to login anybody with webclient  and a correct email address ?

I managed today.

For my testcases, I separated the user registration and the user login and ran into the same problem. The solution was to call client.get() one time before calling the client.post() for login; maybe it has something to do with the _formkey - magic.

HTH

Daniela
Reply all
Reply to author
Forward
0 new messages