【问题标题】:gratAttributeFrom() method is not returning value. It returns only object,promisegratAttributeFrom() 方法没有返回值。它只返回对象,承诺
【发布时间】:2020-07-16 22:26:04
【问题描述】:

如何在 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 变量;有用。但我的要求是把它传入一个字符串然后使用它。

【问题讨论】:

    标签: codeceptjs


    【解决方案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")
    });
    

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2014-10-05
      • 1970-01-01
      • 2021-03-26
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多