【问题标题】:How to switch browsers in Robot Framework using Browser library?如何使用浏览器库在 Robot Framework 中切换浏览器?
【发布时间】:2021-08-19 04:16:23
【问题描述】:

有人可以发布示例代码来创建两个浏览器实例并在使用 Robot Framework 的浏览器(不使用 SeleniumLibrary)库之间切换吗?

即:如何为Switch Browser关键字获取正确的browser id。

Switch Browser ${browser_id}

到目前为止的完整代码:

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}

【问题讨论】:

    标签: browser robotframework


    【解决方案1】:
    *** 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}
    

    【讨论】:

      【解决方案2】:

      浏览器库具有浏览器/上下文/页面实例的自动拆卸功能,因此您必须在套件设置中创建所有需要的实例。解决方法如下:

      *** 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} 
      

      【讨论】:

        猜你喜欢
        • 2019-02-18
        • 2021-08-28
        • 2017-11-18
        • 2018-06-16
        • 2017-09-01
        • 2019-08-29
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        相关资源
        最近更新 更多