Jack
Jack

Reputation: 121

Protractor looping of value in sendKeys(index)

How is possible to send value to sendKeys(value) in loop?

I tried the different options to figure out it but no luck.

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();
    }
   }
  }

Methods

These are methods which were used inside readFromFile method.

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);
}

It seems it might be an issue of Protractor promises.

Upvotes: 1

Views: 147

Answers (2)

Jack
Jack

Reputation: 121

It was an issue of Protractor. This is a solution:

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()]);
        }
      }
    }
  }
}

Upvotes: 2

Ben Mohorc
Ben Mohorc

Reputation: 694

Its hard to give a fully complete answer without seeing the functions that you reference. However, it is possible to use sendKeys() while looping through an object.
Using sendKeys() with a loop:

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]);
  }
}

This example will send 111222333 to elem

Upvotes: 1

Related Questions