Testing Login Page Using Robot Framework


Advertisements

With Robot Framework, we have learnt the following so far −

  • Import Libraries
  • Working with variables
  • Create custom Keywords
  • How to write test-cases
  • How to create Setup and teardown
  • How to execute test cases
  • How to work with data driven test-cases

We will use all of the above features and use it to test login page in this chapter. We have a login page which takes in email-id and password. When you enter correct email id and password, you will be redirected to a welcome page. If the user enters invalid email id or password, the page will get redirected to error page.

The following screenshot shows a login page −

login page

HTML Code

<html>
   <head>
      <title>Login Page</title>
   </head>
   <body>
      <script type="text/javascript">
         function wsSubmit() {
            if (document.getElementById("email").value == "admin@gmail.com" && document.getElementById("passwd").value == "admin") {
               location.href = "http://localhost/robotframework/success.html";
            } else {
               location.href = "http://localhost/robotframework/loginfailed.html";
            }
         }
      </script>
      <div id="formdet">
         Email : <input type="text" id="email" value="" id="email" /><br/><br/>
         Password : <input type="password" id="passwd" value="" /><br/><br/>
         <input type="submit" id="btnsubmit" value="Submit" onClick="wsSubmit();"/>
      </div>
   </body>
</html>

The following screen appears when either the email-id or the password is not valid −

login password page

HTML Code

<html>
   <head> 
      <title>Login Failed</title>
   </head>
   <body>
      <div id="loginfailed">
         <h1>Login Failed</h1>
      </div>
   </body>
</html>

The following screen appears when both the email id and password are valid −

email password page

HTML Code

<html>
   <head>
      <title>Login Successful</title>
   </head>
   <body>
      <div id="loginfailed">
         <h1>Login Successful</h1>
      </div>
   </body>
</html>

Now we are going to write test cases for the above test page. To start with it, we will first run the command to open Ride.

Command

 
ride.py

Once done, we will get started with the project setup as shown below −

project setup

Click New Project and enter the name of the project.

Name project setup

We will save the type of the project as Directory. The name given to the project is testlogin.

Click OK to save the project.

Now, we will create test suite inside the project.

test suite

Click New Suite and it will display a screen as shown below −

display suite

Click OK to save the test suite. We need to import the Selenium Library since we will be working with the browser.

Import Library in the main project and also to the test suite created.

created suite

Click Library as in the above screenshot. Upon clicking Library, the following screen will appear.

screen suite

Click OK to save the library for the project.

Once the library is saved for the project, it will display the library in the settings −

settings suite

Repeat the same step for the Test suite created.

Here is the library added for Test suite −

library suite

Now in the main Project, we will create a setup and teardown. We would like to open the login page in Chrome browser and maximize the window. In teardown, we will close the browser.

For setup, we will create a user−defined keyword called Open Login Page. This keyword will take 2 arguments, login page URL and browser name.

Now, we need 2 scalar variables that will help us store the values – url and the browser name.

In ride, create 2 variables ${loginpage} and ${browser} as follows −

${loginpage}

loginpage

${browser}

browser

Save both variables.

The variables will be displayed under your project as follows −

Variables Displayed

Now, we will add the setup and teardown for the main project.

Click on the project on the left side. In settings, click Suite Setup.

Suite Setup

Suite Setup setting

We have created setup that is using user keyword Open Login Page with arguments ${loginpage} and ${browser}.

Click OK to save the setup.

Now, we have to create the user−defined keyword Open Login Page, which is done as follows −

Right−click on the project and click New User Keyword

New User Keyword project

Upon clicking New User Keyword, the following screen appears −

New User Keyword appears

Here the Keyword is given 2 arguments − ${loginpage} and ${browser}. Click OK to save the user keyword.

New User Keyword arguments

Now we need to enter the library keywords, which will open the URL.

New User Keyword library

Open Login Page user−defined keyword has the following details −

*** Keywords ***
Open Login Page
   [Arguments] ${loginpage} ${browser}
   Open Browser ${loginpage} ${browser}
   Maximize Browser Window
   Title Should Be Login Page

Now, we will create Suite Teardown for the suite.

Suite Teardown

Click Edit for Suite Teardown and enter the details −

Suite Teardown Details

For Suite teardown, we are directly using library keyword, which will close the browser. Click OK to save the suite teardown.

Now, click the Testlogin Suite we have created.

Suite Teardown login

Let us now create a setup for the test suite – Test Setup. This setup needs to get executed first.

Click Edit for Test Setup and enter the details.

Suite Teardown Setup

For the Test Setup, we have created User defined Keyword called Login Page Display, which will take the argument as ${loginpage} as in the above screenshot.

Click OK to save the test setup.

Suite Teardown save

Now, we need to create the user keyword Login Page Display.

Right-click on the test suite and click New User Keyword as shown below −

Suite Teardown click

New User Keyword will display the screen as shown below −

Suite Teardown display

Click OK to save the keyword.

Let us now enter the keyword we need for the user keyword Login Page Display.

Login Page Display

Here we want to go to the loginpage and check if the title of the page matches with the value given.

Now, we will add template to the test suite and create data driven test cases.

To create template, click on the suite and on right side click Edit for Test Template.

data driven test cases

You will be directed to the following screen −

data driven directed

Test Login is again a user-defined keyword. Click OK to save the template.

Before we create the Test Login keyword, we need some scalar variables. The scalar variables will have the details of the email-id, password, successpage, failurepage, etc.

We will create scalar variables for test suite as follows −

scalar variables for test suite

We have created email, password, successpage and failurepage scalar variables as shown in the above screenshot.

Now, we will create Test Login User defined Keyword. Right-click on the test suite and click on New User Keyword.

Create Test Login

Click OK to save the keyword.

The following screenshot shows the keywords entered for Test Login −

Screenshot Test Login

Enter Email, Enter Password, Submit Details and Login Should Fail are User Defined Keywords, which are defined as follows −

Login Should Fail

Enter Email

Enter Email

Enter Password

Enter Password

Submit Details

Submit Details

Login Should Fail

Login Should Fails

Now, we will write test cases, which will take different email id and password details to the template created.

The following is a list of test cases −

password details

Invalid email id Test case

Invalid email

The email is passed with values abcd@gmail.com and ${password} is the password stored in the variable.

Invalid Password

Invalid Password

Invalid Email Id and Password

Invalid Passwords

Empty Email Id

Empty Email Id

Empty Password

Empty Email Id

Empty Email and Password

Empty Emails Id

Now, we are done with the test cases and can run the same.

Go to the Run tab and click Start to execute the test cases.

Run Tab Execute

Here are the log messages for the test cases −

20181027 18:11:40.353 : INFO : Opening browser 'chrome' to base url '
http://localhost/robotframework/login.html'.
20181027 18:11:45.960 : INFO : Page title is 'Login Page'.
Starting test: Testlogin.Testlogin Suite.Invalid EmailId
20181027 18:11:45.991 : INFO : Opening url 'http://localhost/robotframework/login.html'
20181027 18:11:46.169 : INFO : Page title is 'Login Page'.
20181027 18:11:46.180 : INFO : Typing text 'abcd@gmail.com' into text field 'email'.
20181027 18:11:46.706 : INFO : Typing text 'admin' into text field 'passwd'.
20181027 18:11:47.075 : INFO : Clicking button 'btnsubmit'.
20181027 18:11:47.565 : INFO : Current location is 'http://localhost/robotframework/loginfailed.html'.
20181027 18:11:47.584 : INFO : Page title is 'Login Failed'.
Ending test: Testlogin.Testlogin Suite.Invalid EmailId

Starting test: Testlogin.Testlogin Suite.Invalid Password
20181027 18:11:47.600 : INFO : Opening url 'http://localhost/robotframework/login.html'
20181027 18:11:47.767 : INFO : Page title is 'Login Page'.
20181027 18:11:47.783 : INFO : Typing text 'admin@gmail.com' into text field 'email'.
20181027 18:11:48.342 : INFO : Typing text 'invalid' into text field 'passwd'.
20181027 18:11:48.701 : INFO : Clicking button 'btnsubmit'.
20181027 18:11:49.035 : INFO : Current location is 'http://localhost/robotframework/loginfailed.html'.
20181027 18:11:49.051 : INFO : Page title is 'Login Failed'.
Ending test: Testlogin.Testlogin Suite.Invalid Password

Starting test: Testlogin.Testlogin Suite.Invalid EmailId And Password
20181027 18:11:49.054 : INFO : Opening url 'http://localhost/robotframework/login.html'
20181027 18:11:49.213 : INFO : Page title is 'Login Page'.
20181027 18:11:49.221 : INFO : Typing text 'invalid' into text field 'email'.
20181027 18:11:49.555 : INFO : Typing text 'invalid' into text field 'passwd'.
20181027 18:11:49.883 : INFO : Clicking button 'btnsubmit'.
20181027 18:11:50.162 : INFO : Current location is 'http://localhost/robotframework/loginfailed.html'.
20181027 18:11:50.176 : INFO : Page title is 'Login Failed'.
Ending test: Testlogin.Testlogin Suite.Invalid EmailId And Password

Starting test: Testlogin.Testlogin Suite.Empty Emailid
20181027 18:11:50.188 : INFO : Opening url 'http://localhost/robotframework/login.html'
20181027 18:11:50.302 : INFO : Page title is 'Login Page'.
20181027 18:11:50.306 : INFO : Typing text '' into text field 'email'.
20181027 18:11:50.486 : INFO : Typing text 'admin' into text field 'passwd'.
20181027 18:11:50.693 : INFO : Clicking button 'btnsubmit'.
20181027 18:11:50.935 : INFO : Current location is 'http://localhost/robotframework/loginfailed.html'.
20181027 18:11:50.958 : INFO : Page title is 'Login Failed'.
Ending test: Testlogin.Testlogin Suite.Empty Emailid

Starting test: Testlogin.Testlogin Suite.Empty Password
20181027 18:11:50.958 : INFO : Opening url 'http://localhost/robotframework/login.html'
20181027 18:11:51.063 : INFO : Page title is 'Login Page'.
20181027 18:11:51.071 : INFO : Typing text 'admin@gmail.com' into text field 'email'.
20181027 18:11:51.367 : INFO : Typing text '' into text field 'passwd'.
20181027 18:11:51.561 : INFO : Clicking button 'btnsubmit'.
20181027 18:11:51.796 : INFO : Current location is 'http://localhost/robotframework/loginfailed.html'.
20181027 18:11:51.808 : INFO : Page title is 'Login Failed'.
Ending test: Testlogin.Testlogin Suite.Empty Password

Starting test: Testlogin.Testlogin Suite.Empty Email And Password
20181027 18:11:51.811 : INFO : Opening url 'http://localhost/robotframework/login.html'
20181027 18:11:51.908 : INFO : Page title is 'Login Page'.
20181027 18:11:51.916 : INFO : Typing text '' into text field 'email'.
20181027 18:11:52.049 : INFO : Typing text '' into text field 'passwd'.
20181027 18:11:52.193 : INFO : Clicking button 'btnsubmit'.
20181027 18:11:52.419 : INFO : Current location is 'http://localhost/robotframework/loginfailed.html'.
20181027 18:11:52.432 : INFO : Page title is 'Login Failed'.
Ending test: Testlogin.Testlogin Suite.Empty Email And Password

Conclusion

We have seen here how to test a login page with different inputs, which will validate if the login is working fine or not. The details of how the execution takes place is given in the log section.

Useful Video Courses


Video

Cyber Security : Mr Robot Real Life Scenarios Vol 1

17 Lectures 1 hours

Musab Zayadneh

Video

Cyber Security : Mr Robot Real Life Scenarios Vol 2

11 Lectures 31 mins

Musab Zayadneh

Video

UiPath Robotic Process Automation Introduction

20 Lectures 1.5 hours

Maksym Rudnyi

Video

Robot Framework and Selenium

25 Lectures 3 hours

Kamal Kishor Girdher

Video

Obstacle Avoiding Robot with Raspberry Pi

10 Lectures 34 mins

Ashraf Said

Video

Arduino with Robot Operating System (ROS)

29 Lectures 2.5 hours

Ashraf Said

Advertisements