如何解决WebDriverIO测试中页面模块导入错误导致的元素调用异常问题
Let's break down what's going wrong here and fix it—your issue isn't actually about module importing, it's about how you're accessing elements in your page object.
The Root Cause
Looking at your main.pageHW.js, topSignUpBtn is defined as a getter property (using the get keyword), not a method. When you call it with parentheses like mainPage.topSignUpBtn() in your test, you're trying to invoke the Element object returned by the getter as if it were a function. That's why you're getting unexpected output (like a UUID) instead of the element you need.
Solution 1: Use the getter correctly (no parentheses)
Modify your test code to access the getter property without calling it as a function:
describe('final homeTask', () => { it('firstassigment', async () => { let mainPage = require('../page/main.pageHW.js') await browser.url('https://github.com/') // Remove the parentheses after topSignUpBtn let signUpButton = await mainPage.topSignUpBtn await signUpButton.click() }) })
Solution 2: Convert the getter to a method (if you prefer parentheses)
If you want to keep using parentheses in your test code, adjust the page object to define topSignUpBtn as a regular method instead of a getter:
class MainPage { // Remove the `get` keyword and add parentheses to make it a method topSignUpBtn() { return $('a.btn-mktg:nth-child(1)') } } module.exports = new MainPage()
Now your original test code with mainPage.topSignUpBtn() will work as expected.
Why Direct Element Access Works
When you directly use await $('a.btn-mktg:nth-child(1)') in your test, you're correctly fetching the element without any incorrect function calls. The confusion came from mixing up getter properties and methods in your page object.
内容的提问来源于stack exchange,提问作者Krasivyi_User9123

