Skip to content

christseng89/Robot_Framework_Course

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Robot Framework Course

Python Virtual Environment

Create Virtual Environment

python -m venv venv
venv\Scripts\activate

Set Virtual Environment in PyCharm

  1. Open File → Settings (or Ctrl + Alt + S).
  2. Navigate to Project: Robot_Framework → Python Interpreter
  3. Add Interpreter -> Add Local Interpreter
  4. Choose 'Select existing' option -> OK -> OK
  5. Restart PyCharm

Installing Robot framework and required plugins

  1. Open File -> Settings -> Plugins -> Marketplace
  2. Search for 'IntelliBot #Update' -> Install
  3. Restart PyCharm

Robot Framework Official Documentation

SeleniumLibrary Official Documentation

pip install robotframework
robot --version
   Robot Framework 7.2.2 (Python 3.13.1 on win32)

pip install robotframework-seleniumlibrary 
python -c "import SeleniumLibrary; print(SeleniumLibrary.__version__)"
   6.7.0
python -c "import selenium; print(selenium.__version__)"               
   4.29.0

REM Allure Library
pip install allure-robotframework
allure --version                
   2.32.0

REM Data Driver - Excel
pip install robotframework-datadriver
pip install pandas openpyxl

OR

  1. Open File → Settings (or Ctrl + Alt + S).
  2. Navigate to Project: Robot_Framework → Python Interpreter
  3. Click '+'
    • robotframework -> Install Package
    • robotframework-SeleniumLibrary -> Install Package
  4. Close -> OK

Creating the first robot demo case

robot first_demo.robot

report.html -> Open In -> Browser -> Chrome

Handling Sync issues

https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Set%20Selenium%20Implicit%20Wait

...
Open Browser    ${URL}    ${BROWSER}
Set selenium implicit wait    ${TIMEOUT}
...
robot -d reports first_demo.robot

Download Selenium Webdriver Firefox and Test

https://github.com/mozilla/geckodriver/releases

robot -d reports first_demov2.robot
REM Change config.robot => ${BROWSER_TYPE} = Firefox or Chrome

Variables and their scope

robot -d tests test_variables.robot
REM ${env}=    uat or qa
robot -d tests -v env:qa test_variables.robot

robot -d reports first_demov2.robot
REM Change config.robot => ${BROWSER_TYPE} = 1 for Chrome or 2 for Firefox 
REM ${BROWSER}            ${browsers["${BROWSER_TYPE}"]}

robot -d reports -v BROWSER_TYPE:1 first_demov2.robot
robot -d reports -v BROWSER_TYPE:2 first_demov2.robot

Keywords and Argumented Keywords

robot -d tests test_keywords.robot
robot -d tests -v browser:firefox test_keywords.robot
robot -d tests -v browser:chrome test_keywords.robot
REM Resource file Resource\commons.robot

REM Resource file Resource\common.robot
robot -d reports -v BROWSER_TYPE:1 first_demov2.robot
robot -d reports -v BROWSER_TYPE:2 first_demov2.robot

Automating the registration by using a Python locators file

https://www.way2automation.com/ https://www.way2automation.com/way2auto_jquery/index.php

robot -d tests test_registration.robot

robot -d reports -v BROWSER_TYPE:1 first_demov2.robot
robot -d reports -v BROWSER_TYPE:2 first_demov2.robot

xPath and CSS Selectors

Xpath vs CSS Part 1

  • Xpath Format ...
//tagname[@attribute='value']
//id[@id='username']
//input[@name='username']
$x("(//input[@name='username'])")
$x("(//input[@name='username'])[1]")
$x("(//input[@name='username'])[2]")

Xpath vs CSS Part 2

REM Starts with subxxx
$x('//*[starts-with(@id, "sub")]')
$x('//*[starts-with(@id, "111")]')
$x('//*[starts-with(@id, "iden")]')[0]

REM Contains xxxsubxxx
$x('//*[contains(@id, "sub")]')

REM Gmail contains
$x('//*[contains(@id, "fierId")]')
$x('//*[contains(@id, "fierId")]/..')
$x('//*[contains(@id, "fierId")]/parent::div')
$x('//*[contains(@id, "fierNext")]')
$x('//input[contains(@id, "fierId")]')

REM Following sibling
$x('//*[contains(@id, "fierId")]/following-sibling::input')
  • Following sibling: 在 XPath 中用于选择当前节点之后的同级元素(即具有相同父节点的元素)。 它用于选择当前元素的下一个兄弟节点。
<div>
  <p id="first">这是第一个段落。</p>
  <p id="second">这是第二个段落。</p>
  <p id="third">这是第三个段落。</p>
</div>
$x('//p[@id="first"]/following-sibling::p')
   (2) [p#second, p#third]
$x('//p[@id="first"]/following-sibling::p')[0]
   <p id="second">这是第二个段落。</p>
  • Xpath Format ...
//tagName[@attribute='value']
//tagName[@attribute='value'][@attribute='value']
//tagName[starts-with(@attribute,'value')]
//tagName[contains(@attribute,'value')]

//tagName[text()='value']
//tagName[contains(text(),'partialvalue')]

//tagName[@attribute='value']/..
//tagName[@attribute='value']/parent::tagname
//tagName[@attribute='value']/following-sibling::tagname
//tagName[@attribute='value']/preceding-sibling::tagname[1]

Xpath vs CSS Part 3

  • CSS Format ...
$$('body > form > input[type=text]:nth-child(1)')

REM Gmail
$$('#yDmH0d > c-wiz > div > div.UXFQgc > div > div > div.AcKKx > form > span > section > div > div > div.AFTWye.vEQsqe >) div > div.aCsJod.oJeWuf > div > div.Xb9hP > div'
$$("input[id='identifierId']")
$$("input[id='identifierId'][type='email']")
$$("input[id^='identif']")
$$("input[id$='fierId']")
$$("input[id*='dentif']")
$$('#identifierId')
$$('input#identifierId')
$$("input.whsOnd.zHQkBf")[0]

$$("div.rFrNMe.X3mtXb.UOsO2.ToAxb.zKHdkd.sdJrJc.Tyc9J")

document.querySelector("#identifierId")

Advance Topics

Handling Tabs and Popup windows

robot -d tests test_registration.robot

robot -d tests test_windowHandling.robot

Conditional statements - If and Else

robot -d tests test_if_else.robot
robot -d tests -v num1:15 test_if_else.robot
robot -d tests -v num1:8 test_if_else.robot

Loops

robot -d tests test_loops.robot

Handling Dropdown list

robot -d tests test_dropdown.robot

robot -d tests -v LANG:zh test_dropdown.robot
robot -d tests -v LANG:en test_dropdown.robot
robot -d tests -v LANG:hi test_dropdown.robot
robot -d tests -v LANG:ja test_dropdown.robot

Handling Checkboxes

robot -d tests test_checkboxes.robot

Handling Frames

robot -d tests test_frames.robot

Handling Alerts

Alert Page (via Window Handling)

robot -d tests test_alerts.robot

Handling Complex User Gestures

Handling MouseOver

robot -d tests test_mouseover.robot

Handling Drag and Drop Offsets (Slider)

robot -d tests test_slider.robot
robot -d tests -v OFFSET_X:700 test_slider.robot

Refactor the code #1

robot -d tests test_calendar.robot
robot -d tests test_calendar_jq.robot
robot -d tests DataDrivenTest\test_datadriven.robot

Handling Resizable elements

robot -d tests test_resizable.robot
robot -d tests -v OFFSET_X:700 test_resizable.robot

Handling Drag and Drop

robot -d tests test_drag_and_drop.robot

Handling Right click and Multi mouse over elements

robot -d tests test_right_click.robot

JavaScipt, Screen Shots and Headless Mode

Handling JavaScript Execution

robot -d tests test_javascript.robot

Capture Screenshots (Element and Full Page)

https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Capture%20Element%20Screenshot

robot -d tests test_javascript.robot
robot -d tests test_javascript2.robot

Running the test in Headless mode

  • headlesschrome
    launching browser  https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_submit_get    headlesschrome

Handing Web Tables, Calendar/Date Picker and Custom Python Library

Handling WebTables

robot -d tests test_webtables.robot

Handling Calendars - Integrating Custom Python Library

robot -d tests test_calendar.robot

Format Robot Framework Test Cases

pip install -U robotframework-tidy
robotidy test_calendar.robot

Exception Handling in ROBOT Framework

robot -d tests test_exception.robot
robot -d tests -v DIVISOR:4 test_try_except.robot
robot -d tests -v DIVISOR:0 test_try_except.robot

robot -d tests test_try_except2.robot
robot -d tests -v email_id:email test_try_except2.robot

Allure Reports

Generating Allure Reports

pip install allure-robotframework
  • NodeJS
npm install -g allure-commandline --save-dev
robot -d tests --listener allure_robotframework test_try_except2.robot
REM output\allure folder

robot -d tests --listener allure_robotframework:Allure_Report test_try_except2.robot
robot -d tests -v email_id:email --listener allure_robotframework:Allure_Report test_try_except2.robot
REM Failed Test Case
robot -d tests --listener allure_robotframework:Allure_Report test_calendar.robot
  • Open Allure Report - last one Only
allure serve Allure_Report
REM 2 Test Cases ONLY...
REM Timeline will show the 3 runs

Capture screenshots and attaching it to Allure Reports

  • Edit test_try_except2.robot
Library             BuiltIn
Library             SeleniumLibrary
Library             AllureLibrary
Library             Built_In/CustomLib.py
...
    FINALLY
        Capture Screenshot
        close browser
...
Divide
    [Arguments]    ${a}    ${b}
    TRY
        ${result}=    Evaluate    ${a} / ${b}
        RETURN    PASS    ${result}
    EXCEPT
        RETURN    FAIL    0
    END

  • Edit CustomLib.py
# Use -d tests from the Command Line ...
...
import os
...

def capture_screenshot():
    lib = get_selenium_lib()
    randomNum = random.randint(0, 9999)
    fileName = f"seleniumLib-{randomNum}.png"

    # Get the output directory from Robot Framework
    output_dir = BuiltIn().get_variable_value('${OUTPUT_DIR}', 'tests1')

    # Construct absolute path for the screenshot
    file_path = os.path.join(output_dir, fileName)

    # Capture screenshot
    lib.capture_page_screenshot(file_path)

    # Attach file to Allure report if Allure is available
    allure = get_allure_lib()
    if allure:
        allure.attach_file(file_path)

    return file_path
robot -d tests test_try_except2.robot
robot -d tests -v email_id:email test_try_except2.robot
robot -d tests -v email_id:email --listener allure_robotframework:Allure_Report test_try_except2.robot
robot -d tests --listener allure_robotframework:Allure_Report test_try_except2.robot

robot -d tests --listener allure_robotframework:Allure_Report test_calendar.robot
  • test_calendar.robot
Teardown
    run keyword if test failed    Capture Screenshot
    
allure serve --port 2666 Allure_Report

Parallet Test Execution

Parallel Test Execution using Pabot

pip install -U robotframework-pabot
robot -d tests --listener allure_robotframework:Allure_Report DataDrivenTest\test_datadriven.robot

pabot --processes 2 -d tests --listener allure_robotframework:Allure_Report DataDrivenTest\test_datadriven.robot
   ...
   Robot Framework remote server at 127.0.0.1:8270 started.
   Storing .pabotsuitenames file
   2025-03-11 14:47:52.104115 [PID:6764] [0] [ID:0] EXECUTING Test Datadriven
   2025-03-11 14:48:07.381371 [PID:6764] [0] [ID:0] still running Test Datadriven after 15.0 seconds
   2025-03-11 14:48:27.715000 [PID:6764] [0] [ID:0] still running Test Datadriven after 35.0 seconds
   2025-03-11 14:48:53.116358 [PID:6764] [0] [ID:0] still running Test Datadriven after 60.0 seconds
   2025-03-11 14:49:07.098242 [PID:6764] [0] [ID:0] PASSED Test Datadriven in 73.8 seconds
   4 tests, 4 passed, 0 failed, 0 skipped.
   ===================================================
   Output:  C:\Users\samfi\PycharmProjects\Robot_Framework\tests\output.xml
   Log:     C:\Users\samfi\PycharmProjects\Robot_Framework\tests\log.html
   Report:  C:\Users\samfi\PycharmProjects\Robot_Framework\tests\report.html
   ...
   Total testing: 1 minute 13.80 seconds
   Elapsed time:  1 minute 15.82 seconds
   
  • Test all robots in the folder Parallel
REM All folders and subfolders
pabot --processes 3 -d tests --listener allure_robotframework:Allure_Report .

REM Only the current folder
pabot --processes 3 -d tests --listener allure_robotframework:Allure_Report test*.robot
   ...
   34 tests, 34 passed, 0 failed, 0 skipped.
   ...
   Total testing: 6 minutes 36.30 seconds
   Elapsed time:  2 minutes 24.37 seconds

allure serve --port 2666 Allure_Report
  • Test Subfolder
pabot --processes 3 -d tests --listener allure_robotframework:Allure_Report .\DataDrivenTest
   ...
   4 tests, 4 passed, 0 failed, 0 skipped.
   ...
   Total testing: 52.50 seconds
   Elapsed time:  54.19 seconds
 

Tagging, Fixtures and Assertions

Tagging the test cases

  • test_try_except.robot
  • test_try_except2.robot
Sample Try / Except
    [tags]    try_except
pabot -d tests --processes 2 --listener allure_robotframework:Allure_Report --include try_except .
   ...
   2 tests, 2 passed, 0 failed, 0 skipped.
   ...
   
pabot -d tests --processes 3 --listener allure_robotframework:Allure_Report --exclude try_except test*.robot
   ...
   32 tests, 32 passed, 0 failed, 0 skipped.
   ...
   

Test Fixtures

  • Test Setup (Each Test Case)

  • Test Teardown (Each Test Case)

  • Suite Setup (Only Once)

  • Suite Teardown (Only Once)

robot -d tests --listener allure_robotframework:Allure_Report Fixtures\LoginTest.robot
   ...
   Create DB Connection
   Login Test                                                            Launching Browser
   .Executing Login Test
   .Closing Browser
   Login Test                                                            | PASS |
   ------------------------------------------------------------------------------
   User reg Test                                                         Launching Browser
   .Executing User reg test
   .Closing Browser
   User reg Test                                                         | PASS |
   ...                                                  
   ------------------------------------------------------------------------------
   Close DB Connection
   ...

Test Assertions (Keywords examples)

Page Object Model (POM) + Data Driven Testing + Jenkins Integration

Introduction Page Object Model (POM)

• Business Elements (Locators) provide UI locators for interaction.
• Common Resources include reusable utilities.
• The Page Objects Specification includes business keywords and references elements in the Locators, 
    which are then saved in the 'PageObjects' folder.
• Test Cases use Page Objects for structured test cases in folder 'PomTestCases'.  
• Data Driven supplies test data for automation.

Designing the first Page Object file

  • PageObjects\HomePage.robot
robot -d tests PageObjects\HomePage.robot               
    [ ERROR ] Suite 'HomePage' contains no tests or tasks. (Alright)
    REM pabot ... --exclude PageObjects\ ...

Designing the first Test Case

  • PomTestCases\FindNewCarsTest.robot
robot -d tests PomTestCases\FindNewCarsTest.robot
robot -d tests -v Brand:BMW PomTestCases\FindNewCarsTest.robot
robot -d tests -v Brand:Kia PomTestCases\FindNewCarsTest.robot

Adding the Data Driven Test

  • PomTestCases\FindNewCarsTestDataDriven.robot
...
Test Template       Find New Cars Test    # Ensure this matches the keyword name
...
*** Test Cases ***
Find New Cars Data Driven Test    ${browser}    ${brandname}    ${carheading}


*** Keywords ***
Find New Cars Test
    [Arguments]    ${browser}    ${brandname}    ${carheading}
    launching browser    ${TestSiteUrl}    ${browser}
...
    
robot -d tests PomTestCases\FindNewCarsTestDataDriven.robot
robot -d tests PomTestCases\GetCarNameTest.robot

pabot -d tests --processes 3 --listener allure_robotframework:Allure_Report PomTestCases\.
allure serve --port 2666 Allure_Report

Integrating Test with Jenkins

java -jar jenkins.war
    ...
    Jenkins initial setup is required. An admin user has been created and a password generated.
    Please use the following password to proceed to installation:
    
    b3554df7bbeb4dabb8be33b4d8839226
    
    This may also be found at: C:\Users\samfi\.jenkins\secrets\initialAdminPassword
    ...
    2025-03-13 11:56:09.548+0000 [id=26]    INFO    hudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and running
    2025-03-13 11:56:11.838+0000 [id=69]    INFO    h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
    2025-03-13 11:56:11.843+0000 [id=69]    INFO    hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1

type C:\Users\samfi\.jenkins\secrets\initialAdminPassword
    
  • http://localhost:8080/ => Install Suggested Plugins

  • New Item => RobotFramework Project => Freestyle project => OK

  • Build Steps => Add build step => Execute Windows batch command => Save

cd C:\Users\samfi\PycharmProjects\Robot_Framework
robot -d tests PomTestCases\FindNewCarsTestDataDriven.robot
  • Dashboard => RobotFramework Project => Build Now
  • Dashboard => RobotFramework Project => Last build => Console Output
  • Dashboard => Manage Jenkins => Plugins => Available plugins => HTML Publisher => Install
  • Dashboard => RobotFramework Project => Configure => Post-build Actions => Add post-build action => Publish HTML reports =>
    • HTML directory to archive => C:\Users\samfi\PycharmProjects\Robot_Framework\tests
    • Index page[s] => report.html
    • Report title => Robot Framework Report => Save => Build Now
java -Dhudson.model.DirectoryBrowserSupport.CSP="" -jar jenkins.war
  • Dashboard => RobotFramework Project => Build Now => Robot Framework Report

Robot Framework Request Library

Test API Request

REM RestAPI sample
java -jar way2automation-restapi.jar
    ...
    2025-03-16 11:11:03.226  INFO 372 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2025-03-16 11:11:03.233  INFO 372 --- [           main] c.w.testapi.TestapiApplication           : Started TestapiApplication in 10.902 seconds (JVM running for 12.3)
pip install robotframework-requests
robot -d tests test_api_request.robot

Database Testing

MySQL DB Testing

pip install robotframework-databaselibrary
pip install PyMySQL
robot -d tests test_mysql_connection.robot

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published