YOU54F / cypress-plugins

A home for various Cypress Plugins

Home Page:https://cypress-plugins.saf.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Running test suites and channel selection

you1anna opened this issue Β· comments

Hi,
I have a couple of questions about the tool:

  1. When using npx cypress-slack-reporter or index.ts is it possible to run only a subset of tests from package.json? E.g.
    "cy:run:pr": "cypress run --spec 'cypress/integration/PR/*'"

  2. Is there a way to pass in the Slack channel results are posted to?

Thanks,
Robin

Hey,

  1. The running of the tests is seperate to the invocation of the slack reporter.If you run your command cy:run:pr and then npx cypress-slack-reporter, do you not get the desired result?

  2. I believe the webhooks are specific to a channel when created, but stand to be correct. If so, it would be simple to pass a channel value, either name or id. I'd just need to check the slack api

Cheers,
saf

@you1anna what is your use case for posting to a different channel?

Would you want to post to a different channel on failure? If so we could support different webhook urls for test pass / test fail and build fail?

I assume issue 1 is no longer an issue?

Hey @YOU54F, one of our use cases is to post to various channels depending on the result, but we also want to post to multiple channels at the same time. Unsure if those are handled by the Slack API.

The first issue is resolved, thanks.

No it doesn't, you would need to provide a seperate webhook per channel iirc and if you wanted to email multiple channels, you can execute the reporter multiple times with a different webhook.

We could support a distinct webhook per result (build fail/test fail/test pass) and support an array of webhooks. If there is an array, the message could be sent once for each webhook?

Yes that should work, that would allow us to supply as many webhooks as are needed, and have results sent to as many channels as necessary per test run.

Hi @YOU54F, regarding the first issue I notice the following command

yarn cypress run --spec 'cypress/integration/PR/test.spec.js' && npx cypress-slack-reporter

gives an error Error: Cannot find test report @ mochareports

My package.json is using the same dep versions as the one in this repo.

Running
yarn cypress run --spec 'cypress/integration//PR/test.spec.js' && npx ts-node script.ts

Will run the reporter but also seems to run the full test suite in addition to test.spec.js

I have exported the webhook first using export SLACK_WEBHOOK_URL=xxxso not sure why this is failing. Your solution to issue 1 makes sense in theory, haven't seen it working yet however.

Can you show me the contents of your script.ts?

When you run your PR, where is your test report created? Have you created a mochawesome test report in json format? If so you need to tell the slack reporter where your report lives.

With regards to point 2, I have released a new version 0.10.0 which will support a webhook per status ( build fail / test fail / test pass ) and any webhook env var can support multiple webhooks separates by a comma

Am running it locally using spec.ts from this repo, in the root of my repo.
There are existing test reports in cypress/reports/mocha/mochawesome.json

Also tried with
yarn cypress run --spec 'cypress/integration/PR/test.spec.js' && ./node_modules/.bin/cypress-slack-reporter --report-dir cypress/reports/mocha (relative to package.json) with the same error, I must be passing something incorrectly?

Regarding point 2, that's awesome thanks. Can't wait to try it out πŸ‘

Thanks, I had a duplicated report dir that was not longer used, my bad. This is working as needed:

yarn cypress run --spec 'cypress/integration/PR/test.spec.js' && npx cypress-slack-reporter --vcs-provider bitbucket...

@you1anna happy days! Let me know how you get on with testing the new feature for diff webhooks per alert and then we can close this down. Thanks for the feature request as well, and I hope others can make use of it too :) I will be able to at work to alert a main channel when there is a failure and just a healthcheck channel when it passes 🀘🏽

Excellent will do, should hopefully be able to get it into our pipeline this week and provide you with some feedback πŸ™‚

Hey @YOU54F, consistently getting an error with the report directory when not running via script.ts

rm -rf ./cypress/reports/mocha && yarn cypress run --spec 'cypress/integration/PR/test.spec.js' && npx cypress-slack-reporter --vcs-provider bitbucket --ci-provider custom --report-dir 'mochawesome-report' --video-dir 'cypress/videos' --custom-url 'https://go.testing.build.xxx-cdn.net' --verbose true

The error is
Error: Cannot find test report @ mochawesome-report

When passing the report dir as cypress/reports/mocha
Error: Cannot find test report @ cypress/reports/mocha

Is additional config needed when invoking using this command, as opposed to with script.ts?

@YOU54F not sure how to proceed with this one, would you be able to repro locally? Afaik the only way to trigger a subset of tests with the reporter.

Have double checked all package versions are the same, it doesn't seem to detect the reportDir correctly regardless of the setting.

Ah didn't realise HTML had to be true, thanks. Have set this in reporterOpts. The issue is now:

  • Successful runs post the result to slack
  • failures do not

rm -rf ./cypress/reports/mocha && yarn cy:run:ecards && npx cypress-slack-reporter --vcs-provider bitbucket --ci-provider custom --report-dir 'cypress/reports/mocha' --video-dir 'cypress/videos' --custom-url 'https://go.testing.build.xxcdn.net' --verbose true

All tests pass
Screen Shot 2020-07-10 at 09 33 30

Some tests fail

  • No slack post
    error Command failed with exit code 4. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Will try creating a branch in this repo to show the issue.

We are the payload parameter as JSON in as post request, as opposed to sending a JSON string as the body of a post request. Unsure which is the new way?

Hey @YOU54F Here is a repo example, if you export a SLACK_WEBHOOK_URL value first then run the command in the readme, hopefully it should repro the issue, thanks.

https://github.com/you1anna/cypress-slack-reporter-issue

Hey dude, thanks for the repro, will have a look. I think I know what the issue is. You might just need to pass in the specs you want to run, as a parameter to the node script you are running, so that the cli runner for cypress can pick it up.

Might not get a chance until later this week. Thanks for perservering though

We are the payload parameter as JSON in as post request, as opposed to sending a JSON string as the body of a post request. Unsure which is the new way?

I don't understand this comment, could you elaborate please?

Hey dude, thanks for the repro, will have a look. I think I know what the issue is. You might just need to pass in the specs you want to run, as a parameter to the node script you are running, so that the cli runner for cypress can pick it up.

Might not get a chance until later this week. Thanks for perservering though

Thanks, the command above is passing cy:run:ecards which runs a /ecards/* command, is there an example of the correct way to do this? Passing specs individually would get messy, I think as there may be many tests.

Whenever you have time that would be a great help, cheers.

We are the payload parameter as JSON in as post request, as opposed to sending a JSON string as the body of a post request. Unsure which is the new way?

I don't understand this comment, could you elaborate please?

I'm not sure how to differentiate between the new and old web hooks, devops control that stuff, but I believe we are using the standard hooks.

Hey @you1anna

Updated your repo with examples

https://github.com/you1anna/cypress-slack-reporter-issue/pull/1/files

Basically you need you chain your slack command with || otherwise the exit code thrown from cypress will cause the next step to fail.

Setup some test webhooks against a slack org I used test integrations,they are removed now so you will need to create your own.

We need to pass in the htmlReport path to get the path and file name of the html report for building up a url link.

do you have a remote path and filename for your html report in your custom location? If so maybe we need to support the user providing a customUrl and a custom path inc filename to the html test report.

Or provide a noHtml flag to ignore the html report side entirely (we only need the json test report to determine the test run state, the html report is just reporting sugar in the slack message)

Btw you can run the script like so

SPECS='cypress/integration/pass/*' ts-node script.ts

If you were you add the following option spec: process.env.SPECS, here https://github.com/YOU54F/cypress-slack-reporter-issue/blob/5954b8f79ccea0c4c0a515b184e9e3fb8c70742f/script.ts#L14

CypressNpmApi.run({
  spec: process.env.SPECS,
  reporter: "cypress-multi-reporters",
  reporterOptions: {
    reporterEnabled: "mocha-junit-reporter, mochawesome",
    mochaJunitReporterReporterOptions: {
      mochaFile: "cypress/reports/junit/test_results[hash].xml",
      toConsole: false,
    },
    mochawesomeReporterOptions: {
      reportDir: "cypress/reports/mocha",
      quiet: true,
      overwrite: false,
      html: false,
      json: true,
    },
  },
})

Thanks very much for this @YOU54F

My slack webhook doesn't seem to be posting anything currently, will need to confirm it's still working on Monday.

These are the results trying with Makefile:

with make robin-test

  β”‚ βœ–  RandR/PR/ecards.spec.js                  00:32        1        -        1        -        - β”‚
    βœ–  1 of 1 failed (100%)                     00:32        1        -        1        -        -  

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
 ciProvider:- custom
 customUrl:- https://go.testing.build.rg-cdn.net
 vcsProvider:- bitbucket
 reportDirectory:- cypress/reports/mocha
 videoDirectory:- cypress/videos
 screenshotDirectory:- cypress/screenshots

TypeError: Cannot read property 'split' of undefined
make: *** [robin-test] Error 1

Trying with the script after adding the SPEC param :

SPECS='cypress/integration/pass/*' && npx ts-node script.ts

       Spec                                              Tests  Passing  Failing  Pending  Skipped  
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ βœ–  error/error.spec.js                        0ms        -        -        1        -        - β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
  β”‚ βœ–  fail/fail.spec.js                        00:01        1        -        1        -        - β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
  β”‚ βœ”  pass/pass.spec.js                        861ms        1        1        -        -        - β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
  β”‚ βœ–  RandR/PR/ecards.spec.js                  00:31        1        -        1        -        - β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    βœ–  3 of 4 failed (75%)                      00:33        3        1        3        -        -  

Merged report available here:- [
  '/Users/robin.miklinski/rg/cypress-slack-reporter-issue/mochawesome-report/mochawesome.html',
  '/Users/robin.miklinski/rg/cypress-slack-reporter-issue/mochawesome-report/mochawesome.json'
]
Constructing Slack message with the following options {
  ciProvider: 'custom',
  vcsRoot: 'bitbucket',
  reportDir: 'mochawesome-report',
  videoDir: 'cypress/videos',
  screenshotDir: 'cypress/screenshots',
  verbose: true
}
Error: TypeError: Cannot read property 'split' of undefined
    at Object.slackRunner (/Users/robin.miklinski/rg/cypress-slack-reporter-issue/node_modules/cypress-slack-reporter/bin/slack/slack-alert.js:43:15)
    at /Users/robin.miklinski/rg/cypress-slack-reporter-issue/script.ts:69:5
    at tryCatcher (/Users/robin.miklinski/rg/cypress-slack-reporter-issue/node_modules/bluebird/js/release/util.js:16:23)

I am unclear whether this error is causing the slack post to fail, will be able to confirm on Monday. Do you know what Cannot read property 'split' of undefined is referring to?

do you have a remote path and filename for your html report in your custom location? If so maybe we need to support the user providing a customUrl and a custom path inc filename to the html test report.

Our build server is GoCD and the report will sit in a custom location as an artefact, support for this would be great, but non essential.

Ahh the split is trying to split a webhook, it suggests you haven't set one. Basically the split function will split a webhook on the , in order to allow the sending a message to multiple channels.

https://github.com/YOU54F/cypress-slack-reporter/blob/04181cf9dbb91c45acdf46aca1a6d42083b74bf3/src/slack/slack-alert.ts#L110

I can wrap that in a try / catch and throw a descriptive error there to help users

If you can set export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/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 you should be able to process the slack report, but it will return a 404 (as those webhooks are been removed now) and then run make robin-test you should be golden

#any
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/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
#error
SLACK_WEBHOOK_ERROR_URL=https://hooks.slack.com/services/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,https://hooks.slack.com/services/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
#failed
SLACK_WEBHOOK_FAILED_URL=https://hooks.slack.com/services/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,https://hooks.slack.com/services/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
#passed
SLACK_WEBHOOK_PASSED_URL=https://hooks.slack.com/services/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,https://hooks.slack.com/services/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

you can check if the env var is set with echo $SLACK_WEBHOOK_URL πŸ‘πŸ½

I've the most recent branch I am working on, I've made it more fault tolerant so that if

  • if can't find a test report, it will always send a build error message, and log a warning
  • if it can't a html file, it will no longer error (will need additional param to pass in a html file name) and still report on the test run, and log a warning
  • if it finds more than one test report, it will use the first and log a warning

Hopefully that should ensure that whatever the state of the test run, it should be able to report a message to slack, as the notification is the most important thing.

Ahh the split is trying to split a webhook, it suggests you haven't set one. Basically the split function will split a webhook on the , in order to allow the sending a message to multiple channels.

https://github.com/YOU54F/cypress-slack-reporter/blob/04181cf9dbb91c45acdf46aca1a6d42083b74bf3/src/slack/slack-alert.ts#L110

I can wrap that in a try / catch and throw a descriptive error there to help users

If you can set export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/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 you should be able to process the slack report, but it will return a 404 (as those webhooks are been removed now) and then run make robin-test you should be golden

#any
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/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
#error
SLACK_WEBHOOK_ERROR_URL=https://hooks.slack.com/services/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,https://hooks.slack.com/services/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
#failed
SLACK_WEBHOOK_FAILED_URL=https://hooks.slack.com/services/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,https://hooks.slack.com/services/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
#passed
SLACK_WEBHOOK_PASSED_URL=https://hooks.slack.com/services/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,https://hooks.slack.com/services/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

I had set the .env file hooks to my test hook and run source etc, but nothing seems to post to slack, so wondering if something else is wrong. Will confirm with the team tomorrow.

Hopefully that should ensure that whatever the state of the test run, it should be able to report a message to slack, as the notification is the most important thing.

Yes those changes all sound sensible, especially if something changes in CI and the report cannot be found for whatever reason.

latest version now released v1.0.2

FYI

https://unix.stackexchange.com/a/90995

basically use && npx cypress-slack-reporter || npx cypress-slack-reporter to allow it to execute the slack message regardless of whether the step before passed or failed

Hey @YOU54F

In CI we have the Test Report button linking to artefacts, containing the report, videos etc which is great - that part's working fine.

Screen Shot 2020-07-22 at 17 28 55
Screen Shot 2020-07-22 at 17 28 16

There are two outstanding issues:

  • How can the commit link to bitbucket be constructed?

Screen Shot 2020-07-22 at 17 17 05

  • Previously locally it was returning video links in the slack post, but have not seen these generated since 18th June, I think on v0.8 on cypress-slack-reporter. The video's directory is being passed as a parameter, and it should be working.

Screen Shot 2020-07-22 at 15 08 59

which ci system do you use for building? can you raise that as a new issue.

2nd issue is fixed, it was a regression.

cheers again @you1anna