TestCafe - <select> 中的 Select 选项

TestCafe - Select option in <select>

我有一个带有几个 <option> 标签的 <select>。其中一些通过使用 class 'is-disabled' 被禁用。我想要做的是 select 列表中的第一个可用选项。为此,我使用了在 testcafe 网站 (https://devexpress.github.io/testcafe/documentation/recipes/testing-select-elements.html) 上找到的示例,但我似乎无法让它工作。

当 运行 测试时,该工具会在 select 上单击一次,然后单击第二个,然后关闭。在此之后,没有值是 selected。

是否有更好的方法来处理期权的动态 selection?或者什么是更好的解决方案?任何帮助将不胜感激!

问候 康乃尔

SizeSelector component:

import {t, Selector} from 'testcafe';

class SizeSelector {

  constructor() {
    this._sizeSelector = Selector('.sizeSelectionGroup');
    this._selectors = this._sizeSelector.child('.productSizeSelection');
    this._widthSelector = this._selectors.nth(0);

    // todo other size types (single numeric/text)
  }

  // todo refactor
  async setFirstAvailableWidth() {
    const options = this._widthSelector.find('option'); // contains 13 elements while debugging
    const availableOptions = options.filter(node => {
      return !node.classList.contains('is-disabled');
    });

    const count = await availableOptions.count; // contains around 8 elements while debugging
    if (count > 0) {
      return await t
        .click(this._widthSelector)
        .click(availableOptions.nth(0))
        .expect(this._lengthSelector.value).ok(); // fails with value undefined
    }

    throw new Error('No available width found.');
  }
}

export default SizeSelector;

所以我不确定这是否适合您的情况。我尝试使用 class 'is-disabled' 模拟一个有多个选项的下拉列表,并使用一个函数进行测试,该函数将单击下拉列表中的第一个选项未禁用我基于功能 this

这是我制作的示例下拉菜单

https://jsfiddle.net/L6p2u/190/

这是测试代码(橙色应该是第一个未禁用的选项)

import { Selector, t } from 'testcafe';

fixture `testcafe canvas`
    .page `https://jsfiddle.net/L6p2u/190/`;

const medwait            = 5000
const longwait           = 15000;
const dropdown           = Selector('#colour');

async function selectFirstAvailableOption(selector) {
    const select = selector;
    await t // select the first available option
        .setTestSpeed(0.7)
        .hover(select)
        .expect(select.hasAttribute("disabled")).notOk({timeout: 5000})
        .click(select)
        .click(select
            .find("option")
            .filter((node) => {
                if (node && node.className.indexOf('is-disabled') == -1) {
                    return true;
                }
                return false;
            })
            .nth(0)).wait(5000); // this wait is just so you can see
}

test('Instructor', async t => {
    await t
        .switchToIframe('[name="result"]')
    await selectFirstAvailableOption(dropdown);
});

使用该解决方案,当您与下拉菜单交互时,您可以避免调用“.setNativeDialogHandler(() => true)”。

export async function create(name) {
  const dropdown = await Selector("#dropDownID");
  const dropdownOption = dropdown.find("option");

  await t
    .click(dropdown)
    .click(dropdownOption.withText(name))
    .click(Selector("#submitBtn"));
}