如何在机器人框架的设置部分为屏幕截图动态路径

How to dynamic path for screenshot in Setting section of robot framework

我想创建一个路径 1 次以保存测试截图 例子 第一个我的路径是

*** Setting ***
Library        Selenium2Library   screenshot_root_directory=/Users/person/Documents/testrobot/screenshot30-01-2017 12-00-00

第二个我的路径是

*** Setting ***
Library        Selenium2Library screenshot_root_directory=/Users/person/Documents/testrobot/screenshot30-01-2017 12-30-00

如何设置动态路径

我的机器人代码

 *** Setting ***
Library        Selenium2Library   screenshot_root_directory=/Users/person/Documents/testrobot/screenshot
Library        DateTime
*** Variables ***

*** Keywords ***

*** Test Cases ***

谢谢

首先指定要保存屏幕截图的目录 - 并将 persist 设置为 False,因为这是一次性的 - 对于一个特定的测试:

Set Screenshot Directory    /Users/person/Documents/testrobot/screenshot30-01-2017 12-00-00    False

然后完成屏幕截图 - 并检查它是否存在:

${ScreenShotOne}    Capture Page Screenshot    Screenshot1.png
File Should Exist    /Users/person/Documents/testrobot/screenshot30-01-2017 12-00-00/Screenshot1.png

然后,更改另一个屏幕截图的目录:

Set Screenshot Directory    /Users/person/Documents/testrobot/screenshot30-01-2017 12-30-00

然后完成与上面相同的过程:

${ScreenShotTwo}    Capture Page Screenshot    Screenshot2.png
File Should Exist   /Users/person/Documents/testrobot/screenshot30-01-2017 12-30-00/Screenshot2.png

您也可以在每次创建屏幕截图时只设置路径:

${ScreenShotTwo}    Capture Page Screenshot    /Users/person/Documents/testrobot/screenshot30-01-2017 12-30-00/Screenshot2.png    False
File Should Exist   /Users/person/Documents/testrobot/screenshot30-01-2017 12-30-00/Screenshot2.png

所有这些都在 Selenium2Library 文档中找到:

Keyword: Capture Page Screenshot
Keyword: Set Screenshot Directory

@Goralight 和我在同一页上,但我创建了全局变量以在关键字中保存路径

*** Setting ***
Library        Selenium2Library
Library        DateTime

*** Variables ***
${WEB}  xxx.xxxxx.xxxx
${BROWSER}  chrome
${TYPE OF FILE}  png
*** Keywords ***

Get DateTime
  ${date1}=  Get Current Date  result_format=%Y-%m-%d %H-%M-%S
  [Return]     ${date1}

Open Web
  Open Browser  ${WEB}   ${BROWSER}
  Maximize Browser Window
  ${Date}=  Get DateTime
  Set Global Variable  ${Path}  /Users/person/Documents/testrobot/screenshot/${Date}

Close Web
  Close Window

Screenshot
  [Arguments]  ${filename}
  Set Screenshot Directory  ${Path}
  Wait Until Page Contains  Element
  # ${datetime}=  Get DateTime
  Capture Page Screenshot  ${filename}.${TYPE OF FILE}
  Log To Console  ${\n}Screenshot


*** Test Cases ***

[1] Click Home
  Open Web

我的文件夹结果是

click