CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

How to use generator inside another function of the PageObject

Open dmytro-reznichenko opened this issue 9 years ago • 3 comments

What are you trying to achieve?

I want to use a generator inside another function of the PageObject.

`module.exports = { _init() { I = actor(); },

typeItInButton: '#type', typeSignatureField: { css: 'input[type="text"]' }, signatureText: 'Test Signature', submitSignAgreementButton: '#sign-agreement-submit',

grabSignature: function* () { let grabbed = yield I.grabAttributeFrom('input[type="text"]', 'value'); return grabbed; },

typeSignatureAndProceedNext: function*() { I.click(this.typeItInButton); let value = yield* this.grabSignature(); I.say("VALUE is " + value); if (this.grabSignature() !== this.signatureText) { I.fillField(this.typeSignatureField, this.signatureText); } I.click(this.submitSignAgreementButton); }, };`

But the script ignores the row in scenario: Scenario('test successful login', function*(I) { agreementDocumentsPage.typeSignatureAndProceedNext();

I'm new to JS and CodeceptJS, as I understand it happens because typeSignatureAndProceedNext is generator as well. Is there a way to implement this? Please assist.

dmytro-reznichenko avatar Dec 01 '17 20:12 dmytro-reznichenko

I am also struggling with the use of generators while trying to make proper use of helpers and page objects. It seems that generators do not work well within helpers. However, I do have scenarios which are successfully using yield. Did you try calling yield* before the step in your scenario?

yield* agreementDocumentsPage.typeSignatureAndProceedNext();

dwaned avatar Dec 16 '17 09:12 dwaned

As of v1.1.0, you can now use async/await instead yield and yield* statements. I would highly recommend switch to it (though you need to upgrade to node >8.9).

async/await simplifies things a great deal, as I was also having problems remembering which yield or yield* to use.

To migrate, just replace the following:

  • function* with async
  • yield* with await
  • yield with await

Then that should be it!

reubenmiller avatar Jan 18 '18 21:01 reubenmiller

Here is an example straight from the docs.

'use strict';
let I;

module.exports = {

  _init() {
    I = actor();
  },

  // setting locators
  container: "//div[@class = 'numbers']",
  mainItem: {
    number: ".//div[contains(@class, 'numbers__main-number')]",
    title: ".//div[contains(@class, 'numbers__main-title-block')]"
  },

  // introducing methods
  openMainArticle() => {
    I.waitForVisible(this.container)
    let title;
    within(this.container, async () => {
      title = await I.grabTextFrom(this.mainItem.number);
      let subtitle = await I.grabTextFrom(this.mainItem.title);
      title = title + " " + subtitle.charAt(0).toLowerCase() + subtitle.slice(1);
      I.click(this.mainItem.title)
    })
    return title;
  }
}

Closing the issue due to inactivity.

reubenmiller avatar Feb 26 '18 18:02 reubenmiller