Selenium Robot Framework Python Cookie Example

In this example, you will learn how to use Selenium2Library to handle cookies in web application testing. Selenium2Library has keywords: Add Cookie, Delete All Cookie, Delete Cookie, Get Cookie Value, and Get Cookies.


Step 1: launch RIDE console, add a resource file named "resource.txt". In this resource file, import Selenium2Library and OperatingSystem library. Add a variable ${Browser}=Firefox.  This resource file will be used in the test.

When view the resource.text in the "Text Edit" view, it looks like this.

Step 2: add a new Suite named "Cookiekeywords". In this suite, import the resource.txt file as Resource, define two variables ${ExpectedCookieValue} and ${ExtractedCookieValue} and a a test case named "CookieTest".

Step 3: add following steps to the CookieTest

Test Case Keywords view looks like this

*** Settings ***
Resource          ../resource.txt

*** Variables ***
${ExpectedCookieValue}    selenium1234
${ExtractedCookieValue}    ${EMPTY}

*** Test Cases ***
    Open Browser    ${Browser}
    Maximize Browser Window
    Add Cookie    SeleniumTest    selenium1234
    Get Cookie Value    SeleniumTest
    ${ExtractedCookieValue}    Get Cookie Value    SeleniumTest
    Log    "Extracted Cookie Value"
    Log    ${ExtractedCookieValue}
    Should Be Equal    ${ExtractedCookieValue}    ${ExpectedCookieValue}    'Cookie Should Equal'
    Close Browser

Step 4: run the test as pybot and see the result. Test passed. see the result file at this location below.