【问题标题】:Cypress - Use Custom Command based on value赛普拉斯 - 根据值使用自定义命令
【发布时间】:2019-12-20 19:50:42
【问题描述】:

我需要编写一个自定义命令来根据通过 spec.ts 文件提供的元素值执行某些步骤。我无法弄清楚如何将值 A 和 B 传递给自定义命令,如何根据 A 或 B 存储定位器值,然后使用 then 命令打印值

Spec.ts 文件

cy.writetemp(A)
cy.writetemp(B)

自定义命令

cypress.command.add(writetemp,(A) => {
cy.get("select[name='Type']").should('have.value', 'A'), then print Apple
cy.get("select[name='Type']").should('have.value', 'B'), then print Banana

【问题讨论】:

    标签: cypress


    【解决方案1】:

    我不确定你到底想要实现什么。

    但是您可以像这样定义自定义命令:

     Cypress.Commands.add("writeTemp", (param) => {
            if (param === "body") {
                cy.get(param).should('exist')
            } else if (param === "B") {
                cy.get("asdasd").should("contain", param);
            }
        })
    

    参数可以任意传递。这里我只指定了一个(“param”)。

    用法:

    describe("abc", () => {
        it("get body and succeed", () => {
            cy.visit("index.html");
            cy.writeTemp("body")
        });
    
        it("get asdasd and fail", () => {
            cy.visit("index.html");
            cy.writeTemp("B")
        });
    })
    

    如果我误解了你,请告诉我。

    【讨论】:

      【解决方案2】:

      我想基于定位器构建逻辑,但意识到这是处理它的不好方法。我切换到属性值并且它起作用了。感谢大家的宝贵时间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2020-02-08
        • 2021-08-25
        • 2021-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多