Mika72
Mika72

Reputation: 411

How to switch browsers in Robot Framework using Browser library?

could someone post example code for creating two browser instances and switching between those using Robot Framework's Browser (NOT using SeleniumLibrary) library?

Namely: how to obtain correct browser id for Switch Browser keyword.

Switch Browser ${browser_id}

Full code so far:

Library          Browser
Library          String
Library          Collections

** Variables **
${url1}=    https://www.google.com
${url1}=    https://www.bing.com
@{browser_ids}=    Create List

** Test Cases **
Open Browser 1
    [Tags]   UI    dev
    New Browser    chromium    headless=false
    New Context    viewport={'width': 1920, 'height': 1080}
    New Page       ${url1}

Open Browser 2
    [Tags]   UI    dev
    New Browser    chromium    headless=false
    New Context    viewport={'width': 1920, 'height': 1080}
    New Page       ${url2}

Page Switch tests

    [Tags]   UI    dev

    Switch to Browser1
    Get Element   text=Gmail
    Switch to Browser2
    Get Element    text=© 2021 Microsoft


** Keywords **
Switch to Browser2
    Switch Page    ${browser_ids}[1]

Switch to Browser1
    Switch Page    ${browser_ids}[0]

Store Active Browser Id
    ${active_id}=    Get Page Ids   ACTIVE    ACTIVE    ACTIVE
    Log To Console    ${active_id}
    ${active_id}=    Split String    ${active_id}[0]    =
    Log To Console    ${active_id}[1]
    Append To List    ${BROWSER_IDS}    ${active_id}

Upvotes: 0

Views: 2175

Answers (2)

mkorpela
mkorpela

Reputation: 4395

*** Settings ***
Library  Browser
Suite Setup  Setup


*** Test Cases ***
Do something with page1
   Switch Page  ${PAGE1}

Do something with page2
   Switch Page  ${PAGE1}

Page switchy
   Switch Page  ${PAGE1}
   Get Element   text=Gmail
   Switch Page  ${PAGE2}
   Get Element   text=© 2021 Microsoft

*** Keywords ***
Setup
   ${PAGE1}=  New Page  https://www.google.com
   Set Suite variable  ${PAGE1}
   ${PAGE2}=  New Page  https://www.bing.com
   Set Suite variable  ${PAGE2}

Upvotes: 1

Mika72
Mika72

Reputation: 411

Browser library has automatic teardown for Browser/Context/Page instances, so you have to create all needed instances of those in Suite Setup. Solution below:

*** Settings ***
Library          Browser
#Library          OperatingSystem
Library          Collections
Library          String
Suite Setup      Create Suite Ecosystem

*** Variables ***

@{bids}
@{cids}
@{pids}
${url1}=    https://www.google.com
${url2}=    https://www.bing.com

*** Test Cases ***

Setup Verification
    [Tags]   UI    dev
    ${bids}=   Get Browser Ids
    Log To console    ${bids}

    ${bids}=   Get Context Ids
    Log To console    ${cids}

    ${pids}=   Get Page Ids
    Log To console    ${pids}

Page Switch tests
    [Tags]   UI    dev
    Switch to Page 1
    Get Element   text=Gmail
    Switch to Page 2
    Get Element   text=© 2021 Microsoft

** Keywords **

Switch to Page 1
    Switch Page     ${pids}[0]

Switch to Page 2
    Switch Page     ${pids}[1]

Store Active Browser Id
    ${active_id}=    Get Browser Ids   ACTIVE
    #${active_id}=    Split String    ${active_id}[0]    =
    Log To Console    ${active_id}[0]
    Append To List    ${bids}    ${active_id}[0]

Store Active Context Id
    ${active_id}=    Get Context Ids   ACTIVE    ACTIVE
    Log To Console    ${active_id}
    # ${active_id}=    Split String    ${active_id}[0]    =
    Log To Console    ${active_id}[0]
    Append To List    ${cids}    ${active_id}[0]

Store Active Page Id
    [Arguments]    ${new_id}
    ${active_id}=    Get Page Ids   ACTIVE    ACTIVE    ACTIVE
    # ${active_id}=    Split String    ${active_id}[0]    =
    Log To Console    ${active_id}[0]
    Append To List    ${pids}    ${new_id}

Create Suite Ecosystem
    # Create 1B-1C-2P Ecosystem

    # 1st Browser
    New Browser    chromium    headless=false
    Store Active Browser Id

    # 1st Context of 1st Browser
    New Context    viewport={'width': 1920, 'height': 1080}
    Store Active Context Id

    # 1st Page of 1st Context of 1st Context
    New Page       ${url1}
    ${current}=    Switch Page    CURRENT
    Store Active Page Id    ${current}

    # 2nd Page of 1st Context of 1st Context
    New Page       ${url2}
    ${current}=    Switch Page    CURRENT
    Store Active Page Id    ${current} 

Upvotes: 0

Related Questions