AssertionError: the given combination of arguments (promise and string) is invalid for this assertion

AssertionError: the given combination of arguments (promise and string) is invalid for this assertion

我尝试使用 wdio + moch + chai。 这是我配置时 wdio 生成的代码。

import LoginPage from  '../pageobjects/login.page';
import SecurePage from '../pageobjects/secure.page';

describe('My Login application', () => {
it('should login with valid credentials', async () => {
    await LoginPage.open();

    await LoginPage.login('tomsmith', 'SuperSecretPassword!');
    await expect(SecurePage.flashAlert).toBeExisting();
    await expect(SecurePage.flashAlert).toHaveTextContaining(
        'You logged into a secure area!');
  });
});

没关系,一切正常。但是当我尝试添加 chai 并使用 chai 的断言时,我的测试失败了:

A​​ssertionError:给定的参数组合(承诺和字符串)对于此断言无效。您可以使用数组、映射、对象、集合、字符串或弱集来代替字符串。

我在异步模式下使用 wdio。这是我使用 chai

的代码
import * as chai from "chai";

import LoginPage from  '../pageobjects/login.page';
import SecurePage from '../pageobjects/secure.page';

const {expect} = chai

describe('My Login application', () => {
 it('should login with valid credentials', async () => {
    await LoginPage.open();

    await LoginPage.login('tomsmith', 'SuperSecretPassword!');
    await expect(SecurePage.flashAlert).exist;
    await expect(SecurePage.flashAlert).contain(
        'You logged into a secure area!');
   });
});

在第一个示例中,您使用的是来自 expect-webdriverio 库的断言,它们是异步操作。因此,您必须在 expect 之前添加 await 关键字以等待断言。

在第二个示例中,您使用的是来自 chai 库的断言,这不是异步操作。因此,您不需要在 expect 之前添加 await 关键字,而是必须在异步操作之前添加 await 关键字。此外,如果您使用 chai 库进行断言,则必须添加 isExistinggetText 等方法。在你的情况下应该是:

import * as chai from "chai";

import LoginPage from  '../pageobjects/login.page';
import SecurePage from '../pageobjects/secure.page';

const {expect} = chai

describe('My Login application', () => {
 it('should login with valid credentials', async () => {
    await LoginPage.open();

    await LoginPage.login('tomsmith', 'SuperSecretPassword!');

    // added ↓ added ↓                       added ↓ and changed ↓
    expect(await (await SecurePage.flashAlert).isExisting()).to.be.true;
    expect(await (await SecurePage.flashAlert).getText()).contain(
        'You logged into a secure area!');
   });
});