如何在Selenium IDE 3.7.4中处理数组

时间:2019-05-22 23:44:13

标签: selenium selenium-ide

Selenium IDE 3.7.4 中,我想做一个循环来单击具有相似Xpath的几个链接。为此,我认为在创建数组 但是尝试使用具有以下设置(命令,目标,值)的简单数组不起作用。

+----------------+---------------------------------------+---------+
| Command        | Target                                | Value   |
+----------------+---------------------------------------+---------+
| execute script | var a = "A|B|C".split("|"); return a; | arr     |
+----------------+---------------------------------------+---------+
| execute script | return 0                              | counter |
+----------------+---------------------------------------+---------+
| while          | ${counter} < 3                        |         |
+----------------+---------------------------------------+---------+
| echo           | "Element = " + ${arr[${counter}]}     |         |
+----------------+---------------------------------------+---------+
| execute script | return ${counter} + 1                 | counter |
+----------------+---------------------------------------+---------+
| end            |                                       |         |
+----------------+---------------------------------------+---------+

此测试的预期输出为:

Element = A
Element = B
Element = C

我得到以下输出:

"Element = " + ${arr[${counter}]}
"Element = " + ${arr[${counter}]}
"Element = " + ${arr[${counter}]}

如何在Selenium IDE中处理数组并打印其元素?我只发现了一个与此有关的问题here,但版本较旧 Selenium IDE中具有其他命令,例如getEval在最新版本中不存在。

在此先感谢您的帮助。

更新

由于Jim的回答,我得以在Selenium IDE上完成一个有效的数组示例。代码如下

{
  "version": "2.0",
  "name": "Array",
  "url": "https://www.seleniumhq.org",
  "tests": [{
    "name": "Untitled",
    "commands": [{
      "command": "open",
      "target": "https://www.seleniumhq.org",
      "targets": [],
      "value": ""
    }, {
      "command": "executeScript",
      "target": "var a = \"projects|download|documentation\".split(\"|\"); return a;",
      "targets": [],
      "value": "arr"
    }, {
      "command": "executeScript",
      "target": "return 0",
      "targets": [],
      "value": "counter"
    }, {
      "command": "pause",
      "target": "4000",
      "targets": [],
      "value": ""
    }, {
      "command": "while",
      "target": "${counter} < 3",
      "targets": [],
      "value": "counter"
    }, {
      "command": "executeScript",
      "target": "return \"css=#menu_\" + ${arr}[${counter}] + \" > a\";",
      "targets": [],
      "value": "output"
    }, {
      "command": "click",
      "target": "${output}",
      "targets": [],
      "value": ""
    }, {
      "command": "pause",
      "target": "3000",
      "targets": [],
      "value": ""
    }, {
      "command": "executeScript",
      "target": "return ${counter} + 1;",
      "targets": [],
      "value": "counter"
    }, {
      "command": "end",
      "target": "",
      "targets": [],
      "value": ""
    }]
  }],
  "suites": [{
    "name": "Default Suite",
    "persistSession": false,
    "parallel": false,
    "timeout": 300,
    "tests": ["78b402ba-8dfa-47d6-bee8-7a0d928db6f1"]
  }],
  "urls": ["https://www.seleniumhq.org/"],
  "plugins": []
}

1 个答案:

答案 0 :(得分:1)

这是一个不错的数组测试用例。我只做了两个小改动,现在就可以了:

  • ${arr[${counter}]}更改为${arr}[${counter}]
  • 您必须在Execute Script而不是Echo内部进行数组访问(echo只会打印您放置在其中的任何内容)

我的测试宏:(可以粘贴到Kantu Selenium IDE source code tab中)。

{
  "Name": "array",
  "CreationDate": "2019-5-23",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://stackoverflow.com/questions/56266336/how-to-handle-arrays-in-selenium-ide-3-7-4",
      "Value": ""
    },
    {
      "Command": "executeScript",
      "Target": "var a = \"A|B|C\".split(\"|\"); return a;",
      "Value": "arr"
    },
    {
      "Command": "executeScript",
      "Target": "return 0",
      "Value": "counter"
    },
    {
      "Command": "while_v2",
      "Target": "${counter} < 3   ",
      "Value": "counter"
    },
    {
      "Command": "executeScript",
      "Target": "return \"Element = \" + ${arr}[${counter}] ",
      "Value": "output"
    },
    {
      "Command": "echo",
      "Target": "${output}",
      "Value": ""
    },
    {
      "Command": "executeScript",
      "Target": " return ${counter} + 1  ",
      "Value": "counter"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}