【问题标题】:Type '{}' is not assignable to type 'WebElement'类型“{}”不可分配给类型“WebElement”
【发布时间】:2019-09-27 02:54:46
【问题描述】:

上下文:我正在更新 e2e 测试以使用 async/await。当尝试将函数(返回类型为 promise.Promise )更新为异步并从测试中调用它时,将导致 TS 错误。

当前实现:

export function waitUntilElementIsVisible(element: ElementFinder): promise.Promise<WebElement> {
    browser.wait(ExpectedConditions.presenceOf(element))

    return browser.wait(ExpectedConditions.visibilityOf(element))
}

尝试过的解决方案:

export async function waitUntilElementIsVisible(element: ElementFinder): Promise<WebElement> {
   await browser.wait(ExpectedConditions.presenceOf(element))

   return browser.wait(ExpectedConditions.visibilityOf(element))
} 

函数调用如下:

it('should wait until element is visible, async () =>
     await waitUntilElementIsVisible( error_page)
     expect(web element displayed)
})

我面临以下问题:

Type '{}' is not assignable to type 'WebElement'.  Property 'getDriver' is missing in type '{}'.

我这样做正确吗?任何建议将不胜感激!

【问题讨论】:

  • 您能否发送对象error_page 的样子?
  • 这意味着这不是browser.wait(ExpectedConditions.visibilityOf(element)) 返回的内容。因此,您的类型注释很可能是错误的。
  • @PrzemyslawJanBeigert error_page 对象看起来像:class ErrorPage { getErrorPage(): ElementFinder { return $('.error-page') } }await waitUntilElementIsVisible(ErrorPage.getErrorPage())
  • 您在return 声明中错过了await。应该是return await browser.wait(ExpectedConditions.visibilityOf(element))

标签: typescript selenium-webdriver promise protractor


【解决方案1】:

函数应该如下所示:

export async function waitUntilElementIsVisible(element: ElementFinder): Promise<any> {
   await browser.wait(ExpectedConditions.visibilityOf(element))
}

首先,如果我们仔细查看visibilityOf code,我们会发现它自己调用了presenceOf,因此您不需要额外的检查。

其次,在这种情况下不需要返回任何东西,而且,作为browser.wait docs says,它返回!webdriver.promise.Promise&lt;T&gt;,但我认为它不可能是WebElement,它返回ExpectedConditions.visibilityOf返回的东西。

【讨论】:

  • 感谢您的回答和解释!
【解决方案2】:

不是答案,但需要比评论更多的空间

我很困惑,你说你是这样调用函数的

it('should wait until element is visible, async () =>
     await waitUntilElementIsVisible( error_page)
     expect(web element displayed)
})

那么你的对象是

class ErrorPage { 
  getErrorPage(): ElementFinder { 
    return $('.error-page') 
  } 
}

现在我认为一切都清楚了,ErrorPage 是对象 ({}),但你需要一个 elementFinder。所以你需要的是

ErrorPage.getErrorPage() // instead of error_page

但在评论中你说这就是你所说的方式

await waitUntilElementIsVisible(ErrorPage.getErrorPage())

那么你使用哪种方式? error_pageErrorPage.getErrorPage()

【讨论】:

    猜你喜欢
    • 2020-04-02
    • 2019-06-02
    • 2018-01-04
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2023-01-28
    相关资源
    最近更新 更多