赛普拉斯没有获得必需的选择器

时间:2021-05-26 17:23:22

标签: javascript typescript cypress

我正在尝试在 Cypress 中设置一个函数来获取选择器,但它不起作用

class carPage {
model = 'model'
getModel = () => this.get(this.model)
get = (key) => cy.get(`cy-data-${key}`); }

这应该在屏幕上抓取具有 cy-data-model 的选择器。

在我的测试中:

import car from "../../helpers/carPage";

describe('Tests', () => {

beforeEach(() => {
    cy.visit('/');
    }        
);

it("should get car text", function () {

    car.getModel(); 
 }

但我收到错误属性 'getModel' 在类型 'typeof carPage'.ts(2339) 上不存在

如果我创建静态方法它会起作用,但我不明白为什么上面的方法不起作用?

1 个答案:

答案 0 :(得分:0)

您的代码如下所示吗?

carPage.ts

class carPage {
    model = 'model'
    getModel = () => this.get(this.model)
    get = (key) => cy.get(`cy-data-${key}`); 
}

export default carPage;

测试文件:

import car from "../../helpers/carPage";
...
car.getModel(); 

如果是这样,则测试文件中的变量 car 引用类 carPage。 要使用实例字段,您必须先创建类的新实例

import CarPage from "../../helpers/carPage";
...
const car = new CarPage();
car.getModel();