首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebdriverIO -在异步模式下按索引单击元素不起作用

WebdriverIO -在异步模式下按索引单击元素不起作用
EN

Stack Overflow用户
提问于 2021-09-27 07:41:15
回答 1查看 216关注 0票数 0

WebdriverIO不再支持同步模式,我目前正在过渡到异步模式。到目前为止,一切正常,但我注意到我的按索引点击/选择元素的方法停止了工作,例如,这在同步模式下可以很好地工作:

代码语言:javascript
复制
get expandButtons()  {return $$(faqLocators.expandButton)};

clickOnExpandButtonByIndex(index: number) {
    const expandButton = this.expandButtons[index]
    expandButton.waitForExist()
    expandButton.click()
}

但是相同的方法重写为异步模式会抛出一个错误,所以在我当前的实现中,上面的方法看起来是这样的:

代码语言:javascript
复制
async clickOnExpandButtonByIndex(index: number) {
    const expandButton = await this.expandButtons[index]
    await expandButton.waitForExist()
    await expandButton.click()
}

测试中的用法如下:

代码语言:javascript
复制
it('My test', async () => {
        await expect(faqPage.arrowDownIcon).toBeDisplayed();
        await faqPage.clickOnExpandButtonByIndex(0)
        await expect(faqPage.arrowUpIcon).toBeDisplayed();
    });

上面的代码抛出了以下错误:

代码语言:javascript
复制
 Cannot read property 'waitForExist' of undefined
 TypeError: Cannot read property 'waitForExist' of undefined
    at FAQPo.<anonymous> (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:23:28)
     at step (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:33:23)
    at Object.next (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:14:53)
     at fulfilled (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:5:58)
     at processTicksAndRejections (internal/process/task_queues.js:97:5)

 Cannot read property 'click' of undefined
 TypeError: Cannot read property 'click' of undefined
    at FAQPo.<anonymous> (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:23:28)
     at step (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:33:23)
    at Object.next (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:14:53)
     at fulfilled (/Users/badb/IdeaProjects/my-test-project/project/apps/retail-app-e2e/src/pages/FAQ/FAQ.po.ts:5:58)
     at processTicksAndRejections (internal/process/task_queues.js:97:5)

我还有以下警告:

代码语言:javascript
复制
TS2532: Object is possibly 'undefined'.
TS2684: The 'this' context of type 'Element | undefined' is not assignable to method's 'this' of type 'Element'.   Type 'undefined' is not assignable to type 'Element'.

我在文档中读到,这是异步模式下的常见问题,但建议的解决方案在我的情况下不起作用。并且我无法单击按索引与多个元素交互。有人能解释一下如何在异步模式下实现上述方法吗?我参考了这篇文章https://webdriver.io/docs/sync-vs-async/

EN

回答 1

Stack Overflow用户

发布于 2021-10-07 11:16:18

代码语言:javascript
复制
   const expandButtons = await this.expandButtons()

    const expandButton = await this.expandButtons[index]

Expand按钮是一个函数而不是数组,所以等待函数给元素数组,然后在其中调用index

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69342878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档