Testcafe - 根据下拉选择填充输入字段

Testcafe - Populate input field based on dropdown selection

无论如何我都不是 Javascript 方面的专家,只是在我真正喜欢的 testcafe 中转转。

但是,作为一个简单的测试,我正在尝试根据从下拉列表中选择的内容来填充输入字段。真的很简单,它是一个 Title 字段,所以 Mr、Mrs 等。如果选择了 'Mr',则填充男性姓名。如果 'Mrs' 则填充女性姓名。

但是,我无法读取选择的值,一旦它被选择传递到我的 If...else 语句中,它总是转到默认的最后一个选项。有人可以帮助我吗?

我的代码:

import { Selector } from 'testcafe';

const TitleSelect = Selector('#title');
const TitleOption = TitleSelect.find('option');
const FirstName = Selector('#firstname');

fixture `Getting Started`
.page //any page with a Title dropdown and Firstname input field;

test('My first test', async t => {
await t
    .click(TitleSelect)
    .click(TitleOption.withText('Mrs'))

    if (TitleOption === 'Mr') {
        await t
        .wait(1000)
        .typeText(FirstName, "Wayne")

    } else if (TitleOption === 'Mrs') {
        await t
        .wait(1000)
        .typeText(FirstName, "Waynetta")

    } else {
        await t
        .typeText(FirstName, "something else");
    } 

    await t
    .takeScreenshot({
        path: 'If_else_statment.jpg',
        fullPage: true
      })
    .wait(2000)   
    ;        
  });

您可以使用以下代码获取输入的状态:

const value = await TitleSelect.value;

更多信息请参考 https://devexpress.github.io/testcafe/documentation/test-api/selecting-page-elements/selectors/using-selectors.html#obtain-element-state