赛普拉斯,无论我尝试什么,如果声明都不起作用
Cypress, If statement not working no matter what i try
我正在尝试在赛普拉斯中做一个简单的测试,看看一个字段是否是只读的,但无论我尝试什么,我都无法让 if 语句工作,我检查了赛普拉斯文档并经历了一大堆不同类型的例子,我通常不会遇到抓住元素并使用它们的问题,但我对此有点困惑,我错过了什么?:
(我已经用 .hasClass 和 .find 尝试过,但仍然没有成功,我也尝试过使用元素的其他属性,但没有任何效果)
这是我要与之交互的字段的 html 代码:
<textarea _ngcontent-nud-c545="" rows="1" cdktextareaautosize=""
aria-label="Product name" matinput="" type="text"
placeholder="'Enter a product name...'" required=""
formcontrolname="productName"
class="cdk-textarea-autosize mat-input-element mat-form-field-autofill-control ng-tns-c98-55 ng-pristine ng-valid cdk-text-field-autofill-monitored ng-touched"
ng-reflect-enabled="" ng-reflect-type="text" ng-reflect-placeholder="'Enter a product name...'" ng-reflect-required="" ng-reflect-name="productName" id="mat-input-3" data-placeholder="'Enter a product name...'" aria-invalid="false" aria-required="true" style="height: 21px;"> </textarea>
这是我的代码,用于获取它并测试它是否包含文本 'productName':
cy.get('[formcontrolname="productName"]').then (($productName) => {
if ($productName.text().includes('productName')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
});
无论我尝试什么,if 语句都会转到 'it hasnt found it'
而不是 text()
你必须使用 .val()
cy.get('[formcontrolname="productName"]').then(($productName) => {
if ($productName.val().includes('Clay brick unit')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
})
我认为您正在尝试确认该元素,如果是这样,您将需要 .attr()
而不是 .text()
cy.get('[formcontrolname="productName"]').then (($productName) => {
if ($productName.attr('formcontrolname').includes('productName')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
});
但实际上,如果 未找到 ,.then()
部分将不会 运行 - 测试将在 cy.get('[formcontrolname="productName"]')
上失败,因为 .get()
命令有一个内置的存在断言。
您在使用 $productName.val()
时遇到的问题是 typescript 抱怨它无法解析返回的类型。
尝试
const val = $productName.val() as string; // specify the returned value type
if (val.includes('Clay brick unit')) {
对我有用的代码如下所示,以前我的 If 语句不起作用,我似乎无法 select 使用页面元素的别名时我想要的文本字段,使用 .val () 函数解决了这个问题。使用 .val() 我可以获取文本字段的值,然后将其与预期值进行比较,在我们的例子中,预期值为 'Clay brick unit'
代码解释:
我使用 'cy.get('[formcontrolname="productName"]')' 来获取我需要的文本字段,然后我创建一个名为 '$productName' 的别名,然后我使用 .val() 函数来 return 来自文本字段的值($productName),然后我使用 if 语句查看我的文本字段中的值是否包含 'Clay brick unit',如果包含则输出 'its found it',如果找不到则输出'It hasnt found it' 到日志
cy.get('[formcontrolname="productName"]').then(($productName) => {
const val = $productName.val() as string; // specify the returned value type
if (val.includes('Clay brick unit')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
});
return this;
}
我正在尝试在赛普拉斯中做一个简单的测试,看看一个字段是否是只读的,但无论我尝试什么,我都无法让 if 语句工作,我检查了赛普拉斯文档并经历了一大堆不同类型的例子,我通常不会遇到抓住元素并使用它们的问题,但我对此有点困惑,我错过了什么?:
(我已经用 .hasClass 和 .find 尝试过,但仍然没有成功,我也尝试过使用元素的其他属性,但没有任何效果)
这是我要与之交互的字段的 html 代码:
<textarea _ngcontent-nud-c545="" rows="1" cdktextareaautosize=""
aria-label="Product name" matinput="" type="text"
placeholder="'Enter a product name...'" required=""
formcontrolname="productName"
class="cdk-textarea-autosize mat-input-element mat-form-field-autofill-control ng-tns-c98-55 ng-pristine ng-valid cdk-text-field-autofill-monitored ng-touched"
ng-reflect-enabled="" ng-reflect-type="text" ng-reflect-placeholder="'Enter a product name...'" ng-reflect-required="" ng-reflect-name="productName" id="mat-input-3" data-placeholder="'Enter a product name...'" aria-invalid="false" aria-required="true" style="height: 21px;"> </textarea>
这是我的代码,用于获取它并测试它是否包含文本 'productName':
cy.get('[formcontrolname="productName"]').then (($productName) => {
if ($productName.text().includes('productName')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
});
无论我尝试什么,if 语句都会转到 'it hasnt found it'
而不是 text()
你必须使用 .val()
cy.get('[formcontrolname="productName"]').then(($productName) => {
if ($productName.val().includes('Clay brick unit')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
})
我认为您正在尝试确认该元素,如果是这样,您将需要 .attr()
而不是 .text()
cy.get('[formcontrolname="productName"]').then (($productName) => {
if ($productName.attr('formcontrolname').includes('productName')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
});
但实际上,如果 未找到 ,.then()
部分将不会 运行 - 测试将在 cy.get('[formcontrolname="productName"]')
上失败,因为 .get()
命令有一个内置的存在断言。
您在使用 $productName.val()
时遇到的问题是 typescript 抱怨它无法解析返回的类型。
尝试
const val = $productName.val() as string; // specify the returned value type
if (val.includes('Clay brick unit')) {
对我有用的代码如下所示,以前我的 If 语句不起作用,我似乎无法 select 使用页面元素的别名时我想要的文本字段,使用 .val () 函数解决了这个问题。使用 .val() 我可以获取文本字段的值,然后将其与预期值进行比较,在我们的例子中,预期值为 'Clay brick unit'
代码解释: 我使用 'cy.get('[formcontrolname="productName"]')' 来获取我需要的文本字段,然后我创建一个名为 '$productName' 的别名,然后我使用 .val() 函数来 return 来自文本字段的值($productName),然后我使用 if 语句查看我的文本字段中的值是否包含 'Clay brick unit',如果包含则输出 'its found it',如果找不到则输出'It hasnt found it' 到日志
cy.get('[formcontrolname="productName"]').then(($productName) => {
const val = $productName.val() as string; // specify the returned value type
if (val.includes('Clay brick unit')) {
cy.log('its found it');
} else {
cy.log('it hasnt found it');
}
});
return this;
}