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

问题描述 投票:0回答:1

无论如何我都不是Java专家,只是得到我真正喜欢的testcafe。

但是,作为一个简单的测试,我试图根据从下拉列表中选择的内容来填充输入字段。真的很简单,这是一个“标题”字段,所以先生,太太等。根据是否选择了“先生”,然后填充男性姓名。如果为“太太”,则将填充女性名称。

但是,一旦选择了要传递给我的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)   
    ;        
  });
javascript testing automated-tests e2e-testing testcafe
1个回答
1
投票

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

const value = await TitleSelect.value;

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

© www.soinside.com 2019 - 2024. All rights reserved.