Your ultimate guide

Showing posts with label Robot Framework. Show all posts
Showing posts with label Robot Framework. Show all posts

Handling Dropdown using SeleniumLibrary on Robot Framework

Handling Dropdown using SeleniumLibrary on Robot Framework:
Example:




Select From List By Index: This keyword is used to Select options from the dropdown list locator by indexes.
    Syntax: Select From List By Index        locators        index
    Example: Select From List By Index        id:tcg        2
    #Canada - 2

Unselect From List By Index: This keyword is used to unselect options from the dropdown list locator by indexes.
    Syntax: Unselect From List By Index        locators        index
    Example: Unselect From List By Index        xpath://*[@id='tcg']        2




Select From List By Label: This keyword is used to Select options from the dropdown list locator by labels (visible text).
    Syntax: Select From List By Label        locators        labels
    Example: Select From List By Label        name:cnt        India

Unselect From List By Label: This keyword is used to unselect options from the dropdown list locator by labels (visible text).
    Syntax: Unselect From List By Label        locators        labels
    Example: Unselect From List By Label        name:cnt        India




Select From List By Value: This keyword is used to select options from the dropdown list locator by values.
    Syntax: Select From List By Value        locators        value
    Example: Select From List By Value        id:tcg        cnd

Unselect From List By Value: This keyword is used to unselect options from the dropdown list locator by values.
    Syntax: Unselect From List By Value        locators        value
    Example: Unselect From List By Value        id:tcg        cnd




Select All From List: This keyword is used to select all options from the multi-selection list locator.
    Syntax: Select All From List        locator

Unselect All From List: This keyword is used to unselect all options from the multi-selection list locator.
    Syntax: Unselect All From List        locator








Handling Radio button and Checkbox using Robot Framework



Radio Button Keywords in SeleniumLibrary:

The following image contains the Source code of the radio button and its Output.
        


Note:  The radio button group must have shared the same name to be treated as a group and the value attribute defines the unique value associated with each radio button.
    group_name is the name of the group, and value is the id or value attribute of the actual radio button.

Select Radio Button: 
  • This keyword is used to select the radio button by using name and value/id attributes.
  • It contains Two Arguments group_name and value.
    Syntax:   Select Radio Button    group_name    value.
    Example: Select Radio Button    sex    Male 

Radio Button Should Be Set To: 
  • This keyword is used to check that a particular radio button should have been selected.
  • It contains Two Arguments group_name and value.
    Syntax: Radio Button Should Be Set To    group_name    value
    Example: Radio Button Should Be Set To    sex    Male

Radio Button Should Not Be Selected: 
  • This keyword is used to check any radio button in a group should not be selected. 
  • It contains only Argument group_name.
    Syntax: Radio Button Should Not Be Selected    group_name.
    Example: Radio Button Should Not Be Selected    sex

Page Should Contain Radio Button: check the radio button locator is found on the current page. 
    Syntax: Page Should Contain Radio Button        locator
    Example: Page Should Contain Radio Button        xpath://input[@value='Female']

Page Should Not Contain Radio Button: check if the radio button locator is not found on the current page.
    Syntax: Page Should Not Contain Radio Button        locator
    Example: Page Should Contain Radio Button        xpath://input[@value='Female21']

CheckBox Keyword in SeleniumLibrary:

Select Checkbox: This keyword is used to select the checkbox identified by the locator.
    Syntax:   Select Checkbox        locator

Unselect Checkbox: This keyword is used to remove the selection checkbox identified by the locator.
    Syntax:   Unselect Checkbox        locator

Checkbox Should Be Selected: This keyword is used to Verify the checkbox is selected.
    Syntax:   Checkbox Should Be Selected        locator

Checkbox Should Not Be Selected: This keyword is used to verify the checkbox is not selected.
    Syntax:   Checkbox Should Not Be Selected        locator

Handling Text Box Using Robot Framework



Input Text: This keyword is used to give text into the text field identified by the locator.
    Syntax: Input Text    locator    text

Clear Element Text: This keyword is used to Clear the value of the text-input element identified by the locator.
    Syntax: Clear Element Text    locator

Click Button: This keyword is used to Click the button identified by the locator.
    Syntax: Click Button    locator
Note: By default, buttons are searched using id, name and value.



Example Test Case:
  1. Open Browser Navigate to Demo Form URL: https://testingcolleges.blogspot.com/2023/08/demo-form.html
  2. Enter First Name as "Testing" and Last Name as "Colleges".
  3. Give the email ID: "123@gmail.com"
  4. Then Remove the above email ID and Add a new id: abc@gmail.com.
  5. Click on Submit button.
  6. Close the Browser.

Program:
  1. *** Settings ***
  2. Library    SeleniumLibrary
  3. *** Variables ***
  4. ${url}      https://testingcolleges.blogspot.com/2023/08/demo-form.html
  5. ${firstName}    Testing
  6. ${lastName}     Colleges
  7. ${email}    123@gmail.com
  8. ${email2}   abc@gmail.com
  9. *** Test Cases ***
  10. TextBox
  11.     Open Browser    ${url}  Chrome
  12.     Input Text    id:fname  ${firstName}
  13.     Input Text    id:lname  ${lastName}
  14.     Input Text    id:mail   ${email}
  15.     Sleep    2
  16.     Clear Element Text    id:mail
  17.     Input Text    id:mail   ${email2}
  18.     Click Button    Submit
  19.     Sleep    2
  20.     Close Browser
Output:


Robot Framework SeleniumLibrary - Browser Keywords

SeleniumLibrary:
  • SeleniumLibrary is a web testing library for Robot Framework.
  • SeleniumLibrary uses the Selenium WebDriver modules internally to control a web browser.
  • Currently, Selenium Library provides 177 Keywords. Every keyword has one specific action.
  • To use this library in the robot framework script we need to import it. In the robot framework libraries are implemented in the *** settings *** section.
    • Syntax: for importing in the selenium library
      1. *** Settings ***
      2. Library SeleniumLibrary


Browser Keywords:

Create Webdriver: 
  • It Creates an instance of Selenium WebDriver. By this, we can give the location of the Webdriver.
  • This keyword should only be used if provided by Open Browser.
  • driver_name must be a WebDriver implementation name like Chrome, Firefox, Edge, Safari, or Remote.
  • If the WebDriver is configured in a "Python_installed_location/Scripts" folder, there is no need to use the "Create Webdriver" function.
    Syntax: 
    Create Webdriver        driver_name        executable_path="path to the webdriver"

    Example:
    Create Webdriver        Chrome        executable_path="C:\Driver\chromedriver.exe"



Open Browser: This keyword helps to open a URL in a specific Browser.
    Syntax: 
            Open Browser        URL        Browser_Name(Chrome/Firefox/Edge/Safari)
    Example: 
            Open Browser        http://example.com        Chrome



Maximize Browser Window: It maximizes the current browser window.



Get Title: Returns the Title of the Current page.



Get Source: Returns the entire HTML source code of the current page.



Get Location: Returns the Current Bowser window URL.



Close Window: Close the currently opened and selected browser window/tab.



Close Browser: Close the current browser.



Close All Browser: Closes all open browsers.



Example Program:
  1. *** Settings ***
  2. Library       SeleniumLibrary

  3. *** Variables ***
  4. ${url}       https://www.google.com/

  5. *** Test Cases ***
  6. BrowserCommands
  7.        Create Webdriver       Chrome    executable_path="C:\Driver\chromedriver.exe"
  8.        Open Browser       ${url}       Chrome

  9.        Maximize Browser Window

  10.        ${title}       Get    Title
  11.        Log To Console       Title of the current page : ${title}

  12.        ${pageSource}       Get    Source
  13.        Log To Console       ${pageSource}

  14.        ${pageURL}       Get    Location
  15.        Log To Console       Current page URL : ${pageURL}

  16.        Close window
Output:
  1. Title of the page : Google
  2. <html> --------------- </html>
  3. Current page URL : https://www.google.com/

Creating Robot file & Components of Robot framework

Creating Robot Framework file:
# After doing all the setup, our PyCharm will be ready to create the robot file. 
# Every robot test case file has the extension '.robot'.
- Steps for creating a robot file:
> Right-Click on your Project/Package/Directory(based on where we need to create a robot file), select New then click on File.
> Give the name of the robot file with an extension .robot
> Example: form.robot (filename.robot)

Components of Robot Framework:
    Robot Framework scripts are mainly divided into four sections/components. By this, we can systematically arrange our script. The starting and ending names of every component must be *** (3 asterisks).
1.  *** Settings ***
2.  *** Variables ***
3.  *** Test Cases ***
4.  *** Keywords ***
*** Settings ***
    In this Settings section, we can add libraries and resources.
*** Variables ***
    In this Variables section, we will define variables that are commonly used in test cases.
*** Test Cases ***
    In this Test Cases section, we write multiple test cases with test scripts/keywords.
*** Keywords ***
    In this Keywords section, we create user define keywords.

------- *** -------

Example:

User story:
1.  As a user, I want to access the Facebook login page using the Chrome browser.
2.  I will input my username and password as abcd@gmail.com, followed by 123456.
3.  I will click on the Login Button.
4.  And then Close the browser. 
Programs:
1.  *** Settings ***
2.  Library  SeleniumLibrary
3.  
4.  *** Variables ***
5.  ${username}    abcd@gmail.com
6.  ${password}    123456
7.  
8.  *** Test Cases ***
9.  LoginTest
10.       Open Browser    https://www.facebook.com/    Chrome
11.         loginToApplication
12.         Close Browser
13.  
14.  *** Keywords ***
15.  loginToApplication
16.         Input Text    xpath://input[@id='email']    ${username}
17.         Input Text    xpath://input[@id='pass']    ${password}
18.         Click Button    Log in

We will learn all the above keywords in the coming sessions, now just try to understand the program.
#  In the *** Settings *** section, we imported a library called SeleniumLibrary. SeleniumLibrary contains a lot of keywords in the above programme we use some of the keywords supported by this library - Open Browser, Input Text, Click Button, Close Browser.
#  In the *** Variable *** section, we define 2 variables.
- username = abcd@gmail.com
- password = 123456
#  In the *** Keywords *** section, we created a user define keyword called loginToApplication. and that keyword has some actions to input a username and password and then click the login button.
- Here instead of giving username and password values directly, we give variables names with are declared in the *** Variables *** section.
- By using that variable names we can get their values here.
#  In the *** Test Cases *** section, we created a test case with the name LoginTest.
-  Here first it will open the Chrome browser then it will navigate to the Facebook login page.
-  Then it calls the loginToApplication keyword. Whatever action is inside that keyword they will be run here.
-  Finally, Browser will Close.

Robot Framework Setup in Pycharm IDE



Robot Framework In Python:

About Robot Framework
# Robot Framework is an open-source automation framework for Acceptance Testing.
# Robot Framework is easy to learn and not much programming knowledge is required.
# It uses a keyword-driven testing technology approach.
# Users can create new higher-level keywords from existing ones using the same syntax used for creating test cases.
# To write the script in selenium automation we need to know lots of methods for every action, but in the robot framework by using some keywords we can automate the web page.
# Robot framework supports a lot of Libraries - BuiltIn Library, Selenium Library, DB Library, DateTime Library etc.,
- Every Library has different keywords by importing that library we can access those keywords.
- By default, these libraries are not included in the robot framework, we need to download the library and use it.
- To automate the webpage we use Selenium Library.
 
Robot Framework setup:
1.  Install Python
2.  Install Pycharm IDE
3.  Install selenium (optional)
4.  Install robot framework
5.  Install Robot framework selenium library

Step 1: Install Python
- Goto Python's official website, then download and install the latest version of Python. 
- I am already posted how to install Python software, click here for the installation process.

Step 2: Install Pycharm IDE
- Click on this link: https://www.jetbrains.com/pycharm/
- Then download Pycharm Community Edition and install it.
- I am already posted how to install Pycharm IDE, click here for the installation process.
Step 3: Install selenium
- Go to the command prompt (win + R, then type cmd and press ENTER).
- Give the command pip install selenium and press ENTER. Then selenium will be installed.
Step 4: Install the robot framework
- Go to the command prompt (win + R, then type cmd and press ENTER).
- Give the command pip install robotframework and press ENTER.
        

Step 5: Install the robot framework Selenium library
- Go to the command prompt (win + R, then type cmd and press ENTER).
- Give the command pip install robotframework-seleniumlibrary and press Enter.
        

------------------ *** ------------------

Setup In PyCharm IDE:

    selenium, robotframework and robotframework-seleniumlibrary these 3 packages, whenever we create a new PROJECT in the PyCharm IDE we need to install these packages in that project also.

To add these packages to the project using GUI:
    1.  First, open Pycharm IDE and open your Project or create a new Project (Click on the Menu (☰),  Select File in that click on New Project and give the project name then click on Create)
    2.  To install packages, Click on Menu (☰), Select File in click on Settings.
    3.  Then select your Project (Project: your project name), then click on Project Interpreter.
    4.  Click on the "+" symbol to add a package, then search package name and install it.
a.  Search selenium, then click on install package.
b.  After installing selenium, search robotframework and then click on install package.
c.  After installing robotframework, search robotframework-seleniumlibrary and then click on install package.

To add these packages to the project using CLI (Terminal):
    1.  First, open Pycharm IDE and open your Project or create a new Project (Click on the Menu (☰),  Select File in that click on New Project and give the project name then click on Create)
    2.  Go to the Terminal in Pycharm, for that Click on the Menu(☰), select View, then select Tool windows and then click on Terminal. (Shortcut key: Alt + F12)
    3.  By default, the terminal locates the current project path.
a.  Type command pip install selenium then press ENTER, then selenium will be installed.
b.  Type the command pip install robotframework then press ENTER, and then the robot framework will be installed.
c.  Type the command pip install robotframework-seleniumlibrary and then press ENTER.

------------------ *** ------------------

Installing Plugin to PyCharm IDE:
# By default, PyChram IDE can't identify the robot framework files.
# To identify the robot file we need to add a plugin to PyCharm IDE this is a one-time activity.
# There are a lot of plugins that are there to identify the robot file (intelliBot, intelliBot @seleniumlibrary patched, Hyper RobotFramework Support etc.,) we need to add one of the plugins.
- To add Plugin to Pycharm:
- Open PyCharm IDE
- Click on Menu (☰), then select File and then click on Settings.
- In the setting tab click on Plugins
- Search for Hyper RobotFramework Support and install it.