[英]How to make the same alias accessible in all tests in Cypress
I'm trying to make the same alias, that is accessible in severel different tests.我正在尝试制作相同的别名,可以在严格不同的测试中访问。 And the documentation doesn't explain how to achieve this.
并且文档没有解释如何实现这一点。
I'm trying to do this:我正在尝试这样做:
Before
- Generate datetime-stamp as a string: $dateTime
Test 1
- Test that no order exists where $dateTime is the first name for the buyer.
Test 2
- Make an order, setting $dateTime as the first name
Test 3
- Test that there is an order where $dateTime is the first name
How do I, in the before()
-function make a variable that is accessible in all my tests?我如何在
before()
函数中创建一个可以在所有测试中访问的变量? ... And please note, that since the $dateTime
should be the same in all tests, then it should be generated once and not in beforeEach()
. ...请注意,由于
$dateTime
在所有测试中都应该相同,所以它应该生成一次,而不是在beforeEach()
。
I can see that it is actually described in the "Do not do this"-section here .我可以看到它实际上在此处的“请勿执行此操作”部分中进行了描述。 But I don't see how this is 'making my code do backflips'.
但我不明白这是如何“让我的代码做后空翻”。
And are there an advantage of using fixtures, if I do this anyway:如果我仍然这样做,是否有使用装置的优势:
context( 'Buy product' , function () {
let dateTime = new Date();
let dateTimeStr = dateTime.toLocaleDateString('en-GB', { year: 'numeric', day: 'numeric', month: 'short', hour: 'numeric', minute: 'numeric', second: 'numeric' });
dateTimeStr = dateTimeStr.replaceAll( ', ', '--' );
dateTimeStr = dateTimeStr.replaceAll( ' ', '-' );
dateTimeStr = dateTimeStr.replaceAll( ':', '' );
beforeEach( () => {
cy.wrap( dateTimeStr ).as('dateTimeStr');
});
it( 'Order with DateTime DOES NOT exists', () => {
cy.visit( 'https://example.org/dashboard/orders' );
cy.get( "@dateTimeStr" )
.then( (dateTimeStr) => {
cy.contains( dateTimeStr ).should('not.exist');
});
});
it( 'Buy product', function() {
cy.visit( 'https://example.org/product/test' );
...
cy.get( '#first_name' ).type( this.dateTimeStr );
...
cy.get('#submit_order').click();
});
it( 'Order with DateTime exists', () => {
cy.visit( 'https://example.org/dashboard/orders' );
cy.get( "@dateTimeStr" )
.then( (dateTimeStr) => {
cy.contains( dateTimeStr ).should('exist');
});
});
});
This code works.此代码有效。 But is explicitly mentioned in the documention, that I shouldn't do this.
但是在文档中明确提到,我不应该这样做。 However... The documention doesn't really explain why.
但是......文档并没有真正解释原因。 Flakyness?
片状? Anti-pattern?
反模式? I don't know.
我不知道。 It seems pretty smart to me.
这对我来说似乎很聪明。
I aggree with the documentation that using an allias that covers all documents is wrong, but the thing you are trying to do is not wrong and I suggest use Custom command我同意文档的说法,即使用涵盖所有文档的别名是错误的,但您尝试做的事情并没有错,我建议使用自定义命令
Cypress.Commands.add('getDateTimeStr', () => {
let dateTime = new Date();
let dateTimeStr = dateTime.toLocaleDateString('en-GB', { year: 'numeric', day: 'numeric', month: 'short', hour: 'numeric', minute: 'numeric', second: 'numeric' });
dateTimeStr = dateTimeStr.replaceAll( ', ', '--' );
dateTimeStr = dateTimeStr.replaceAll( ' ', '-' );
dateTimeStr = dateTimeStr.replaceAll( ':', '' );
cy.wrap(dateTimeStr).as('dateTimeStr')
})
And use it in the beforeEach并在 beforeEach 中使用它
context( 'Buy product' , function () {
beforeEach( () => {
cy.getDateTimeStr()
});
it( 'Order with DateTime DOES NOT exists', () => {
cy.visit( 'https://example.org/dashboard/orders' );
cy.get( "@dateTimeStr" )
.then( (dateTimeStr) => {
cy.contains( dateTimeStr ).should('not.exist');
});
});
it( 'Buy product', function() {
cy.visit( 'https://example.org/product/test' );
...
cy.get( '#first_name' ).type( this.dateTimeStr );
...
cy.get('#submit_order').click();
});
it( 'Order with DateTime exists', () => {
cy.visit( 'https://example.org/dashboard/orders' );
cy.get( "@dateTimeStr" )
.then( (dateTimeStr) => {
cy.contains( dateTimeStr ).should('exist');
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.