机器人框架,Chrome新标签页问题

时间:2018-12-08 12:02:41

标签: robotframework

我有一个简单的Robot Framework脚本

*** Settings ***
Documentation  Simple Amazon.in demo
Library  SeleniumLibrary

*** Variables ***
${MESSAGE}  Hello, World

*** Test Cases ***
User must sign in to check out
    [Documentation]  This is some basic info about the test
    [Tags]  Smoke
    Open Browser  http://www.amazon.in  chrome
    Input text  id=twotabsearchtextbox  Ferrari 458
    Click Button  xpath=//div[@class='nav-search-submit nav-sprite']/input[@class='nav-input' and 1]
    Wait until page Contains  results for "Ferrari 458"
    Click Link  css=#result_0 a.s-access-detail-page
    Wait until Page Contains  Back to search results for "Ferrari 458"
    Click Button  id=add-to-cart-button
    Wait Until Page Contains  1 item added to Cart

但是,只要chrome达到Click Link css=#result_0 a.s-access-detail-page,它就会打开一个新标签,而我的机器人脚本也会失败。我该如何纠正。请帮助

1 个答案:

答案 0 :(得分:2)

您可以使用select window关键字和Get Window Titles关键字在它们之间导航Get Window Titles关键字将返回标题列表,该列表中的最后一个索引是已打开的新标签页,要从列表中访问它,您可以执行以下${Tabs[1]}(因为在此代码中,列表中只有2个值)

*** Settings ***
Documentation  Simple Amazon.in demo
Library  SeleniumLibrary

*** Variables ***
${MESSAGE}  Hello, World

*** Test Cases ***
User must sign in to check out
    [Documentation]  This is some basic info about the test
    [Tags]  Smoke
    Open Browser  http://www.amazon.in  chrome
    Input text  id=twotabsearchtextbox  Ferrari 458
    Click Button  xpath=//div[@class='nav-search-submit nav-sprite']/input[@class='nav-input' and 1]
    Wait until page Contains  results for "Ferrari 458"
    Click Link  css=#result_0 a.s-access-detail-page
    ${Tabs} =   Get Window Titles
    select window  title=${Tabs[1]}
    Wait until Page Contains  Back to search results for "Ferrari 458"
    Click Button  id=add-to-cart-button
    # Wait Until Page Contains  1 item added to Cart
    Wait Until Page Contains  Added to Cart

结果:

==============================================================================
Amazon :: Simple Amazon.in demo
==============================================================================
User must sign in to check out :: This is some basic info about th...
DevTools listening on ws://127.0.0.1:29864/devtools/browser/75b8be3c-6e76-474f-b391-d340fb322895
User must sign in to check out :: This is some basic info about th... | PASS |
------------------------------------------------------------------------------
Amazon :: Simple Amazon.in demo                                       | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\development\robot-scripts\sssss\output.xml
Log:     C:\development\robot-scripts\sssss\log.html
Report:  C:\development\robot-scripts\sssss\report.html

我更改了代码的最后一行,因为它不是有效的文本。参见代码中的注释。