【问题标题】:Testcafe: .includes() is not a function when used in an if/else statementTestcafe:.includes() 在 if/else 语句中使用时不是函数
【发布时间】:2020-01-09 17:43:48
【问题描述】:

我正在尝试在我的一个 Testcafe 案例中使用 if/else 语句,但条件是查看字符串是否包含单词(子字符串),如果不运行则转到 else

这是一个示例代码:

const textData = Selector('.css-f3fafa').innerText;

        if (textData.includes("Administrator") ) {
            await t
            .hover(button1)
            .expect(button).eql('Submit')
            .click(buttonUp)
            .setNativeDialogHandler(() => true)
            .click(modalSub)
            .wait(5000)
            .expect(thank).eql('Thank you for your submission.')

        } else {
            await t
            // .hover(button1)
            // .expect(button).eql('Sign Up')
            .expect(textData).contains('Administrator')
            .click(button1)
            .expect(getStarted).ok('Get Started')
        }

我不断收到类型错误:textData.includes... is not a function,但如果我这样做了.expect(textData).includes...,它工作得很好并且它通过了。我还尝试了indexOf,将a === true 添加到条件中,我尝试将String() 添加到变量中,这有点工作,但它直接跳到了else(即使条件为真也不通过If)

是否有另一种方法可以使这种情况与 Testcafe 一起使用?

【问题讨论】:

  • textData的实际数据类型是什么?由于某种原因,它听起来不是一个字符串。当您尝试indexOf 时发生了什么?你收到错误了吗?
  • 我也遇到了同样的错误;这是实际的元素

    Data Reviewer, Administrator, Tester

  • @DjKniteX:请您回复一下您是如何做到这一点的?我面临同样的问题,但无法使用已接受答案中的步骤修复它。
  • 嗨@Janaaaa;我让它与@Andreas 发布的示例一起使用!

标签: javascript testing ecmascript-6 automated-tests testcafe


【解决方案1】:

DOM 节点属性和方法被实现为异步 getter。

Using Selectors | TestCafe -> Obtain Element State

“选择器返回的选择器和承诺公开 API 以获取匹配元素的状态(大小、位置、类等)。参见DOM Node State请注意,这些方法和属性获取器是异步,所以使用 await 来获取元素的属性。”

文档中的示例:

const headerText = await Selector('#header').textContent;
import { Selector } from 'testcafe';

fixture `My fixture`
    .page('http://devexpress.github.io/testcafe/example/');

const windowsInput = Selector('#windows');

test('Obtain Element State', async t => {
    await t.click(windowsInput);

    const windowsInputChecked = await windowsInput.checked; // returns true
});

【讨论】:

  • 我尝试将 await 添加到我的 const 变量中,但效果不佳;我得到了这个 1)无法获取有关节点的信息,因为指定的选择器与 DOM 树中的任何节点都不匹配。
  • 信息很清楚。没有(或者可能还没有)与给定选择器匹配的元素。如果有匹配的元素,请检查文档。
  • 我想我让它工作了;我尝试使用另一种条件,但我所做的几乎是复制了您的步骤(使用我的类名),例如在 await t.click 之后立即使用 const ,然后在 if else 之前使用它,到目前为止它似乎工作
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多