1

How to get value using grabAttributeFrom in codecept. It returns only object,promise. How to get the attribute value from it.

 let userid_xpath = await I.grabAttributeFrom("//div[@class='mat-form-field-infix']/input[contains(@id,'mat-input')]","id");
    var userid = "//*[@id='"+userid_xpath+"']";
    I.fillField(userid,"username")

If i use like above and execute the test, i dont get any error. but i saw the debug panel displays like below

 Emitted | step.after (I grab attribute from "//mat-label[contains(text(),'UserId')]//ancestor::label//ancestor::span//ancest... 
    Emitted | step.before (I fill field "//*[@id='[object Promise]']", "username")

How to get the attribute value and use it in a string. If i pass the userid_xpath variable in assert ; it works. but my requirement is to pass it in a string and then use it.

1 Answers1

1

Look carefully in documentation

It returns what it said to return.

Returns Promise<string> attribute value.

Communication between codeceptjs and browser asynchronous. Codeceptjs looks for synchronisation of actions sent to browser. But it can't look for browser response, so you should manually tell for codeceptjs to wait for result.

In CodeceptJS such implementation is made by Promises

To get value from Promise you should await it by let userid_xpath = await I.grabAttributeFrom(someSelector);

But awaiting of Promise will end up execution due to inner implementation of Codeceptjs. So, as said in docs:

Resumes test execution, so should be used inside async with await operator.

mark you test function as async:

Scenario("should log viewability point, when time and percent are reached", async (I) => {
  I.amOnPage(testPage);
  const userid_xpath = await I.grabAttributeFrom("//div[@class='mat-form-field-infix']/input[contains(@id,'mat-input')]","id");
  const userid = "//*[@id='" + userid_xpath + "']";

  I.fillField(userid, "username")
});