Cypress.io中的选择器存储位置 [英] Where to store selectors in Cypress.io

查看:64
本文介绍了Cypress.io中的选择器存储位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是赛普拉斯的新手.避免将硬选择器/定位器硬编码到每个规范中的最佳方法是什么.在其他框架中,我们将使用所有选择器创建一个配置文件,并让规范引用它.

I am new to Cypress. What is the best way to avoid hard coding selectors/locators into each spec. In other frameworks we would create a config file with all of the selectors and have the spec reference it.

场景:我可能在多个规格中使用了一个选择器.如果选择器发生更改,我不想在每个规格中都进行更改.我宁愿只在一个位置更改它.

Scenario: I may have a selector being used in multiple specs. If the selector changes I do not want to have to change it in each spec. I would rather change it in one location only.

推荐答案

您不需要做任何魔术.只需将选择器放入文件中,然后导入即可.

You don't need to do any magic. Simply put your selectors into a file, and import it.

// cypress/support/selectors.js

export default {
  mySelector: '.my-selector',
  mySelector2: '.my-selector-2'
};

// cypress/integration/one.spec.js

import selectors from '../support/selectors.js';

describe('test', () => {
  it('test', () => {
    cy.get(selectors.mySelector);
  });
});

这篇关于Cypress.io中的选择器存储位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆