How can I report cypress failed test to custom URL - cypress

I would like to report my failed cypress test to my URL.
For example to www.myserver.com/failedtest
How can I do it?
Is there any solution ready? I found only slack integration.

Take a look at this awesome answer: https://stackoverflow.com/a/69382872/1757737
The gist of it:
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
const details = { error: runnable.err.message}
fetch('https://somewebhook', {method: 'POST', mode: 'no-cors', body: JSON.stringify(details)});
}
})

Related

Set cookies and return user on Cypress request

Problem
I have a Cypress command where I can login with a random user. The API will return the following response:
{
user: { ... }
token: { ... }
}
What I would like to do is to:
Create user using cy.request
Set the cookie in the browser
Return the response out of the command so that I can work with it outside of the command
What I have tried
return cy.request({
method: 'POST',
url: getApiUrl('__cypress__/login'),
body: requestBody,
log: false,
})
.then(({ body }) => {
cy
.setCookie('_token', body.token.plainTextToken)
.then(() => {
Cypress.log({
name: 'login',
message: JSON.stringify(body),
consoleProps: () => ({ user: body }),
});
});
})
.its('body', { log: false }) 👈 times out here
What I'm looking for is to do something like:
cy.login().then(({ user }) => {
// use logged in user
})
Question
Cypress times out on .its(...) line. Is this possible to do it? Looking at the docs I couldn't find any example on what I'm trying to achieve
(from the comments)
It happens because previously chained subject, does not return anything. An explicit return for the body property will fix it.

MS Teams - getAuthToken API failing on iOS

msTeams.authentication.getAuthToken is returning failure Callback with Error message- Auth Library Error. It is working fine for Android and Desktop. The issue only occurs on iOS.
Following is the code snippet for the reference,
getClientToken() {
return new Observable<any>(subscriber => {
this.teamsService.authentication.getAuthToken({
resources: ['https://graph.microsoft.com/openid',
'https://graph.microsoft.com/user.read.all',
'https://graph.microsoft.com/group.read.all',
'https://graph.microsoft.com/groupmember.read.all'],
successCallback: clientToken => {
subscriber.next({ clientToken, type: this.authType });
subscriber.complete();
},
failureCallback: reason => {
subscriber.error(reason);
}
});
});
}
enter image description here

Cypress: changing the code while running crashes my tests (request aborted)

I'm testing an Angular App with Cypress.
I'm running my test with the Cypress dashboard, that I open using this command:
$(npm bin)/cypress open
I'm calling an API with my test: it works.
But when I change my code, Cypress will rerun the code which will cause my first (and only my first test) to fail. The request calling the API is aborted.
The only way to make it work again is to manually end the process, then start it again.
Has anyone got an idea what is causing this strange behaviour?
Here is my test code:
beforeEach(() => {
cy.visit('/');
cy.server();
cy.route('POST', `myUrl`).as('apiCall');
});
it('should find a doctor when user searches doctor with firstName', () => {
cy.get('#myInput').type('foo');
cy.get('#submitButton]').click();
cy.wait('#apiCall').then((xhr) => {
expect(xhr.status).to.eq(200);
});
});
You can prepare XHR stub like this:
describe('', () => {
let requests = {}; // for store sent request
beforeEach(() => {
cy.server({ delay: 500 }); // cypress will answer for mocked xhr after 0.5s
cy.route({
url: '<URL>',
method: 'POST',
response: 'fixture:response',
onRequest: ({ request }) => {
Object.assign(requests, { someRequest: request.body }); // it has to be mutated
},
});
});
And then in test:
it('', () => {
cy
.doSomeSteps()
.assertEqual(requests, 'someRequest', { data: 42 })
});
There is 2 advantages of this solution: first 0.5s delay make test more realistic because real backend doesn't answer immediately. Second is you can verify if application will send proper payload after doSomeActions() step.
assertEqual is just util to make assertion more readable
Cypress.Commands.add('assertEqual', (obj, key, value) =>
cy
.wrap(obj)
.its(key)
.should('deep.equal', value)
);

How to stub a call to graphql using cypress?

I'm writing a Vue app that uses vue-apollo to interact with graphql. I'm wondering if it's possible to stub the graphql requests. I thought this should work:
it('should access a story', function() {
cy.server();
cy.route('http://localhost:3002/graphql', {
data: {
Story: { id: 2, title: 'story title', content: 'story content' }
}
});
cy.visit('/stories/2');
});
Unfortunately, I get an error from graphql complaining that id is an Int instead of an ObjectId. Am I missing something?
The problem was that stubbing fetch requests isn't yet implemented in Cypress (which is what Vue Apollo is using). I ended up following these instructions:
Install github/fetch
Add this to cypress/support/index.js:
.
Cypress.on('window:before:load', win => {
win.fetch = null;
win.Blob = null;
});
Now it works!
I got it working with this package here:
npm i #iam4x/cypress-graphql-mock
Add this line to 'support/commands.js'
import "#iam4x/cypress-graphql-mock";
go to your graphiql playground and download your schema
add task command to 'plugins/index.js' (REMEMBER TO CHANGE PATH TO SCHEMA FILE YOU DOWNLOADED EARLIER)
module.exports = (on, config) => {
on("task", {
getSchema() {
return fs.readFileSync(
path.resolve(__dirname, "../../../schema.graphql"),
"utf8"
);
}
});
};
write your tests with loaded schema
beforeEach(() => {
cy.server();
cy.task("getSchema").then(schema => {
cy.mockGraphql({
schema
});
});
});`
describe("Login Form", () => {
it("should redirect after login", () => {
cy.mockGraphqlOps({
operations: {
Login: {
login: {
jwt: "some-token",
user: {
id: "5d5a8e1e635a8b6694dd7cb0"
}
}
}
}
});
cy.visit("/login");
cy.getTestEl("email-input").type("Max Mustermann");
cy.getTestEl("password-input").type("passwort");
cy.getTestEl("submit").click();
cy.getTestEl("toolbar-title").should("exist");
});
})
Visit the original repo for further explanation as i find it less confusing. The package you have installed is just a working fork of this one:
https://github.com/tgriesser/cypress-graphql-mock

Get currently executed describe/test name

Is it possible with Jest (Jasmine) to get the currently executed name of the test or describe inside the test?
Using Jasmine: How to get name of current test is not working anymore, at least with Jest.
e.g.
test('Error missing body', (done) => {
console.log('Currently executing: ' + REFERENCE_TO_TEST_NAME);
done();
});
Thanks
From this thread:
console.log(expect.getState().currentTestName);
Worked for me.
The tests are supposed to contain only the basic code for your test: Arrange / Act / Assert, so it's not a good practice to introduce this kind of code at this place. But if you want to log the currently running test, you can use the custom_reporter API: https://jasmine.github.io/2.1/custom_reporter.html
You can get the same result that you expect by adding this code:
jasmine.getEnv().addReporter({
specStarted: function(result) {
console.log(`Spec name: ${result.fullName}, description: ${result.description}`);
}
});
you can try:
let spec = test('Error missing body', (done) => {
console.log('Currently executing: ' + spec.getFullName());
done();
});
const testParam = 'any text you need';
describe(`${testParam}`, () => {
test('mind the backtick', () => {
console.log(`Currently executing: ${testParam}`);
});
});

Resources