.as()
语法:
1 | .as(aliasName) |
使用:
1 2 3 4 | cy.get('.main-nav').find('li').first().as('firstNav') // Alias first 'li' as @firstNav cy.route('PUT', 'users', 'fx:user').as('putUser') // Alias that route as @putUser cy.stub(api, 'onUnauth').as('unauth') // Alias that stub as @unauth cy.spy(win, 'fetch').as('winFetch') // Alias that spy as @winFetch |
给DOM元素添加别名,然后使用ci .get()访问别名元素
1 2 3 4 | it('disables on click', () => { cy.get('button[type=submit]').as('submitBtn') cy.get('@submitBtn').click().should('be.disabled') }) |
将路由别名化,然后使用ci .wait()来等待别名化的路由
1 2 3 4 | cy.route('PUT', 'users', 'fx:user').as('userPut') cy.get('form').submit() cy.wait('@userPut') .its('url').should('contain', 'users') |
将ci .fixture()数据别名化,然后使用该数据通过别名访问它。
1 2 3 4 5 6 7 | beforeEach(() => { cy.fixture('users-admins.json').as('admins') }) it('the users fixture is bound to this.admins', function () { cy.log(`There are ${this.admins.length} administrators.`) }) |
实例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | describe('A fixture', () => { describe('alias can be accessed', () => { it('via get().', () => { cy.fixture('admin-users.json').as('admins') cy.get('@admins') .then((users) => { cy.log(`There are ${users.length} admins.`) }) }) it('via then().', function () { cy.fixture('admin-users.json').as('admins') cy.visit('/') .then(() => { cy.log(`There are ${this.admins.length} admins.`) }) }) }) describe('aliased in beforeEach()', () => { beforeEach(() => { cy.fixture('admin-users.json').as('admins') }) it('is bound to this.', function () { cy.log(`There are ${this.admins.length} admins.`) }) }) |
别名的几种路线
1 2 3 4 5 | cy.route(/company/, 'fixture:company').as('companyGet') cy.route(/roles/, 'fixture:roles').as('rolesGet') cy.route(/teams/, 'fixture:teams').as('teamsGet') cy.route(/users\/\d+/, 'fixture:user').as('userGet') cy.route('PUT', /^\/users\/\d+/, 'fixture:user').as('userPut') |
更多系列文章:https://blog.csdn.net/qq_33676825