Cypress数据驱动测试
1.数据在代码内部,直接定义后.forEach方式,执行时会对每个数据执行一次测试语句。
describe('test ', function() { var testdatas=['username1','username2'] testdatas.forEach((username)=>{it('login test',function(){ cy.loginByUserName(username,'1234567') cy.url().should('contain','Home')}) }) })
2.外部数据文件导入测试.cy.fixturn只能在case里运行。
例如外部有json文件,里边数据为["usera", "userb"]
a) .fixture(filepath).then(()=>{使用数据})
describe('test ', function() { it('login test',function(){ cy.fixture('LoginUser').then((username)=>{ for(var i=0;i<username.length;i++){ cy.loginByUserName(username[i],'password') cy.wait(2000) cy.url().should('contain','Home')} }) }) })
b).fixture(filepsath).as('aliasname')设置文件内容为变量,后边用this.aliasname调用变量
describe('test ', function() { beforeEach(function(){ cy.fixture('LoginUser').as('usernames') }) context('context',function(){ it('login test',function(){ cy.loginByUserName(this.usernames[0],'password') cy.wait(2000) cy.url().should('contain','Home')}) }) }
3使用import导入外部数据
import testData from '../fixtures/login.json' describe('Test ', function() { testData.forEach((username)=> {it('Login by Users '+username,function(){ cy.loginByUserName(username,'password') cy.url().should('contain','Home') cy.clearCookies() cy.clearLocalStorage() }) })
})