简体   繁体   English

如何从赛普拉斯的下拉按钮中获得 select 多个复选框项目

[英]How to select multiple checkbox item from a dropdown button in Cypress

Hi folks I am new into cypress大家好,我是赛普拉斯的新手

I have a dropdown checkbox button from where I have to select multiple values at once我有一个下拉复选框按钮,我必须从那里一次到 select 多个值

在这里输入代码

For this I have created a local function in type script as below为此,我在类型脚本中创建了一个本地 function,如下所示

function calling function 来电

selectItems('Item 1','Item 4') 

function definition function 定义

selectItems(value1: any, value2: any){
cy.get('dropdownlocator').click();
cy.get('dropdownlocatorCheckboxItems').contains(value1).click();
cy.get('dropdownlocatorCheckboxItems').contains(value2).click()
}

This is working fine but what I wanted is instead of doing the hard coding for each value I should make it so generic that if I pass single value in param it will work or if I pass more than 2 values it should also work这工作正常但我想要的不是对每个值进行硬编码我应该让它变得如此通用以至于如果我在参数中传递单个值它会起作用或者如果我传递超过 2 个值它也应该起作用

There are two things that you can take a look at:您可以查看两件事:

The arguments object arguments object

This is a hidden object of every function call, and can be used to figure out what arguments are passed in without defining what is passed in这是每个function调用的隐藏object,可以用来计算传入的是什么arguments而不用定义传入的是什么

selectItems() {
  cy.wrap(arguments).each(value => {
    cy.contains('dropdownlocatorCheckboxItems', value).click()
  })
})
...
selectItems(value1)

selectItems(value1, value2)

This may cause trouble with the Typescript compiler, it's an older technique pre-typescript.这可能会导致 Typescript 编译器出现问题,这是一种较旧的预打字技术技术。

There is also Rest parameters还有Rest个参数

selectItems(...theValues) {
  for (const value of theValues) {
    cy.contains('dropdownlocatorCheckboxItems', value).click()
  }
})
...
selectItems(value1)

selectItems(value1, value2)

Changing the parameter type to a string array is naïve.将参数类型更改为字符串数组是幼稚的。

You only shift the problem to the line before the function call您只将问题转移到 function 调用之前的行

const selectItems(values: string[]) = {
 ...
})

const values = [value1, value2]  // might as well just put these in the call params
selectItems(values)

You can change the signature of your function to an array and use .forEach to iterate through the array values.您可以将 function 的签名更改为数组并使用.forEach遍历数组值。

const selectItems(values: string[]) = {
  cy.get('dropdownlocator').click().then(() => {
    // Add the forEach inside a .then to ensure it happens after you click the `dropdownlocator`
    values.forEach((value) => {
      cy.get('dropdownlocatorCheckboxItems').contains(value).click();
    })
  })
}
...
selectItems(['foo', 'bar', 'baz']);
selectItems(['foo']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM