How to Handle Logins and User Profiles#
Imagine that you have to automate tests of a web application that requires you to log in. Sometimes you want to ensure that multiple user roles can log in and do certains tasks.
You can use the log in combo method and settings classes LoginForm1Step
, LoginForm2Steps
, and LoginCredentials
to do this at scale.
One or Two Steps to Log In#
Most websites process login in either one or two steps. This often means that the username and password fields are either on the same page or on separate pages.
Use LoginForm1Step
when username and password are prompted on the same page.
Use LoginForm2Steps
when username is prompted first, and then the option to input password appears later on the same or a separate page. The two-step variation is often to verify whether a user exists or not before password can be entered (or should be redirected to a registration page).
As LoginCredentials
is independent of the login form, it works with both options.
Class | Step 1 | Step 2 | Post Login |
---|---|---|---|
LoginForm1Step | Input username and password. Then click submit. | – | Multiple options to await confirmation or redirect. |
LoginForm2Steps | Input username and click submit. | Input password and click submit. | Multiple options to await confirmation or redirect. |
Examples#
Basic Usage#
Getting started:
Conditional Flows#
Sometimes it's useful to let a flow be dependent of succesfull handling of the login form. This is possible by setting return_bool
to True
as parameter in the settings class and using a conditional if
statement in combination with the login combo. For example:
Testing Purposes#
Let's expand the example and imagine that a website can be personalised based on which user role is signed in. For instance, we want to ensure that only the admin role has visible access to an administration module.
Firstly, let's define the settings classes:
After the the settings classes are defined, let's attempt to log both users in:
Hereafter, let's imagine that we want ensure that only the admin role has visible access to an administration module: