如何在机器人框架中参数化Open Browser关键字以从命令行无头运行?

时间:2019-11-21 06:47:04

标签: robotframework

打开浏览器$ {REMOTE_URL} $ {HEADLESS}

我想从命令行将headlesschrome值传递给Open Browser关键字

1 个答案:

答案 0 :(得分:1)

如果您想使chrome无头工作,则以下代码应该可以工作。 当您想要参数化时,请放弃此文档-https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#resource-and-variable-files

创建所有变量并将其存储在文件中,然后,在调用以下任何代码时,请使用以下语法,或,从命令行传递如下所示的各自的name:value,以使其在机器人代码中使用

robot -v <<variable file>> *.robot

OR

robot -v name:value *.robot

包装清单

(rf2) C:\Users\kgurupra>pip list
Package                         Version
------------------------------- ---------
appdirs                         1.4.3
beautifulsoup4                  4.8.1
certifi                         2018.8.24
chardet                         3.0.4
colorama                        0.4.1
configparser                    4.0.2
crayons                         0.3.0
idna                            2.8
pip                             19.3.1
requests                        2.22.0
robotframework                  3.1.2
robotframework-selenium2library 3.0.0
robotframework-seleniumlibrary  4.1.0
robotframework-xvfb             1.2.2
selenium                        3.141.0
setuptools                      40.2.0
soupsieve                       1.9.5
tqdm                            4.38.0
urllib3                         1.25.7
webdriver-manager               2.3.0
webdrivermanager                0.7.4
wheel                           0.31.1
wincertstore                    0.2
xvfbwrapper                     0.2.9

代码

*** Settings ***
Library  Selenium2Library

*** Test Cases ***
Test title
    ${options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
    Call Method    ${options}    add_argument    --headless
    #my_create_webdriver   googlechrome  ${options}
    create webdriver  Chrome  chrome_options=${options}
    Maximize Browser Window
    go to   https://google.com
    Maximize Browser Window
    capture page screenshot
    close browser

使用无头OpenBrowser

*** Settings ***
Library  Selenium2Library

*** Test Cases ***
Test title
    ${options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
    Call Method    ${options}    add_argument    --headless
    #my_create_webdriver   Chrome  ${options}
    Open Browser    http://google.com       Headless Chrome
#    create webdriver  Chrome  chrome_options=${options}
    Maximize Browser Window
    go to   https://google.com
    Maximize Browser Window
    capture page screenshot
    close browser

输出

(rf2) C:\Users\kgurupra>robot sa.robot
==============================================================================
Sa
==============================================================================
Test title                                                            ..
DevTools listening on ws://127.0.0.1:54461/devtools/browser/ff94b77f-b963-428f-835e-f5e1932a1915
Test title                                                            | PASS |
------------------------------------------------------------------------------
Sa                                                                    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\Users\kgurupra\output.xml
Log:     C:\Users\kgurupra\log.html
Report:  C:\Users\kgurupra\report.html

更好的方法

Xvfb是X窗口系统的无头显示服务器,相反,如果使chrome / firefox无头运行,为什么不使用以下方法。

安装用于Robot Framework的XvfbRobot库,这是一个用于创建虚拟显示的机器人库,可用于在无头模式下运行硒测试。

pip install robotframework-xvfb

示例

*** Settings ***
Documentation     This example demonstrates how to use current library
Library      Selenium2Library
Library      XvfbRobot

*** Test Cases ***
Create Headless Browser
    Start Virtual Display    1920    1080
    Open Browser   http://google.com
    Set Window Size    1920    1080
    ${title}=    Get Title
    Should Be Equal    Google    ${title}
    [Teardown]    Close Browser