简体   繁体   English

从机器人框架上的关键字传递参数

[英]Pass parameter from keyword on robot framework

I am new on python and robot framework.我是 python 和机器人框架的新手。 Previously, I use ruby as testing, and I oftenly use this之前我用ruby做测试,经常用这个

feature file:

Scenario : User can open browser
 User opens browser using "Chrome"


rb file:

And (/^User opens browser using "(.*)" $/) do |browser|{
   //open browser step
}

but when i try similar step on python like this但是当我像这样在 python 上尝试类似的步骤时

*Keyword*
User opens browser using ${browser}
  IF ${browser} == Chrome
  open browser  chrome
  ELSE
  open browser  firefox
  END

*Test Cases*
User should be able to open page with browser
   User opens browser using chrome

I got response No keyword with name 'User opens browser using chrome' found.我得到响应没有找到名称为“用户使用 chrome 打开浏览器”的关键字。

Can you help me guys?你们能帮帮我吗? Or maybe there will be no clue to create similar steps on python robot framework?或者可能没有线索在 python 机器人框架上创建类似的步骤? Thank you in advance先感谢您

you need to send the ${browser} as a argument to the keyword like:您需要将 ${browser} 作为参数发送给关键字,例如:

*** Test Cases ***
Open User Browser   ${browser}

***Keywords***
Open User Browser
[Arguments]   ${inputVar}   # this variable can be called anything
   IF   '''${inputVar}''' == '''Chrome'''
      open browser   chrome
   ELSE
      open browser   firefox
   END

It looks like some spacing is incorrect but it's difficult to know whether that's from copying into here.看起来有些间距不正确,但很难知道这是否来自复制到这里。 However it is possible to use embedded arguments within keywords and I can give you an example.但是,可以在关键字中使用嵌入式 arguments,我可以举个例子。

A couple of other things to note其他一些需要注意的事项

  1. The IF statement is a bit redundant as you can pretty much pass the variable Firefox or Chrome directly to the Open Browser paramter and make it a bit cleaner. IF 语句有点多余,因为您几乎可以将变量 Firefox 或 Chrome 直接传递给 Open Browser 参数并使其更简洁。

  2. You can be strict on the embedded variables if you wish to by specifying them as different options as per example below如果您希望通过将它们指定为不同的选项,如下例所示,您可以严格限制嵌入变量

  3. Open Browser usually takes url first - if you're not specifiying url then you may need to use named arg afaik打开浏览器通常首先需要 url - 如果您没有指定 url 那么您可能需要使用命名参数 afaik

     *** Settings *** Library SeleniumLibrary Test Teardown Close Browser *** Test Cases *** User should be able to open page with browser User opens the chrome browser User tries to open an alternative browser User opens the firefox browser User tries to open browser not supported by keyword User opens the ie browser # Fails *** Keywords *** user opens the ${browser:(chrome|Chrome|firefox|Firefox)} browser Open Browser browser=${browser}

Both "Chrome" and "chrome" will work with Open Browser without switching capitilization though if you wanted to you could convert to lowercase before sending to open browser (Uses String library) “Chrome”和“chrome”都可以在不切换大写的情况下与打开浏览器一起使用,但如果您愿意,您可以在发送到打开的浏览器之前转换为小写(使用字符串库)

*** Keywords ***
user opens the ${browser:(chrome|Chrome|firefox|Firefox)} browser
    ${browser_lowercase}  Convert To Lowercase  ${browser}
    Open Browser  browser=${browser_lowercase}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM