我试图在我的测试中添加 else if/switch case ,但是 else if - 它只进入 if case,如果'if'失败它不会进入 else if 它也发生在开关盒中。 module.exports.selectEnviroment = function(env) {
switch (env) {
case 'alpha':
cy.get('[role="presentation"]')
.find('[href="#/company-detail/5bb3765e64f66ca0027e15245"]')
.click();
break;
case 'beta':
cy.get('[role="presentation"]')
.find('[ng-href="#/company-detail/5bb62c019ee36000273a6e2b"]')
.eq(0)
.click();
break;
it('Booking should be done using invoice', () => {
cy.visit(`${blah_URL}#/xyz/`);
let env = blah.split('.')[1];
selectEnviroment(env);
根据环境,应该选择大小写,但是没有
if (
cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5bb62c019ee36000273a6e2b"]') ) {
cy.get('[role="presentation"]')
.find('[ng-href="#/company-detail/5bb62c019ee36000273a6e2b"]')
.eq(0)
.click();
} //alpha
else if (cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5bae05a39af4a90027fcdf43"]')) {
cy.get('[role="presentation"]')
.find('[ng-href="#/company-detail/5bae05a39af4a90027fcdf43"]')
.eq(0)
.click();
} //QA
else if (cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5b855022323d37000f48bcdc"]')) {
cy.get('[role="presentation"]')
.find('[ng-href="#/company-detail/5b855022323d37000f48bcdc"]')
.eq(0)
.click();
} //Gamma
else if (cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5bb62ccf5cb043002737d929"]')
) {
cy.get('[role="presentation"]')
.find('[ng-href="#/company-detail/5bb62ccf5cb043002737d929"]')
.eq(0)
.click();
}
it('flight booking should be done using new credit card', () => {
cy.visit(`${COCKPIT_URL}#/company-list/`);
selectEnviroment();
最佳答案
您正在使用 Cypress 命令并期望它们立即生成结果。这不是 Cypress 的工作方式。调用 Cypress 函数只是要求 Cypress 将命令添加到其命令列表以最终运行的一种方式。
.then()
就是为这种情况而创建的。它允许您添加一些代码以在链中的前一个命令之后直接运行:
cy.get('.myDiv').then(elem => {
// elem is a jQuery object
console.log(elem.text());
if (elem.text() == 'Some text') {
// do something
else {
// ...
}
}
我强烈建议reading the introduction to Cypress in the docs .它写得很好,易于阅读。 Cypress 与其他测试框架不同,基本了解 Cypress 的工作原理对于编写良好的 Cypress 代码是必要的。
关于javascript - Cypress ,如果其他/切换案例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52665091/