Cypress-JavaScrip基础知识

简单的定义变量和条件控制与函数

// 定义变量,定义变量有三种方式这里简单的举栗
let a = "admin";
console.log(a);

// if条件控制;这里只有if else 并没有else if
if (a == "hello world") {
    console.log(a);
}
else {
    console.log("not hello world")
}


// 函数
function print(data){
    console.log("data is:", data)
}

// 调用
print("you are the best!")

 

 

 

普通函数与箭头函数使用

// 普通函数
function add(a,b) {
    return a + b;
}

let ret = add(3, 4);
console.log(ret);


// 箭头函数
func = (a, b) => {
    return a + b;
}

console.log(func(4,5))

 

 

回调函数

// callback为函数
function print(string, callback) {
    console.log(string)
    console.log("after log")
    callback()
}

// 调用函数
print("a", function () {
    // 在函数中再次调用print函数
    print("b", function() {
        print("c", function(){ console.log("finished")})
    })
})

// 箭头函数调用函数
// 调用函数
print("d",  () => {
    // 在函数中再次调用print函数
    print("e", () => {
        print("f", () => { console.log("finished")})
    })
})

 

 

 

JavaScript的测试和断言

# mocha 测试;chai 断言;cypress内部集成了这两个库,如下展示这两个库进行测试的用法
安装:
npm install mocha chai

// 使用mocha和chai库,对demo_04.js文件进行测试;运行测试命令:npx mocha demo_04.js

// 导入断言
var should = require('chai').should()

// 编写测试用例;describe:相当于测试套件
describe('test a variable', () =>{
    let a = 'tes'

    // 编写测试用例
    it('a should be a string', () => {
        // 进行断言:a 为 string类型
        a.should.be.a('string')
    })

    it('a should equal to tes', () => {
        // 进行断言:a 等于 tes
        a.should.equal('tes')
    })
})

 

posted @ 2022-02-23 20:09  1142783691  阅读(130)  评论(0编辑  收藏  举报