您好,我正在尝试通过以下页面对象模型在 cypress 中运行脚本。但是当我运行脚本时,cypress 抛出错误,页面的 _HomePage.HomePage 不是构造函数
我在下面附上我的代码以供参考。
在主页对象中
class HomePage
{
myaccountIcon(){
return cy.get('a[title=Account] div.icon-wrapper.top-action-icons.quick-menu');
}
viewCartICon(){
return cy.get('a[title=Cart]');
}
shortListIcon(){
return cy.get('a[title=Shortlist]');
}
hamburgerIcon(){
return cy.get('div.gc-icon-col.gc-icon-closed img[class=img-responsive]');
}
editNameField()
{
return cy.get('form > div:nth-child(1) > input');
}
editEmailField()
{
return cy.get('form > div:nth-child(2) > input');
}
editPasswordField()
{
return cy.get('form > div:nth-child(3) > input');
}
gettwoWayDataBinding()
{
}
selctDropdown()
{
return cy.get("select");
}
gnavShop_option()
{
return cy.get('ul.navbar-nav li:nth-child(2)');
}
sddCTA()
{
return cy.get('a.tile-8.sdd');
}
clickPersonalizedGnavSection(){
return cy.get('#selection-panel > div > a.tile-8.personalized').click();
}
}
export default HomePage;
在我的脚本中
///<reference types ="cypress"/>
import { HomePage } from '../support/pageObjects/HomePage';
import { MppPage } from '../support/pageObjects/OrderReviewPage';
describe("Testframwork", () => {
let data1;
before(function () {
cy.log("Loading the fixure file")
cy.fixture('example.json').then(function (data) {
data1 = data;
})
})
it("Checkout scenario", () => {
const homePage = new HomePage();
const mp = new MppPage();
cy.visit('https://google.com');
homePage.editNameField().type(data1.name);
homePage.editEmailField().type(data1.email);
homePage.editPasswordField().type(data1.password);
homePage.selctDropdown().select(data1.gender);
homePage.gnavShop_option().click();
cy.get('.card-body h4').each(($el, index, list) => {
if ($el.text().includes('Blackberry')) {
cy.get('div.card-footer button').eq(index).click();
}
})
mp.getContinueCheckout().click();
})
})
在我的package.json文件中
{
"name": "cypressautomation",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "satyajit",
"license": "ISC",
"devDependencies": {
"@types/cypress-cucumber-preprocessor": "^4.0.1",
"cypress": "^10.6.0",
"cypress-cucumber-preprocessor": "^4.3.1",
"mochawesome": "^7.1.3",
"multiple-cucumber-html-reporter": "^1.21.6"
},
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true,
"cucumberJson": {
"generate": true,
"outputFolder": "cypress/cucumber-json",
"filePrefix": "",
"fileSuffix": ".cucumber"
}
}
}
你可以移动你的构造函数吗:
const homePage = new HomePage();
const mp = new MppPage();
在你的describe()块之外。含义将它们放在您的导入下。这样它们就成为全局的,并且可以被你的describe()块、before()块、after()块和it()块使用;这也将允许其他 it() 块访问:homePage 和 mp。
Cypress 告诉您,由于它具有 ASYNC 性质,因此它在部署之前会读取大量代码,并且会失败,因为它看不到您的构造函数(主页和 mp)。