结合 Robotframework 关键字和页面对象是 good/bad 做法吗?

Is it a good/bad practice to combine Robotframework keywords and page objects?

所以我有这种情况,我想将关键字文件与页面对象文件结合起来。

假设我有 foo.robot 个关键字和 bar.robot 个页面对象:


#foo.robot
*** Settings ***
Ressource  bar.robot

*** Keywords ***
Click Submit Button
   Wait Untile Element Is Visible  ${variable_from_bar}  60s
   Click Button                    ${variable_from_bar}

#bar.robot
*** Variables ***
${variable_from_bar}               //button[@id="dummybutton"]

我的想法是像这样把它们结合起来

#foobar.robot

*** Variables ***
${variable_from_bar}               //button[@id="dummybutton"]

*** Keywords ***
Click Submit Button
   Wait Untile Element Is Visible  ${variable_from_bar}  60s
   Click Button                    ${variable_from_bar}

我不应该做这个练习有什么特别的原因吗?或者以后应用后会遇到什么技术问题?

一般来说,我会建议将 .robot 重命名为 .resource,这样资源文件在命名上也会与套件文件区分开来。

至于组合,我认为这不是一个坏方法,但层次结构可以更加分离。假设有跨多个页面使用的 Web 元素,您希望将它们放入独立于页面对象资源文件的单独资源文件中。

假设有一个独特的按钮。

button.resource

*** Keywords ***
Click Unique Button
    [Arguments]    ${button_locator}
    # Handle some unique behaviour
    Click Button    ${button_locator}

这可以用于多个页面,其中按钮的用途不同,ID 不同,但按钮点击的处理方式仍然相同。

PageA.resource

*** Settings ***
Resource    button.resource

*** Variables ***
${variable_from_bar}               //button[@id="dummybutton"]

*** Keywords ***
Do Stuff
    Click Unique Button    ${variable_from_bar}

PageB.resource

*** Settings ***
Resource    button.resource

*** Variables ***
${download_file}               //button[@id="downloadbutton"]

*** Keywords ***
Do Stuff
    Select File    my_file.txt
    Click Unique Button    ${download_file}

总而言之,您可以将页面对象关键字和定位器变量合并到同一个文件中,但您应该将通用的、独立于页面的关键字保留在它们自己的资源文件中。