1

我正在努力让 Selenium 从 jQueryUI 官方插件生成的选择菜单中选择一个选项,如下所述,Selenium 打开菜单,然后单击以选择一个项目,关闭菜单,但是未选择该选项。

设置的一些背景:

  • 使用 Selenium IDE Firefox 插件
  • 使用 jQueryUI SelectMenu Demo 页面进行测试(所以我们可以确保代码片段是每个人都可以访问的,并且消除了 selectmenu 至少被正确实现,作为他们的演示站点)

测试来源:http: //jqueryui.com/selectmenu/

我尝试将 span.ui-selectmenu-text 定位为 selenium 单击,然后单击 ID=ui-id-5 的元素,但没有成功。然后我尝试单击 span.speed 按钮,然后选择该选项。也没有运气。

在所有情况下,菜单都会打开和关闭,但仍会选择原始值。

然而,奇怪的事情发生了。在 Selenium IDE 中,如果我执行以下操作,则选择它:

  1. 双击打开菜单的命令。
  2. 双击命令以选择正确的 ID 元素。
  3. 再次双击命令以选择正确的 ID 元素。

现在突然间,所选选项被更新。但是当我端到端运行脚本时,通过两次单击命令,它再次失败,并且在页面上选择了默认选项。

编码:

<tr>
    <td>open</td>
    <td>/selectmenu/</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>css=span.ui-selectmenu-text</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>id=ui-id-5</td>
    <td></td>
</tr>

如果有人能指出我正确的方向,任何提示或提示,或问题的实际解决方案,那就太好了。

在发布此之前我确实搜索过,但我能找到的唯一一个是 3 年前的,并且该解决方案不再起作用:

如何使用 ui selectmenu (jQuery) 和 selenium 进行测试

4

2 回答 2

0

如果您感兴趣的只是选择一个值,您可以在不单击并打开菜单的情况下执行此操作。只需运行:|select|实际 SELECT html 元素的定位器|你想要的值|

这是可行的,因为 jQuery.ui.selectmenu 采用 SELECT html 元素并相应地对其进行样式设置。

<tr>
  <td>select</td>
  <td>id=address-state</td>
  <td>label=Alabama</td>
</tr>

现在,如果你真的想用 UI 模拟整个用户输入,那么你应该使用 clickAt 命令,例如:

<tr>
  <td>clickAt</td>
  <td>id=ui-id-1-button</td>
  <td></td>
</tr>

其中 ui-id-1-button 是插入页面的 jQuery.ui.select 的 DIV(使用 Chrome 或 FF 中的检查器查看 DOM 结构)。要选择,请查看页面底部的 DOM,其中有一个带有 UL 的 DIV,通常通过生成的 ui-id-1-menu 的 ID 进行选择,在其中您可以使用下拉选项选择器并发送 clickAt 命令。

我想出了如何通过 user-extensions.js 命令逐页滚动下拉菜单并截取选项的屏幕截图:

Selenium.prototype.doScreenshootSelectMenuOptions = function(btnLocator, jsonOptions) {
    /**
     * Capture the jQuery.ui.selectmenu dropdown option values page by page screenshots.
     *
     * @param btnLocator    Element locator for the selectmenu button
     * @param jsonOptions   Options for the command as a JSON string. They are <code>
     *                      {"dropdownLocator":"<optional value>", "fileName":"<optional value>", "extension":"<optional value>"}
     *                      <\code>
     *                      <ul>
     *                          <li>dropdownLocator: Optional element locator for hidden selectmenu options locator. If
     *                          ommited it will be computed from options.dropdownLocator</li>
     *                          <li>fileName: Optional name of the file to use for the screenshot. It will be appended
     *                          with ' pg#' where # is the drop down page number. If omitted the default file name is
     *                          'Dropdown'</li>
     *                          <li>extension: Optional name of the file extension to use for the screenshot. If
     *                          omitted the default extension is '.png'</li>
     *                      </ul>
     */
    LOG.debug("user-extensions: doScreenshootSelectMenuOptions('" + btnLocator + "', '" + jsonOptions + "')");

    var btn = this.browserbot.findElement(btnLocator);
    if(btn.id.indexOf('-button') <= 0) {
        throw new SeleniumError("screenshootSelectMenuOptions: wrong value for 'btnLocator'. It's applied to jQuery.ui.selectmenu drop downs!");
    }

    var options = JSON.parse(jsonOptions);
    if(typeof options.dropdownLocator  === 'undefined') {
        options.dropdownLocator = btn.id.substr(0, btn.id.indexOf('-button')) + '-menu';
    } else if(options.dropdownLocator.indexOf('-menu') <= 0) {
        throw new SeleniumError("screenshootSelectMenuOptions: wrong value for 'jsonOptions.dropdownLocator'. It's applied to jQuery.ui.selectmenu drop downs!");
    }

    options.fileName  = (typeof options.fileName  !== 'undefined') ? options.fileName  : "Dropdown";
    options.extension = (typeof options.extension !== 'undefined') ? options.extension : ".png";
    var ddBtn           = this.browserbot.findElement(btnLocator),
        opts            = this.browserbot.findElement(options.dropdownLocator);
    ddBtn.scrollIntoView();     // Scroll to dropdown so it has room to open downwards
    this.doClickAt(btnLocator); // Open dropdown

    var height          = isNaN(parseInt(opts.style.height)) ? opts.scrollHeight : parseInt(opts.style.height),
        scrollHeight    = opts.scrollHeight,
        pages           = scrollHeight / height,
        level           = utils.screenshoot.getNextLevel();
    if(pages <= 0) {
        throw new SeleniumError("screenshootSelectMenuOptions could not computer the number of dropdown menu pages to screenshoot!");
    }

    // For each dropdown values page(s)
    for(var pgNr = 0; pgNr < pages; pgNr++) {
        var pgHeight = (pgNr * height);
        LOG.debug("user-extensions: screenshootSelectMenuOptions opts.scrollTo('0', '" + pgHeight + "')");
        opts.scrollTo(0, pgHeight);
        this.doScreenshotEntirePage(options.fileName + ' pg' + (pgNr + 1) + options.extension, level);
        ddBtn.scrollIntoView();
    }

    this.doClickAt(btnLocator); // Close dropdown
    utils.screenshoot.resetFromLevel(level);
};

于 2015-04-07T19:49:34.997 回答
0

实际上,我终于找到了这个问题的一个非常简单的答案。您首先在元素上使用 mouseOver,然后单击它。问题解决了。

希望能帮助到你。

保罗,当你花时间写这个答案时,为了公平起见,我会让选民选择“接受的答案”。感谢您的贡献。

于 2015-05-07T19:44:49.163 回答