代码之家  ›  专栏  ›  技术社区  ›  soccerway

我们将如何调用一个在单独文件中编写的函数?

  •  1
  • soccerway  · 技术社区  · 6 年前

    describe ('Calling a function', function(){
    it('Call the Subtract function and asert the calculation', function(){
        cy
        .wrap({sub: subValues})
        .invoke('sub', 15, 8)
        .should('eq', 7) // true        
        })
    
    })
    

    //减法函数:

    const subValues = (a, b) => {
      return a - b 
    }
    
    2 回复  |  直到 6 年前
        1
  •  6
  •   Brendan    6 年前

    https://docs.cypress.io/api/cypress-api/custom-commands.html

    把这个放在你的 support/commands.js 文件:

    Cypress.Commands.add('subValues', (a, b) => { return a - b });
    

    support/index.js 文件,如果它不在那里(应该在那里):

    import "./commands";
    

    在你的测试中这样称呼它:

    describe ('Calling a function', function(){
      it('Call the Subtract function and asert the calculation', function(){
        cy
          .subValues(15, 8)
          .should('eq', 7) // true        
        });
    });
    
        2
  •  1
  •   Joshua Wade Abel    6 年前

    Cypress包含一个外部库文件,默认位于 (path to project)/support/index.js .


    您还可以在 support index.js :

    import './MySupportFile.js'
    

    cy 对象对我有效:

    cy.myFunction = greeting => {
        console.log(greeting);
    };
    
    cy.myNamespace = {};
    
    cy.myNamespace.myNamespacedFunction = () => {
        console.log("hi");
    };
    

    这些功能在 物体将携带到任何 /integration