Selenium IDE:每当按下播放按钮时关闭Ajax调用

时间:2013-05-09 23:57:06

标签: ajax jquery selenium selenium-ide

每当用户点击Selenium IDE中的播放按钮时,我都想触发一个Ajax调用。 jQuery $ .ajax函数会更好。有没有办法做到这一点?

在网上搜索20分钟后,我没有找到任何人试图这样做。

1 个答案:

答案 0 :(得分:2)

您可以使用其中一个评估JavaScript代码的Selenium命令。例如,waitForCondition命令执行JavaScript代码段,直到最后一个表达式的计算结果为真。请注意,如果您的测试页已经有jQuery,则可以跳过前两部分。

将jQuery库加载到页面

JavaScript代码段

(function(window) {
    var script = window.document.createElement("script");
    script.src = "http://code.jquery.com/jquery-1.9.1.min.js";
    script.type = "text/javascript";
    window.document.getElementsByTagName("head")[0].appendChild(script);
})(selenium.browserbot.getUserWindow());
1 == 1; // put something that evaluates to "true" here to make sure that our code runs only once

Selenium命令

<!--Inject jQuery-->
<tr>
    <td>waitForCondition</td>
    <td>(function(window) { var script = window.document.createElement(&quot;script&quot;); script.src = &quot;http://code.jquery.com/jquery-1.9.1.min.js&quot;; script.type = &quot;text/javascript&quot;; window.document.getElementsByTagName(&quot;head&quot;)[0].appendChild(script); })(selenium.browserbot.getUserWindow()); 1==1;</td>
    <td>1000</td>
</tr>

验证jQuery已下载并可以使用

JavaScript代码段

typeof selenium.browserbot.getUserWindow().jQuery == 'function'

Selenium命令

<!--Validate jQuery-->
<tr>
    <td>waitForCondition</td>
    <td>typeof selenium.browserbot.getUserWindow().jQuery == 'function'</td>
    <td>60000</td>
</tr>

进行ajax调用

JavaScript代码段

(function(window) {
    window.jQuery.ajax('http://www.google.co.uk/search?q=jquery');
})(selenium.browserbot.getUserWindow());
1 == 1;

Selenium命令

<!--Make an AJAX call-->
<tr>
    <td>waitForCondition</td>
    <td>(function(window) {window.jQuery.ajax('http://www.google.co.uk/search?q=jquery'); })(selenium.browserbot.getUserWindow()); 1 == 1;</td>
    <td>1000</td>
</tr>