gratAttributeFrom()方法未返回值。它只返回对象,承诺

时间:2020-04-04 20:07:14

标签: codeceptjs

如何使用Codecept中的grabAttributeFrom获得价值。它仅返回对象,承诺。如何从中获取属性值。

 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")

如果我像上面那样使用并执行测试,则不会出现任何错误。但我看到调试面板显示如下

 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")

如何获取属性值并在字符串中使用它。如果我在assert中传递userid_xpath变量;有用。但是我的要求是将其传递为字符串然后使用。

1 个答案:

答案 0 :(得分:1)

仔细查看documentation

它返回说要返回的内容。

返回Promise<string>属性值。

codeceptjs和浏览器异步之间的通信。 Codeceptjs寻找发送到浏览器的动作的同步。但是它无法寻找浏览器的响应,因此您应该手动告诉codeceptjs等待结果。

在CodeceptJS中,此类实现由Promises

完成

要从Promise中获得价值,您应该等待let userid_xpath = await I.grabAttributeFrom(someSelector);

但是由于Codeceptjs的内部实现,等待Promise最终将被执行。因此,如docs中所述:

恢复测试执行,因此应在async内部使用await运算符。

将您测试的功能标记为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")
});