【问题标题】:wdio / Appium - " TypeError: $(...).waitForDisplayed is not a function" in my testwdio / Appium - “TypeError: $(...).waitForDisplayed is not a function”在我的测试中
【发布时间】:2021-06-11 15:45:47
【问题描述】:

我正在尝试学习使用 wdio 和 appium 自动化 End2end 测试 React-native 移动应用程序。

我试图在这个问题中点击的目标组件是这样的: Component screen shot

我在当前的测试项目中收到 TypeError: $(...).waitForDisplayed is not a function" 的错误。当我将执行异步模式时,我得到“未找到元素”。

我可以验证 ID 在 Appium Element Inspector 中是否可见 ScreenShot here

以下是我的代码 (#1 & #2) 无论哪种方式,我都遇到了错误。我真的需要了解为什么会出现此错误。 #1

describe('Test Unit - Assync Mode', () => {
  it('Client must be able to login in the app. ', async () => { 
    // pay attention to `async` keyword
    const el = await $('~pressSkip') // note `await` keyword
    await el.click()
    await browser.pause(500)
  })
})

Error Message

#2

beforeEach(() => {
   $('~pressSkip').waitForDisplayed({ timeout: 20000 })
})

describe('My Simple test', () => {
  it('Client must be able to login the app', () => {
    // Click Skip button id: "pressSkip"
    $('~pressSkip').click();
    // Enter Login
      // Email id: "loginEmail"
      // Password id: "loginPwd"
    // Click Login Button id:
  });
});


    =============
Wdio.conf.js
=============
const { join } = require('path');

exports.config = {
//
// ====================
// Runner Configuration
// ====================
//
// WebdriverIO allows it to run your tests in arbitrary locations (e.g. locally or
// on a remote machine).
runner: 'local',
//
// ==================
// Specify Test Files
// ==================
   
specs: [
    './test/specs/**/*.js'],
// ============
// Capabilities
// ============
//
capabilities: [{
// http://appium.io/docs/en/writing-running-appium/caps/
// This is `appium:` for all Appium Capabilities which can be found here
'appium:platformName': 'Android',
'appium:deviceName': 'emulator-5554',
'appium:platformVersion': '8.1.0',
'appium:newCommandTimeout': '60',         
'appium:app': join(process.cwd(), '/android/app/build/outputs/apk/debug/app-debug.apk'),
}],
//
// If you only want to run your tests until a specific amount of tests have failed use
// bail (default is 0 - don't bail, run all tests).
    bail: 0,
//
// Set a base URL in order to shorten url command calls. If your `url` parameter starts
// with `/`, the base url gets prepended, not including the path portion of your baseUrl.
// If your `url` parameter starts without a scheme or `/` (like `some/path`), the base url
// gets prepended directly.
    baseUrl: 'http://localhost:/wd/hub',
//
// Default timeout for all waitFor* commands.
    waitforTimeout: 10000,
//
// Default timeout in milliseconds for request
// if browser driver or grid doesn't send response
    connectionRetryTimeout: 120000,
//
// Default request retries count
    connectionRetryCount: 3,
//
// Test runner services
// Services take over a specific job you don't want to take care of. They enhance
// your test setup with almost no effort. Unlike plugins, they don't add new
// commands. Instead, they hook themselves up into the test process.
    services: [['appium',{
    // This will use the globally installed version of Appium
    command: 'appium',
    args: {
    basePath: "/wd/hub",
// This is needed to tell Appium that we can execute local ADB commands
// and to automatically download the latest version of ChromeDriver
    relaxedSecurity: true,}
}]],
port: 4723,
hostname: "localhost",
// Make sure you have the wdio adapter package for the specific framework installed
// before running any tests.
framework: 'jasmine',
//
// Test reporter for stdout.
// The only one supported by default is 'dot'
// see also: https://webdriver.io/docs/dot-reporter
    reporters: ['spec'],
//
// Options to be passed to Jasmine.
    jasmineOpts: {
// Jasmine default timeout
   defaultTimeoutInterval: 60000,
//
// The Jasmine framework allows interception of each assertion in order to log the state of the application
// or website depending on the result. For example, it is pretty handy to take a screenshot every time
},
}

【问题讨论】:

  • 你好,邦格!欢迎来到 Stackoverflow!你确定该元素存在于 dom 中并且选择器没有问题吗?
  • 嗨拉朱 - 感谢您的回复。当我要使用 Appium 的元素检查器(桌面)使用/检查它时,我可以验证我的可访问性 ID。我相信这些组件被正确渲染了。

标签: appium webdriver-io


【解决方案1】:
describe('Test Unit - Assync Mode', () => {
  it('Client must be able to login in the app. ', async () => { 
    // pay attention to `async` keyword
    await (await $('~pressSkip')).waitForDisplayed({ timeout: 20000 })
    const el = await $('~pressSkip') // note `await` keyword
    await el.click()
    await browser.pause(500)
  })
})

将 await 也添加到 waitfordisplay 中

【讨论】:

  • 嗨@PDHide,感谢您的回答。太奇怪了,即使我完全按照你的方式做,甚至将超时时间增加到 200,000。还是不太顺利。我想知道这些错误是否是罪魁祸首? #1 INFO @wdio/cli:launcher: 运行 onPrepare hook 2021-06-12T12:28:34.679Z 错误 @wdio/appium-service: Appium 在超时前退出(退出代码:2)#2 错误 @wdio/cli:utils :“onPrepare”钩子中的服务失败错误:Appium 在超时之前退出(退出代码:2)
  • 尝试使用 waitfor 存在而不是可见性
  • 非常感谢。这真的很有帮助。
  • @Nate 你能否创建一个可重现的示例并提出问题 webdriverio git page ,看看
  • @PDHide 感谢您在这里的回复,这促使我做更多的调试和 POC。我意识到这一行: const { default: $ } = require('webdriverio/build/commands/browser/$');被其他 npm install 或其他东西添加到我的文件中,这迫使我使用 browser.$("selector").waitForDisplayed();。最初,我在“$”之前没有“浏览器”。所以在取出那条线之后,它现在可以工作了。现在我需要找出添加该行的原因。或者如何...
猜你喜欢
  • 2019-03-22
  • 1970-01-01
  • 2021-04-19
  • 2019-02-08
  • 1970-01-01
  • 2022-09-29
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多