Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Cypress使用正则表达式检索id中具有匹配文本的元素_Angular_Cypress - Fatal编程技术网

Angular Cypress使用正则表达式检索id中具有匹配文本的元素

Angular Cypress使用正则表达式检索id中具有匹配文本的元素,angular,cypress,Angular,Cypress,我有多个div元素,id为features-####和features-##,其中#是一个动态图形 如何让Cypress找到id功能-匹配部分的所有元素,然后以编程方式单击第一个或第二个 这就是我到目前为止所做的,但我不知道如何使用regex检索所有元素,然后以编程方式处理它们 describe('builder features component', () => { it('should allow the user to select a features compone

我有多个div元素,id为
features-####
features-##
,其中#是一个动态图形

如何让Cypress找到id
功能-
匹配部分的所有元素,然后以编程方式单击第一个或第二个

这就是我到目前为止所做的,但我不知道如何使用regex检索所有元素,然后以编程方式处理它们

  describe('builder features component', () => {
    it('should allow the user to select a features component', () => {
      cy.get(el).should('div.id', /^features-\w+/).first().click();
    });
  });
使用contains()函数。该函数可以接收regex作为参数

cy.contains(/^features-\w+/).click();
使用contains()函数。该函数可以接收regex作为参数

cy.contains(/^features-\w+/).click();

Cypress建议使用
数据-
属性来选择元素。例如,如果将
data cy=“builder feature”
添加到所有功能中,您就不必关心正则表达式,因为正则表达式只会降低测试速度

cy.get('[data-cy="builder-feature"]').first().click()

请参阅。

Cypress建议使用
数据-
属性来选择元素。例如,如果将
data cy=“builder feature”
添加到所有功能中,您就不必关心正则表达式,因为正则表达式只会降低测试速度

cy.get('[data-cy="builder-feature"]').first().click()

请看。

Rich Churcher是对的,他提到的方法应该是你的首选。但是,如果您无法控制应用程序,并且无法添加
data-cy
属性,您可以使用css选择器语法来实现这一点

const featuresSelector = '[id^="features-"]'; 
这将选择id以…开头的任何元素。。。看到一个很好的解释了吗

然后您可以像这样使用此选择器:

cy.get(featuresSelector).first().click(); // click the 1st element in the collection
cy.get(featuresSelector).eq(1).click(); // click the second element (index starts at 0)
cy.get(featuresSelector).eq(-1).click(); // click the last element
cy.get(featuresSelector).each((element, index, collection) => {
    // or event loop through the entire collection
})

富有的丘吉尔是对的,他提到的方法应该是你的首选。但是,如果您无法控制应用程序,并且无法添加
data-cy
属性,您可以使用css选择器语法来实现这一点

const featuresSelector = '[id^="features-"]'; 
这将选择id以…开头的任何元素。。。看到一个很好的解释了吗

然后您可以像这样使用此选择器:

cy.get(featuresSelector).first().click(); // click the 1st element in the collection
cy.get(featuresSelector).eq(1).click(); // click the second element (index starts at 0)
cy.get(featuresSelector).eq(-1).click(); // click the last element
cy.get(featuresSelector).each((element, index, collection) => {
    // or event loop through the entire collection
})

我不认为
包含
检查元素
id
属性?我不认为
包含
检查元素
id
属性?