简体   繁体   English

赛普拉斯有条件退出跑步/测试

[英]Cypress conditional quit runner/test

So I built out a command that I'm using in multiple tests that looks at the page and if a prompt is there then the page is redirected to another page to handle the prompt (in this case approving a schedule). 所以我构建了一个命令,我在多个测试中使用它来查看页面,如果有提示,那么页面将被重定向到另一个页面以处理提示(在这种情况下批准一个计划)。 Then it additionally looks at that new page and if the page has some text instead then it redirects to the home page (where the issue lies) OR it clicks the button to approve and redirects to the home page and continues normally through the test. 然后它另外查看该新页面,如果页面有一些文本而不是重定向到主页(问题所在)或者它单击按钮批准并重定向到主页并继续正常通过测试。

Cypress.Commands.add('approval_redirect', () => {
  cy.get('body').then(($body) => {
    if ($body.text().includes(
        'You must approve your weekly schedule before starting!'
      )) {
      cy.get('.container a')
        .first()
        .click()
      cy.get('main').then(($main) => {
        if ($main.text().includes('schedule')) {
          cy.get('button')
            .click()
          cy.pause()
        } else {
          cy.get('ul > button')
            .click()
        }
      })
    }
  })
})

Right now if it's going to the new page to verify the schedule and does NOT have a button to click it's returning home and then pausing. 现在,如果它要进入新页面来验证时间表并且没有按钮来点击它返回家然后暂停。 I put in the pause because it would then continue the test with massive failures. 我暂停了,因为它将继续进行大规模失败的测试。

So for example in one test I have: 例如,在一次测试中,我有:

it('starts here', function (){
 cy.login()
   .wait(1000)
 cy.approval_redirect()
 cy.get('#Route')
   .click()
   .wait(1000)
})

So in this if it redirects home after not clicking the button I'd like for it to completely stop the test. 因此,如果它没有点击我想要它完全停止测试的按钮重定向回家。 There's nothing to actually do. 实际上没什么可做的。

Is there a way to completely stop the runner? 有没有办法完全阻止跑步者? How do I put that in my test to check against the command for failure? 如何将其放入我的测试中以检查命令是否失败?

I thought I could just wrap a function around the command with something like: 我以为我可以用命令包围一个函数:

function findFailure(failure,success){cy.get...}

Then instead of cy.pause() I put 然后我把而不是cy.pause()

failure();
return;

And under the ul > button I put 在我放的ul>按钮下

success();
return;

Then in my test I did: 然后在我的测试中我做了:

it('starts here', function (){
 cy.login()
   .wait(1000)
 cy.approval_redirect()
 const failure = function(){
  this.skip()
 }
 const success = function(){
  cy.get('#Route')
    .click()
    .wait(1000)
 }
})

There are no errors and the test runs but it doesn't actually go through the command now. 没有错误,测试运行但现在实际上并没有通过命令。 So how do I conditionally stop the cypress test? 那么如何有条件地停止柏树测试呢?

This one is interesting. 这个很有意思。 I commend your resourcefulness, especially since Cypress has strong feelings when it comes to conditional testing due to its asynchronous nature. 我赞赏你的足智多谋,特别是因为赛普拉斯因为它的异步性而在条件测试方面有强烈的感情。

I had a similar problem recently. 我最近遇到了类似的问题。 For me, nothing on a page was clickable at a certain point if there was network activity, and a toast would appear saying "Loading...", and nothing could be done until network activity was done and the toast disappeared. 对我来说,如果有网络活动,页面上的任何内容都没有可点击,并且吐司会显示“正在加载...”,在网络活动完成并且吐司消失之前无法完成任何操作。 Basically, I needed the page to wait as long as it needed for the toast to disappear before continuing (thus conditional testing). 基本上,我需要页面等待,只要它需要让干杯在继续之前消失(因此有条件的测试)。

What I did was something like this: In commands.js : 我做的是这样的:在commands.js

Cypress.Commands.add('waitForToast', () => {
  cy.get('toast-loading', {timeout: 20000}).should('not.exist');
});

Then in the actual test.spec.js , using the command: 然后在实际的test.spec.js ,使用以下命令:

cy.waitForToast();

What's happening here is Cypress is pausing the test and looking for the toast, and waiting with a timeout of 20000 milliseconds. 这里发生的事情是赛普拉斯暂停测试并寻找吐司,等待超时20000毫秒。 If the toast gets stuck, the test will exit/fail. 如果吐司卡住,测试将退出/失败。 If the toast disappears, the test will continue. 如果吐司消失,测试将继续。

Using asserts so far has been the most effective way to do conditional testing. 到目前为止,使用断言是进行条件测试的最有效方法。 If you're waiting for a button to appear, use an assert with a longer timeout to wait for it. 如果您正在等待按钮出现,请使用具有更长超时的断言等待它。 Then, if it doesn't show up, the test will exit and fail. 然后,如果它没有显示,测试将退出并失败。

In your case, you can assert if you didn't go home. 在您的情况下,如果您不回家,可以断言。

Alternatively 另外

If you want the test to just end without asserting and failing, you can try something like this: 如果您希望测试在没有断言和失败的情况下结束,您可以尝试这样的事情:

cy.get('home-page-item`)
   .its('length')
   .then(numberOfItems => {
      if (numberOfItems > 0) {
      DO-NOTHING-ASSERT
      } else {
      CONTINUE-TEST
      }
   });

Otherwise I don't think Cypress has anything like cy.abortTest() , so you'll have to work around their anti-conditional testing methodology with asserts or if else. 否则我不认为赛普拉斯有像cy.abortTest()这样的东西,所以你必须使用断言或其他方法解决他们的反条件测试方法。

After a bit I noticed an issue with my conditional as it was looking for if a prompt was there then go to the schedule; 稍后我注意到我的条件有问题,因为它正在寻找是否有提示,然后进入时间表; then if certain text wasn't there then go home (where the pause/desire to abort lived) OR click on the button, go home and finish. 然后,如果某些文字不存在,那么回家(暂停/渴望中止的地方)或点击按钮,回家完成。 I noticed that I didn't put in another else on the first condition. 我注意到在第一个条件下我没有放入其他的。

My solution was to just create a function that contained all the steps for a complete test barring no redirects/redirect that had a button click. 我的解决方案是创建一个功能,其中包含完整测试的所有步骤,禁止没有重定向/重定向按钮单击。 Then I put in the failures a console log about what happened to end the test. 然后我在控制台日志中输入了关于结束测试的结果。 Something like: 就像是:

cy.log('Schedule not approved, redirected, no published schedule. Test ended.')

This gives a clean end/break to the test. 这为测试提供了干净的结束/休息。 Albeit wrapping a success/failure would be wonderful. 虽然包装成功/失败将是美好的。

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

相关问题 在 cypress 测试运行程序中找不到文件 - No files found in cypress test runner 远程运行 Cypress Test Runner - Running the Cypress Test Runner Remotely 赛普拉斯测试运行程序在重定向时退出 - Cypress Test runner getting exit while redirect 在远程开发服务器上使用 Cypress Test Runner - Using Cypress Test Runner on Remote Development Server Cypress - Chrome 扩展在 Cypress Test Runner iFrame 中不起作用 - Cypress - Chrome Extension not working inside Cypress Test Runner iFrame 如何从赛普拉斯测试运行程序和控制台日志中禁用阻塞主机 URL? - How to disable blockedHosts urls from Cypress test runner and console logs? 从命令行运行时,赛普拉斯测试失败,但如果从赛普拉斯测试运行器运行,则通过 - Cypress test get failed while running from command line, but passing if running from cypress test runner Cypress 条件测试,else 语句未执行,测试失败 - Cypress Conditional Test, else statement is not executed, fails test 赛普拉斯:test runner一旦测试失败就停止,即使中途测试失败,我们还能继续测试吗? - Cypress:The test runner stops as soon as there is a failure in test, can we continue testing even when a test fails in middle 赛普拉斯测试运行程序通过在 circleCI 中带有信号 SIGSEGV 的退出事件意外退出 - Cypress Test Runner unexpectedly exited via a exit event with signal SIGSEGV in circleCI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM