我想做一个测试(柏树)来循环每个输入字段中的输入和类型值,但是我得到了错误。有人能帮我吗?Oops,看来在运行父命令之前,您正在尝试调用一个子命令。
<div class="content__form">
<div class="formcolcontainer">
<div class="formcol">
<div class="formrow">
<input type="text" id="fname" class="forminput">
<label for="fname" class="formlabel"> FName</label>
</div>
<div class="formrow">
<input type="text" id="lname" class="forminput">
<label for="lname" class="formlabel"> LName </label>
</div>
</div>
</div>
submitFormData(fname, lname){
const inputFields = {
Fname: fname,
Lname: lname,
}
cy.get('.formrow')
.find('input')
.then(input =>{
cy.wrap(input).each((field, value) =>{
cy.find(inputFields[`#${field}`]).type(inputFields[`${value}`])
})
})
}发布于 2022-02-20 19:43:38
类似这样的内容可能会奏效,直接使用<label>文本来获取输入的值。
//earlier
const fname = 'John'
const lname = 'Jones'
const inputFields = {
Fname: fname,
Lname: lname,
}
cy.get('.formrow')
.find('input')
.each(($input, index) => {
cy.wrap($input).sibling('label').invoke('text').then(label =>
const value = inputFields[label.trim()];
cy.wrap($input).type(value);
})
})发布于 2022-02-20 16:52:18
你可以直接做这样的事。您可以创建一个具有名称和姓氏的数组,并使用它们的索引位置在type中直接使用它们。
let inputFields = ['fname', 'lname']
cy.get('.formrow').find('input').each(($ele, index) => {
cy.wrap($ele).type(inputFields[index])
})发布于 2022-02-20 19:25:39
您可以这样做,使用inputFields来驱动测试
const inputFields = {
Fname: { id: 'fname', value: 'Fred' },
Lname: { id: 'lname', value: 'Smith'},
}
cy.get('.formrow').each(($row, index) => {
cy.wrap($row).find('label').then($label =>
const inputSelector = `#${inputFields[label.text()].id}`;
const value = inputFields[label.text()].value;
cy.get(inputSelector).type(value);
})https://stackoverflow.com/questions/71195778
复制相似问题