Javascript 使用typescript将角度网格列单元格文本与字符串数组进行比较

Javascript 使用typescript将角度网格列单元格文本与字符串数组进行比较,javascript,typescript,protractor,angular-promise,Javascript,Typescript,Protractor,Angular Promise,我是typescript新手,希望从ag网格列中提取一个值列表,并将其与字符串数组进行比较。下面是我为实现这一点而编写的函数。但我的实际价值观。推(文本);似乎没有填充数组的实际值。我真的不明白承诺是怎么起作用的。这与承诺有关吗 validateRatingsValues() { const ExpectedRatingsValues: Array<string> = ['A', 'B', 'C', 'D', 'E']; const ActualRatingsValues

我是typescript新手,希望从ag网格列中提取一个值列表,并将其与字符串数组进行比较。下面是我为实现这一点而编写的函数。但我的实际价值观。推(文本);似乎没有填充数组的实际值。我真的不明白承诺是怎么起作用的。这与承诺有关吗

validateRatingsValues() {
   const ExpectedRatingsValues: Array<string> = ['A', 'B', 'C', 'D', 'E'];
   const ActualRatingsValues: Array<string> = [];
   const wrapper = element.all(by.css('.ag-pinned-left-cols-container div[col-id="name"]'))
            .getText()
            .then(text => {
                ActualRatingsValues.push(text);
            });

    let match = true;
    if (ExpectedRatingsValues != null && ActualRatingsValues != null) {
        if (ExpectedRatingsValues.length !== ActualRatingsValues.length) {
            match = false;

        } else {
            for (let i = 0; i < ActualRatingsValues.length; i++) {
                if (ActualRatingsValues[i].toString !== 
                    ExpectedRatingsValues[i].toString) {
                    match = false;
                    break;
                }
            }
        }
    } else {
        match = false;
    }

    expect(match).toBeTruthy();
}
validateatingsvalues(){
const ExpectedRatingsValues:Array=['A','B','C','D','E'];
常量实际值:数组=[];
const wrapper=element.all(by.css('.ag固定左cols容器div[col id=“name”]'))
.getText()
。然后(文本=>{
实际阻力值。推送(文本);
});
让match=true;
if(ExpectedRatingsValues!=null和&ActualRatingsValues!=null){
if(ExpectedRatingsValues.length!==实际ratingsvalues.length){
匹配=假;
}否则{
for(设i=0;i
实际上,我很惊讶您在尝试推送到
实际阻力值时没有遇到错误,因为它是一个
常量

这里发生的事情是,
getText()
调用后面的行实际上是在返回所有承诺之前执行的。这就是为什么它似乎不起作用。最简单的选择是实现异步/等待

下面是它的样子:(注意,我还稍微整理了一下代码)

异步验证值(){
const ExpectedRatingsValues:Array=['A','B','C','D','E'];
//这将在继续之前等待所有承诺的返回
const wrapper=wait element.all(by.css('.ag固定左cols容器div[col id=“name”]'))
.getText()
wrapper.forEach(项=>{
expect(ExpectedRatingsValues.indexOf(item)).ToBegreateThan(-1);
//这应该也行
expect(ExpectedRatingsValues)。toContain(item);
}
expect(wrapper.length).toEqual(ExpectedRatingsValues.length);
}

在不实现async/await的情况下,肯定有一些方法可以让它正常工作。我曾尝试创建一个示例,但后来意识到我的示例不起作用。我建议阅读Matt在评论中发布的问题/答案,以便更好地理解。其中有很多很好的信息。

您的代码中有两个问题

1)
ActualRatingsValues.push(文本)
应为
ActualRatingsValues.concat(文本)

因为
元素.all().getText()
返回一个承诺,其最终值是字符串数组,而不是字符串

2)
wrapper
是一个承诺,您可以为承诺中的
ActualRatingsValues
赋值。 为了使用
实际的持续值
,您必须在promise
then()中使用它

validateatingsvalues(){
const ExpectedRatingsValues:Array=['A','B','C','D','E'];
const wrapper=element.all(by.css('.ag固定左cols容器div[col id=“name”]'))
.getText();
让match=wrapper.then(函数(ActualRatingsValues){
让长度=ExpectedRatingsValues.length;

对于(设i=0;如果你的测试是在10-12行代码之间,这是一个很好的做法,大多数是比较代码,我可以将其移到另一个函数中。但是我一直在尝试将值从页面中获取到数组中。有什么建议吗?可能是重复的
async validateRatingsValues() {
    const ExpectedRatingsValues: Array<string> = ['A', 'B', 'C', 'D', 'E'];

    // this will wait for all the promises to be returned before proceeding
    const wrapper = await element.all(by.css('.ag-pinned-left-cols-container div[col-id="name"]'))
              .getText()

    wrapper.forEach(item => {
        expect(ExpectedRatingsValues.indexOf(item)).toBeGreaterThan(-1);

        // this should work too
        expect(ExpectedRatingsValues).toContain(item);
    }

    expect(wrapper.length).toEqual(ExpectedRatingsValues.length);
}
validateRatingsValues() {
    const ExpectedRatingsValues: Array<string> = ['A', 'B', 'C', 'D', 'E'];
    const wrapper = element.all(by.css('.ag-pinned-left-cols-container div[col-id="name"]'))
            .getText();

    let match = wrapper.then(function(ActualRatingsValues) {
        let length = ExpectedRatingsValues.length;

        for(let i=0;i<length;i++) {
            let find = ActualRatingsValues.includes(ExpectedRatingsValues[i]);
            if (find === false) {
                return find;
            }           
        }

        return true;     
    });

    // match is also a promise which eventual value is a boolean
    // why below expect doesn't consume match inside then()
    // because Jasmine can detect match is a promise and do the assertion
    // inside then() in its implement internally.
    expect(match).toBeTruthy();
}