如何在Cypress中将值传递给:nth-child() CSS选择器

huangapple go评论67阅读模式
英文:

how to pass values to :nth-child() css selector in cypress

问题

cy.get(root-div > div:nth-child(1))

然后如果我要获取第二个元素,我会这样做:

cy.get(root-div > div:nth-child(2))

我们可以使这个 div:nth-child(2) 部分变成动态的,类似这样:

cy.get(root-div > div:nth-child(${0}))

然后在调用此定位器时传递值吗?这样定位器就变成了动态的,可以用于所有子元素。

英文:

I am having a parent Div that has many sub element, while traversing thorough them if I need to select some child element I will do,

<!-- language-all: lang-css -->

cy.get(root-div &gt; div:nth-child(1))

And then I have to get 2nd element I will be doing like

cy.get(root-div &gt; div:nth-child(2))

Can we make this div:nth-child(2) part dynamic something like,

cy.get(root-div &gt; div:nth-child(
cy.get(root-div &gt; div:nth-child(${0}))
))

And then pass the values when we are calling this locator? So that locator becomes dynamic and can be used for all child elements.

答案1

得分: 4

你可以使用.children(selector)命令,并在其选择器内或在其后使用.eq()来指定第n个子元素。

一些选项包括:

// 获取第二个子元素,该子元素是一个div
cy.get('root-div').children('div').eq(1).should(...)

// 同样,使用选择器内的`eq()`
cy.get('root-div').children('div:eq(1)').should(...)

// 遍历所有子元素,带有索引
cy.get('root-div').children((child, index) => {
  cy.wrap(child).should(...

我不建议使用函数或自定义命令,因为这会增加复杂性但不会增加额外的清晰度。

英文:

Since you are dealing with the children elements, you can use the .children(selector) command and specify the nth one either inside it's selector or following it with .eq()

Some options are

// give me 2nd child that is a div
cy.get(&#39;root-div&#39;).children(&#39;div&#39;).eq(1).should(...

// same, using `eq()` inside the selector
cy.get(&#39;root-div&#39;).children(&#39;div:eq(1)&#39;).should(...

// traverse all the children, with index
cy.get(&#39;root-div&#39;).children((child, index) =&gt; {
  cy.wrap(child).should(...

I would not use a function or a custom command as it adds complexity but adds no extra clarity.

答案2

得分: 1

你可以创建一个JavaScript函数来返回该字符串并让它接受一个索引变量

```js
const getNthChild(index) => {
  return `root-div > div:nth-child(${index})`
});
// 在测试中
cy.get(getNthChild(1));

另外,如果你的cy.get()命令返回多个元素,cy.eq()命令可以选择一个索引处的元素。

cy.get('root-div > div').eq(1).should('be.visible')
cy.get('root-div > div').eq(2).should('have.text', 'foo');

或者,你可以创建一个自定义的Cypress命令,尽管我认为这有点过于复杂。

Cypress.Commands.add('getNthChild', (index) => {
  return cy.get(`root-div > div:nth-child(${index})`);
});
// 在测试中
cy.getNthChild(1);
英文:

You could create a JavaScript function for returning that string, and let it take in an indexing variable.

const getNthChild(index) =&gt; {
  return `root-div &gt; div:nth-child(${index})`
});
// In test
cy.get(getNthChild(1));

Additionally, if your cy.get() command yields multiple elements, the cy.eq() command can select an element at an index.

cy.get(&#39;root-div &gt; div&#39;).eq(1).should(&#39;be.visible&#39;)
cy.get(&#39;root-div &gt; div&#39;).eq(2).should(&#39;have.text&#39;, &#39;foo&#39;);

Alternatively, you could create a custom Cypress command, although I think that is a little bit of an overkill.

Cypress.Commands.add(&#39;getNthChild&#39;, (index) =&gt; {
  return cy.get(`root-div &gt; div:nth-child(${index})`);
});
// In test
cy.getNthChild(1);

huangapple
  • 本文由 发表于 2023年7月24日 16:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定