CodeceptJS学习笔记-高级用法02-数据驱动测试

 
 

使用不同的数据执行测试用例

Feature('loginaccount');
 
 
// Define data table inside a test or load from another module
let accounts = new DataTable(['login', 'password']); //
accounts.add(['17600000000', '12345678']); // adding records to a table
accounts.add(['13500000000', '12345678']);
 
// You can skip some data. But add them to report as skipped (just like with usual scenarios)
//没有看出来这个作用是什么
accounts.xadd(['15700000000', '12345678'])
 
Data(accounts).Scenario('test something', (I,current) => {
    //在浏览器打开页面
    I.amOnPage('/user/login')
    I.wait(10)
    //输入用户名密码
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'},current.login)
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:last-child)'},current.password)
    // 点击登录按钮
    I.click('button, html [type="button"]')
    //判断成功登录Account
    I.wait(5)
    I.see('开通SaaS')
    I.seeAttributesOnElements('.antd-pro-views-tenant-index-listTitle button',{type:"button"})
    I.click('Automated_Testing')
    I.amOnPage('/AllType/list')
    I.wait(10)
    var cookie = "abc"
    I.seeCookie('_access_token')
    //截图
});
 
Data(accounts).only.Scenario('test something', (I,current) => {
    //在浏览器打开页面
    I.amOnPage('/user/login')
    I.wait(10)
    //输入用户名密码
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'},current.login)
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:last-child)'},current.password)
    // 点击登录按钮
    I.click('button, html [type="button"]')
    //判断成功登录Account
    I.wait(5)
    I.see('开通SaaS')
    I.seeAttributesOnElements('.antd-pro-views-tenant-index-listTitle button',{type:"button"})
    I.click('Automated_Testing')
    I.amOnPage('/AllType/list')
    I.wait(10)
    var cookie = "abc"
    I.seeCookie('_access_token')
    //截图
});

运行结果

CodeceptJS v2.6.5
Using test root "E:Docoumentcodeceptdemo"
 
loginaccount --
  √ test something | {"login":"17600000000","password":"12345678"} in 33313ms
  √ test something | {"login":"13500000000","password":"12345678"} in 33500ms
  S test something | {"login":"15700000000","password":"12345678"}
  √ test something | {"login":"17600000000","password":"12345678"} in 33538ms
  √ test something | {"login":"13500000000","password":"12345678"} in 32463ms
  S test something | {"login":"15700000000","password":"12345678"}
 
  OK  | 4 passed, 2 skipped   // 2m
筛选出数据集中account.login =="17610772739"的数据,并运行
Feature('loginaccount');
 
// Define data table inside a test or load from another module
let accounts = new DataTable(['login', 'password']); //
accounts.add(['17600000000', '12345678']); // adding records to a table
accounts.add(['13500000000', '12345678']);
accounts.add(['17600000000', '12345678']);
accounts.add(['17600000000', '12345678']);
//筛选出数据集中account.login =="17600000000"的数据
Data(accounts.filter(account => account.login =="17600000000")).Scenario('test something', (I,current) => {
    //在浏览器打开页面
    I.amOnPage('/user/login')
    I.wait(10)
    //输入用户名密码
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'},current.login)
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:last-child)'},current.password)
    // 点击登录按钮
    I.click('button, html [type="button"]')
    //判断成功登录Account
    I.wait(5)
    I.see('开通SaaS')
    I.seeAttributesOnElements('.antd-pro-views-tenant-index-listTitle button',{type:"button"})
    I.click('Automated_Testing')
    I.amOnPage('/AllType/list')
    I.wait(10)
    var cookie = "abc"
    I.seeCookie('_access_token')
    //截图
});

运行结果

CodeceptJS v2.6.5
Using test root "E:Docoumentcodeceptdemo"
 
loginaccount --
  √ test something | {"login":"17600000000","password":"12345678"} in 33422ms
  √ test something | {"login":"17600000000","password":"12345678"} in 33360ms
  √ test something | {"login":"17600000000","password":"12345678"} in 33238ms
 
  OK  | 3 passed   // 2m

数据集也可以使用数组,生成器或函数进行定义

Data(function*() {
    yield { login: '17600000000',password: '12345678'};
    yield { login: '13500000000',password: '12345678'};
  }).Scenario('test something', (I,current) => {
    //在浏览器打开页面
    I.amOnPage('/user/login')
    I.wait(10)
    //输入用户名密码
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'},current.login)
    I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:last-child)'},current.password)
    // 点击登录按钮
    I.click('button, html [type="button"]')
    //判断成功登录Account
    I.wait(5)
    I.see('开通SaaS')
    I.seeAttributesOnElements('.antd-pro-views-tenant-index-listTitle button',{type:"button"})
    I.click('Automated_Testing')
    I.amOnPage('/AllType/list')
    I.wait(10)
    var cookie = "abc"
    I.seeCookie('_access_token')
    //截图
});

运行结果

CodeceptJS v2.6.5
Using test root "E:Docoumentcodeceptdemo"
 
loginaccount --
  √ test something | {"login":"17600000000","password":"12345678"} in 32819ms
  √ test something | {"login":"13500000000","password":"12345678"} in 33566ms
 
  OK  | 2 passed   // 1m
 
原文地址:https://www.cnblogs.com/7047-zfy/p/13231621.html