sendKeys(index)

时间:2018-10-01 13:31:50

标签: javascript typescript asynchronous protractor

如何将值循环发送到sendKeys(value)

我尝试了不同的方法来解决它,但是没有运气。

Numbers.ts

export const Numbers = {
 1: '777',
 2: '777',
 3: '777'
};

Texts.ts

export const Texts = {
  1: '111',
  2: '222',
  3: '333'
};

Code.ts

public async readFromFile(): Promise<void> {
  const numbers: object = Numbers;
  const texts: object = Texts;

  function* generatorNumbersAndTexts(objectNumbers, objectTexts) {
    let i: string;
    let j: string;

    for (i in objectNumbers) {
      for (j in objectTexts) {
        if (objectNumbers.hasOwnProperty(i) &&   objectTexts.hasOwnProperty(j)) {
          yield i;
          yield j;
        }
      }
    }
  }

  for (let indexI of generatorNumbersAndTexts(numbers, texts)) {
    for (let indexJ of generatorNumbersAndTexts(numbers, texts)) {
      texts.hasOwnProperty(indexJ)) {
      await this.clickSendMessage();
      try {
        await this.typeContacts(numbers[indexI]);
      } catch (e) {
        throw new Error(`There is no phone field ${e}`);
      }
      await this.typeMessage(texts[indexJ]);
      await this.sendMessage();
    }
   }
  }

方法

这些是在readFromFile方法中使用的方法。

public async typeContacts(numbers: string): Promise<void> {
  await this.contactField.sendKeys(numbers + ';');
}

public async typeMessage(text: string): Promise<void> {
  await this.messageField.type(text);
}

public async type(text: string): Promise<void> {
  await this.clearInput();
  await this.sendKeys(text);
}

看来这可能是量角器承诺的问题。

2 个答案:

答案 0 :(得分:2)

这是一个量角器的问题。这是一个解决方案:

public async fromJSON(): Promise<void> {
  for (const key in Numbers) {
    if (Numbers.hasOwnProperty(key)) {
      for (const text in Texts) {
        if (Texts.hasOwnProperty(text)) {
          await this.screenshot(`./screens/1${key}.png`);
          await this.clickSendMessage();
          await this.screenshot(`./screens/2${key}.png`);
          await this.typeContacts(Numbers[key.toString()]);
          await this.typeMessage(Texts[text.toString()]);
        }
      }
    }
  }
}

答案 1 :(得分:1)

如果不查看您引用的功能,就很难给出完整的答案。但是,可以在遍历对象时使用sendKeys()
将sendKeys()与循环一起使用:

it('Should show a loop',async()=>{
  let elem = $(''); //Input value of element you are sending key to
  let values = {1:'111',2:'222',3:'333'} //test values
  for(let key in values){
    await elem.sendKeys(values[key]);
  }
}

此示例将向111222333发送elem