简体   繁体   English

如何在 Cypress 的所有测试中访问相同的别名

[英]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()

Update1更新1

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'.但我不明白这是如何“让我的代码做后空翻”。


Update2更新2

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.

相关问题 将相同的随机数传递给赛普拉斯的所有测试 - Passing a same random number to all tests in Cypress 如何在大多数 cypress 测试之前运行,但不是全部 - How to run before on most cypress tests but not all cypress - “运行所有”功能破坏了我的测试 - cypress - "run all" functionality ruins my tests 在回调中可访问的同一数据上进行多个单元测试 - Multiple unit tests on the same data accessible in a callback 如何在 AVA 测试中设置别名 - How to set an Alias in AVA tests 如何在所有页面上都可以访问的内容脚本中创建变量 - How to make a variable in content script which is accessible on all the pages 如何通过键盘访问页面中的所有按钮 - How to make all the buttons in a page accessible through keyboard 如何在Javascript中创建一个可供所有用户访问的变量? - How can I make a variable in Javascript that is accessible to all users? 如何根据 cy.route 观察到的 API 响应中的特定值制作 Cypress 别名? - How can I make a Cypress-alias from a specific value in an API-response as observed by cy.route? 如何在单独的 github 存储库中管理赛普拉斯测试 - How to manage Cypress tests in a seperate github repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM