我正试图从错误消息中获取文本并与预期进行比较,但它返回“0”;空”;由于don'没有正确的css路径或html

I am trying to get text from an error message and compare to the expected but it returns "null" since don't have proper css path or html id

本文关键字:don 由于 路径 html css 返回 获取 取文本 消息 错误 比较      更新时间:2023-09-26

我没有在表单中输入任何数据并试图保存它。它应该抛出一条错误消息。如何在javascript中获取错误消息的文本并进行比较我已经编写了以下代码,但它的值为null。

var errorMsg=element(by.id('alert-div'));
    expect(errorMsg.getAttribute('value')).toBe('Please select the platform for the app');

它显示以下错误消息

消息:null应为"请为"应用程序"。Stacktrace:错误:预期失败

.getAttribute是异步的,所以您必须以异步的方式使用它。

var errorMsg = element(by.id('alert-div'));
    errorMsg.getAttribute('value'))
    .then(function (value) {
        expect(value).toBe('Please select the platform for the app');
     })