javascript 如何在 Selenium IDE 中创建自定义函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14377433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:29:29  来源:igfitidea点击:

How to create custom functions in Selenium IDE?

javascriptselenium-ide

提问by l0b0

This should be possible according to JavaScript Functions in Selenium IDE HTML Tests:

根据Selenium IDE HTML 测试中的 JavaScript 函数,这应该是可能的:

<tr>
    <td>storeEval</td>
    <td>function(input) {return input.replace('foo', 'bar');}</td>
    <td>replaceText</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>replaceText('foo')</td>
    <td>var</td>
</tr>

Instead I get the following exception:

相反,我得到以下异常:

function statement requires a name

After giving it a name the statement runs:

给它一个名字后,语句运行:

<tr>
    <td>storeEval</td>
    <td>function replaceText(input) {return input.replace('foo', 'bar');}</td>
    <td>replaceText</td>
</tr>

But the next line fails to find the definition:

但是下一行找不到定义:

replaceText is not defined

I've also tried referencing the variable instead of the function:

我也试过引用变量而不是函数:

<tr>
    <td>storeEval</td>
    <td>${replaceText}('foo')</td>
    <td>var</td>
</tr>

But apparently it's still undefined:

但显然它仍然未定义:

null is not a function

I also tried making an anonymous function:

我也尝试制作一个匿名函数

<tr>
    <td>storeEval</td>
    <td>(function (input) {return input.replace('foo', 'bar')})</td>
    <td>replaceText</td>
</tr>

and running it with parentheses:

并用括号运行它:

<tr>
    <td>storeEval</td>
    <td>(${replaceText})('foo')</td>
    <td>var</td>
</tr>

Error:

错误:

missing ) in parenthetical 

and without:

并且没有:

<tr>
    <td>storeEval</td>
    <td>${replaceText}('foo')</td>
    <td>var</td>
</tr>

Error:

错误:

missing ; before statement

采纳答案by katranci

What you need is a self executing anonymous function:

您需要的是一个自执行匿名函数

<tr>
    <td>storeEval</td>
    <td>(function(input) {return input.replace(input, 'bar');})('foo')</td>
    <td>replaceText</td>
</tr>

Note that you can also use your variables as parameters:

请注意,您还可以将变量用作参数:

<tr>
    <td>store</td>
    <td>'foo'</td>
    <td>searchText</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>(function(input) {return input.replace(input, 'bar');})(${searchText})</td>
    <td>replaceText</td>
</tr>

回答by user3026903

I test above,but I got a error "[error] Threw an exception: missing ) after argument list" so,I change "${searchText}" to "storedVars['searchText']", it's ok :)

我在上面进行了测试,但出现错误“[error] Throw an exception: missing ) after argument list”所以,我将“${searchText}”更改为“storedVars['searchText']”,没关系:)

ps:JavaScript can be used with two types of Selenese parameters: script and non-script (usually expressions). In most cases, you'll want to access and/or manipulate a test case variable inside the JavaScript snippet used as a Selenese parameter. All variables created in your test case are stored in a JavaScript associative array. An associative array has string indexes rather than sequential numeric indexes. The associative array containing your test case's variables is named storedVars. Whenever you wish to access or manipulate a variable within a JavaScript snippet, you must refer to it as storedVars[‘yourVariableName'].

ps:JavaScript 可以与两种 Selenese 参数一起使用:脚本和非脚本(通常是表达式)。在大多数情况下,您需要访问和/或操作用作 Selenese 参数的 JavaScript 片段内的测试用例变量。在测试用例中创建的所有变量都存储在 JavaScript 关联数组中。关联数组具有字符串索引而不是顺序数字索引。包含测试用例变量的关联数组被命名为 storedVars。每当您希望访问或操作 JavaScript 代码段中的变量时,您必须将其称为 storedVars['yourVariableName']。

http://www.seleniumhq.org/docs/02_selenium_ide.jsp#store-commands-and-selenium-variables

http://www.seleniumhq.org/docs/02_selenium_ide.jsp#store-commands-and-selenium-variables

回答by bfuzze

Katranci's answer was really useful, but once I added in for loops the variables would lose scope. I wound up using Katranci's solution inside window.eval().

Katranci 的回答非常有用,但是一旦我添加了 for 循环,变量就会失去作用域。我最终在 window.eval() 中使用了 Katranci 的解决方案。

window.eval('(function() { var trs = document.querySelectorAll(".my-list table tbody tr"); for (var x in trs) { var trc = trs[x].childNodes; for (var y in trc) { var html = trc[y].innerHTML; if (typeof html != "undefined" && html.match(/Selenium Testing/)) { return trs[x].className.replace(" lastrow", "");     }   } } } )();');

In this situation, I was creating test entries prefixed with 'Selenium Testing' and am using this code to identify those for subsequent test cases. This happens to be a page without jquery.

在这种情况下,我创建了以“Selenium 测试”为前缀的测试条目,并使用此代码来识别后续测试用例的测试条目。这恰好是一个没有 jquery 的页面。

回答by Shane Voisard

It is possible to define a function and reuse it elsewhere:

可以定义一个函数并在其他地方重用它:

<tr>
    <td>storeEval</td>
    <td>(function(){return function(min,max){return Math.floor(Math.random()*(max-min)) + min;} })()</td>
    <td>randomIntInRange</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>(function(){return storedVars['randomIntInRange'](10000,99999) +'-'+ storedVars['randomIntInRange'](1000,9999) })()</td>
    <td>randomZip</td>
</tr>
<tr>
    <td>echo</td>
    <td>${randomZip}</td>
    <td></td>
</tr>
...
[info] echo: 92105-3139

This works in Selenium IDE 2.9.0

这适用于 Selenium IDE 2.9.0