CodeceptJS学习笔记-高级用法04-设置自动登录

 

1.在codecept.conf.js中的plugins下加入以下代码

autoLogin: {

      enabled: true,
      saveToFile: true,
      inject: 'loginAs', // use `loginAs` instead of login
      users: {
        user: {
          login: async (I) => {
             I.amOnPage('/user/login');
             I.wait({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'})
             I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'},'13520661550');
             I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:last-child)'},'@12345678');
             I.say("111");
             // 点击登录按钮
             I.click('button, html [type="button"]');
             I.wait(5)
             I.see('开通SaaS')
             I.seeAttributesOnElements('.antd-pro-views-tenant-index-listTitle button',{type:"button"})
             I.click('Automated_Testing')
             I.wait(5)
          },
          check: (I) => {
            I.amOnPage('/dashboard')
          },
        },
        admin: {
          login: async (I) => {
             I.amOnPage('/user/login');
             I.say("222");
             I.wait(10)
             I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:first-child)'},'17610772739');
             I.fillField({css:'.ant-input-affix-wrapper .ant-input:not(:last-child)'},'@12345678');
             I.say("222");
             // 点击登录按钮
             I.click('button, html [type="button"]');
             I.wait(5)
             I.see('开通SaaS')
             I.seeAttributesOnElements('.antd-pro-views-tenant-index-listTitle button',{type:"button"})
             I.click('Automated_Testing')
             I.wait(5)
          },
          check: (I) => {
            I.amOnPage('/dashboard')
          },
        },
      }
    }

调用

Scenario('log me in', async(I, loginAs) => {
  await loginAs('user');
  I.amOnPage('/dashboard')
  I.wait(10)
  I.see('Dashboard')
});

运行结果

CodeceptJS v2.6.5
Using test root "E:Docoumentcodeceptdemo"
 
loginaccount --
  log me in
    Object: login
      我 在页面 "/user/login"
      我 等 10
      我 填写字段 {"css":".ant-input-affix-wrapper .ant-input:not(:first-child)"}, "13500000000"
      我 填写字段 {"css":".ant-input-affix-wrapper .ant-input:not(:last-child)"}, "12345678"
   111
      我 单击 "button, html [type="button"]"
      我 等 10
      我 看到 "开通SaaS"
      我 see attributes on elements ".antd-pro-views-tenant-index-listTitle button", {"type":"button"}
      我 单击 "Automated_Testing"
      我 等 10
    我 抓取 cookie
    我 在页面 "/dashboard"
    我 等 10
    我 看到 "Dashboard"
  √ OK in 46407ms
 
 
  OK  | 1 passed   // 49s

再次运行,就没有了登录过程,直接使用cookie登录

CodeceptJS v2.6.5
Using test root "E:Docoumentcodeceptdemo"
 
loginaccount --
  log me in
    我 在页面 "/"
    我 设置 cookie [{"name":"_tenant_avatar","value":"","domain":"*******.com","path":"/","expires":53847095461,"size":14,"httpOnly":false,"secure":false,"session":false},{"name":"_access_token","value":"ASDfArbI6V54...
    我 在页面 "/dashboard"
    我 在页面 "/dashboard"
    我 等 10
    我 看到 "Dashboard"
  √ OK in 17594ms
 
 
  OK  | 1 passed   // 20s
原文地址:https://www.cnblogs.com/7047-zfy/p/13232231.html