|
在用Selenium、RT或其他web UI自动化工具时,为了保证测试用例的独立性,通常需要做必要setUp和tearDown,比如常见的登录和登出,而用UI来做登录和登出是很耗时的(目前的工具对cookie、localStorage等支持都不够友好),在大量的UI自动化用例运行时,登录和登出耗时比例不可忽视。Cypress基于异步JS实现,几乎可以无限制的操作浏览器存储!真正让你做到“钱(Time)要花在刀刃上”!
自定义登录指令
- Cypress.Commands.add('login', (userType, options = {}) => {
- // this is an example of skipping your UI and logging in programmatically
- // setup some basic types
- // and user properties
- const types = {
- admin: {
- name: 'Jane Lane',
- admin: true,
- },
- user: {
- name: 'Jim Bob',
- admin: false,
- }
- }
- // grab the user
- const user = types[userType]
- // create the user first in the DB
- cy.request({
- url: '/seed/users', // assuming you've exposed a seeds route
- method: 'POST',
- body: user,
- })
- .its('body')
- .then((body) => {
- // assuming the server sends back the user details
- // including a randomly generated password
- //
- // we can now login as this newly created user
- cy.request({
- url: '/login',
- method: 'POST',
- body: {
- email: body.email,
- password: body.password,
- }
- })
- })
- })
复制代码
调用自定义指令
- describe("测试模块名", function() {
- beforeEach(function() {
- cy.login("admin");
- });
- it("测试用例名", function(){
- ....
- // 真正的测试代码
- ....
- })
- })
复制代码 |
|