【问题标题】:Cypress.io: How to use Fixtures?Cypress.io:如何使用 Fixtures?
【发布时间】:2020-06-22 12:54:05
【问题描述】:

我实际上使用了以下代码:

const userType = new Map([['Manager', '1'],['Developer', '2' ]]) 
for (const [key, value] of usertypes.entries()) 
{
    it('log in', () => 
    {
        cy.login(key,value)
        
        cy.xpath('path')
        .click()
         cy.xpath('path')                        
        .should('have.value' , key)         
    })
}

要在其他测试中使用用户,我想在名为 users 的夹具中定义用户类型。我如何在这个测试中使用这个夹具? 我的 Json 文件看起来像:

[[
     {"key": "Manager"},
     {"value": "1"}
],

[
    {"key": "Developer"},
    {"value": "2" }
]]

我尝试在 beforeElse 中使用 cy.fixture,但我不需要在每个测试中都使用它,而且它不正确。 如何在我的测试中使用 users.json 的数据?

【问题讨论】:

    标签: testing automated-tests cypress fixtures


    【解决方案1】:

    查看您的测试,测试数据是在it() 之外定义的,因此cy.fixture() 将无法在正在运行的测试之外调用。在这种情况下,我会推荐你​​使用 import,所以:

    1. 与您的用户一起创建一个.js 文件(比如说在fixtures 文件夹中) (别忘了Map 允许使用任何类型的键,因此不需要对象符号):
    export const users =  [
        [
            "Manager", 1
        ],
    
        [
            "Developer", 2
        ]
    ]
    
    
    1. 导入变量并将其作为参数传递给您的地图构造函数:
    import { users } from '../fixtures/your_file.js';
    
    
    const userCred = new Map(users);
       for (const [name, password] of userCred.entries()) {
           it('log in', () => {
               cy.login(name, password)
           })
       }
    

    【讨论】:

    • .js 文件必须在集成文件夹中。否则一切都很好。谢谢你的帮助:)
    猜你喜欢
    • 2017-08-03
    • 2021-06-07
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2020-11-23
    • 1970-01-01
    • 2019-10-02
    相关资源
    最近更新 更多