需要运行一个带参数的函数,出现了TypeError错误。

huangapple go评论67阅读模式
英文:

Need to run a function with parameter, getting TypeError

问题

我在Cypress中遵循POM模型,其中我有一个用例需要从JSON文件中选中页面中的复选框。我的文件如下所示:

homePageTest.cy.js

it("some test", () => {
    cy.fixture("jsonValues").then((data) => {
        homePage.populateDataBasedOnJsonValues(data);
    });
});

homepage.js

class HomePage {
    _getcheckBox(question) {
        return cy.get(`div[class$='checkbox-component-${question}'] > label > input`);
    }

    populateDataBasedOnJsonValues(answers) {
        for (let key in answers) {
            const question = this._getcheckBox(answers[key]);
            question.click();
        }
    }
}

export default HomePage;

运行代码时,我得到以下错误:
TypeError
_homePage.default.populateDataBasedOnJsonValues is not a function

所有其他不带参数的方法(例如answers)都正常工作。但是带参数的这个方法不起作用。
我已经尝试将该方法移动到命令文件中,它可以工作,但我不能将其移动到命令文件中,因为它不属于那里。
我不确定我犯了什么错误,尝试在_getcheckBox(answers[key])之前添加this.,但也不起作用。
有人可以帮助吗,我是Cypress和JS的新手。TIA



<details>
<summary>英文:</summary>

I am following POM model in cypress where I have a use case to tick checkboxes in page from json file. My files are as below:
homePageTest.cy.js

it("some test", () => {
cy.fixture("jsonValues").then((data) => {
homePage.populateDataBasedOnJsonValues(data)
});
});


homepage.js

class HomePage{

_getcheckBox(question) {
this.get("div[class$='checkbox-component-" + question + "'] > label > input");
}

populateDataBasedOnJsonValues(answers) {
for (let key in answers) {
const question = _getcheckBox(answers[key]);
question.click()
}
}
}
export default HomePage;





On running the code I am getting following error:
TypeError
`_homePage.default.populateDataBasedOnJsonValues is not a function`

All other methods without parameter (e.g answers) are working fine. But this one with parameter is not working well. 
I have tried to move the method in commands file and it is working, but I can&#39;t move it in commands file as that is not where it belongs to.
I am not sure mistake I am making, have tried adding `this.` before `_getcheckBox(answers[key]);` 
but that also doesnt work.
Can someone please help, new to cypress and JS. TIA
`

</details>


# 答案1
**得分**: 3

错误消息表明您没有创建该类的实例,而是直接使用了导入的类定义。

这是要遵循的一般模式,您需要调整路径等。

```js
import HomePage from '../Homepage.js'

const homePage = new HomePage()

it("一些测试", () => {
  cy.fixture("jsonValues").then((data) => {
    homePage.populateDataBasedOnJsonValues(data)
英文:

The error message indicates you are not creating an instance of the class, but are just using the imported class definition directly.

This is the general pattern to follow, you will need to adjust paths etc.

import HomePage from &#39;../Hompage.js&#39;  

const homePage = new HomePage()

it(&quot;some test&quot;, () =&gt; {
  cy.fixture(&quot;jsonValues&quot;).then((data) =&gt; {
    homePage.populateDataBasedOnJsonValues(data)

huangapple
  • 本文由 发表于 2023年6月8日 02:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定