Dom Robot测试脚本,用于检查元素是否存在并将其属性值分配给变量
在我的机器人测试自动化脚本中,我试图检测元素是否存在。如果是这样,我将尝试将一个变量设置为元素属性的值。如果它不存在,我想将该变量设置为空。尽管如此,我还是无法让它发挥作用:Dom Robot测试脚本,用于检查元素是否存在并将其属性值分配给变量,dom,automated-tests,integration-testing,robotframework,Dom,Automated Tests,Integration Testing,Robotframework,在我的机器人测试自动化脚本中,我试图检测元素是否存在。如果是这样,我将尝试将一个变量设置为元素属性的值。如果它不存在,我想将该变量设置为空。尽管如此,我还是无法让它发挥作用: My Feature Step Keyword :FOR ${rowIndex} IN @{rowIndexes} \ ${xpathColumnIndex} = Evaluate ${columnIndex} + 1 \ ${xpathRowIndex} = Evaluate
My Feature Step Keyword
:FOR ${rowIndex} IN @{rowIndexes}
\ ${xpathColumnIndex} = Evaluate ${columnIndex} + 1
\ ${xpathRowIndex} = Evaluate ${rowIndex} + 1
\ ${rowImageExist} = Element Exists table[contains(@class,'datatable')]/tbody/tr[${xpathRowIndex}]/td[${xpathColumnIndex}]/img
\ Run Keyword If ${rowImageExist} ${rowAutoBidStatus} = Get Element Attribute jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status
\ Run Keyword Unless ${rowImageExist} ${rowAutoBidStatus} = ${EMPTY}
\ Log ${rowAutoBidStatus}
Element Exists
[Documentation] Determines if the desired element exists
[Arguments] ${xpath}
${imageCount} = Get Matching Xpath Count ${xpath}
${imageExists} = Evaluate ${imageCount} > 0
[Return] ${imageExists}
它一直在“Run关键字If”关键字上显示不存在的变量${rowAutoBidStatus}。我尝试过其他组合,比如Set Variable If,但我认为robot希望我给它一个值,而不是另一个关键字来计算。问题是,在确保元素存在之前,我无法运行关键字“Get Element Attribute”。否则它将抛出一个错误
必须有一个简单的方法来做到这一点。它并没有那么复杂,我无法想象我是唯一遇到这个问题的人。我终于明白了
My Feature Step Keyword
:FOR ${rowIndex} IN @{rowIndexes}
\ ${xpathColumnIndex} = Evaluate ${columnIndex} + 1
\ ${xpathRowIndex} = Evaluate ${rowIndex} + 1
\ ${xpathNextRowIndex} = Evaluate ${xpathRowIndex} + 1
\ Get Element Info //table[contains(@class,'datatable')]/tbody/tr[${xpathRowIndex}]/td[${xpathColumnIndex}]/img data-status data-attributes
Get Element Info
[Arguments] ${xpath} ${attribute1} ${attribute2}
${imageExist} = Element Exists ${xpath}
${attr1Locator} = CATENATE SEPARATOR= xpath= ${xpath} @ ${attribute1}
${attr2Locator} = CATENATE SEPARATOR= xpath= ${xpath} @ ${attribute2}
Run Keyword If ${imageExist} Get Element Attribute1 ${attr1Locator}
Run Keyword Unless ${imageExist} Set Element Attribute1 To Empty
Run Keyword If ${imageExist} Get Element Attribute2 ${attr2Locator}
Run Keyword Unless ${imageExist} Set Element Attribute2 To Empty
Get Element Attribute1
[Arguments] ${locator}
${attributes} = Get Element Attribute ${locator}
Set Test Variable ${elementAttribute1} ${attributes}
Set Element Attribute1 To Empty
Set Test Variable ${elementAttribute1} ${EMPTY}
Get Element Attribute2
[Arguments] ${locator}
${attributes} = Get Element Attribute ${locator}
Set Test Variable ${elementAttribute2} ${attributes}
Set Element Attribute2 To Empty
Set Test Variable ${elementAttribute2} ${EMPTY}
Element Exists
[Documentation] Determines if the desired element exists
[Arguments] ${xpath}
${imageCount} = Get Matching Xpath Count ${xpath}
${imageExists} = Evaluate ${imageCount} > 0
[Return] ${imageExists}
看起来在某些情况下,我无法将关键字链接在一起,否则机器人无法正确评估这些关键字。只要我将关键字传递给“Run关键字If”和“Run关键字If”,它就可以正常工作。因此,我只为获取元素属性和将变量设置为空创建了单独的关键字。概述 你使用不正确。您必须给它一个关键字作为条件后的第一个参数,而不是变量引用。它将返回关键字返回的任何内容,您可以将其保存在变量中 换句话说,与此相反(为了清晰起见,使用管道分隔值): 如果您确实希望值是
${Empty}
而不是${None}
,则可以从Robot Framework 2.7.4开始添加ELSE
子句。它看起来像这样(为了清晰起见,我将把它分散到多行:
| | # incorrect
| | Run keyword if | <some condition> | ${variable}= | some keyword
${rowAutoBidStatus}= Run Keyword If ${rowImageExist}
... Get Element Attribute jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status
... ELSE
... Set variable | ${Empty}
我刚试过“Set Keyword If”(),它说评估条件“jquery=..”失败了:SyntaxError。我以前得到过这个,但没有成功。你说“两天后得到”是什么意思据该网站称,你5个小时前才问过questino。很抱歉给你带来了困惑。我是说两天前,我开始写这个机器人测试的时候,而不是我问这个问题的时候。大约从周三早上开始,我就一直在做这个问题。在尝试了六个想法后,在谷歌上搜索答案,并向我的团队成员询问他的答案lp,我终于把它发布到StackOverflow上。我后来一直在做这件事,最后想出了一个解决方案。再一次,对混淆感到抱歉。我将从我的回复中删除这句话。这并不能真正回答你的问题,它只是显示了一堆额外的代码。与你有相同问题的其他人可能不会从中学到任何东西这个答案没有逐行尝试理解解决方案。嗯,这是我能够设计的一个解决方案。这不是理想的解决方案,但至少是这样,如果有人有类似的解决方案,它可能会引导他们找到解决方案。我不知道“运行关键字if”可以这样使用!我一直认为它必须出现在第一列,因为我在网上找到的所有示例都是这样使用的。我也不知道ELSE子句。我尝试了你的建议,效果非常好。非常感谢!
${rowAutoBidStatus}= Run Keyword If ${rowImageExist} Get Element Attribute jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status
${rowAutoBidStatus}= Run Keyword If ${rowImageExist}
... Get Element Attribute jquery=tbody tr:eq(${rowIndex}) td:eq(${columnIndex}) img@data-status
... ELSE
... Set variable | ${Empty}