Set cookie in after() hook nightwatch - nightwatch.js

I tried to set cookie in nightwatch after() hook function but apparently it didn't work. The idea is I want to set the cookie's value as "failed" if the test failed and "success" if the test passed.
export = {
'#tags': [ 'heboh' ],
after(browser) {
browser
.setCookie({ name: 'mycookie', value: 'success' })
.getCookie('mycookie', function callback(result) {
console.log(result); // print null
})
.end();
},
'create heboh'(browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.assert.title('Facebook'); // intended to make it failed
}
}
I specified --verbose and this is what I got
FAILED: 1 assertions failed and 1 passed (5.446s)
INFO Request: POST /wd/hub/session/null/cookie
- data: {"cookie":{"name":"mycookie","value":"true"}}
- headers: {"Content-Type":"application/json; charset=utf-8","Content-Length":45}
INFO Response 404 POST /wd/hub/session/null/cookie (19ms) { sessionId: 'null',
value:
{ error: 'invalid session id',
message: 'No active session with ID null',
stacktrace: '' },
status: 6 }
LOG → Completed command cookie (22 ms)
INFO Request: GET /wd/hub/session/null/cookie
- data:
- headers: {"Accept":"application/json"}
INFO Response 404 GET /wd/hub/session/null/cookie (15ms) { sessionId: 'null',
value:
{ error: 'invalid session id',
message: 'No active session with ID null',
stacktrace: '' },
status: 6 }
null
LOG → Completed command cookie (16 ms)
LOG → Completed command end (0 ms)
Looks like there is no session in after() function.

Try this, I believe you need to call done on your after hook.
after: function (browser, done) {
browser
.setCookie({name: 'mycookie', value: 'success'})
.getCookie('mycookie', function callback(result) {
console.log(result);
})
.end()
.perform(function () {
done();
});
}

Related

Lambda#Edge function result failed validation

I have a lambda#edge in Viewer Request that is generating this error:
ERROR Validation error: Lambda function result failed validation, the function tried to add read-only header, headerName : Transfer-Enoding.
I tried to return this event in case of redirection:
const response = {
status: '301',
statusDescription: 'Found',
headers: {
'location': [{
key: 'location',
value: 'https://test.com'
}]
}
}
callback(null, response)
I also tried keeping the same event but I get the same error
const response = event.Records[0].cf.response;
response.status = 301;
response.statusDescription = 'Found';
response.headers['location'] = [{ key: 'Location', value:'https://www.test.com'}];
Cqn someone tells me how to fix this ?
PS: Even requests that pass by this lambda and doesnt verify the redirection condition result in this error also.

Cypress: how to fix a redirect error in Cypress?

To test, I need to log in to the site, and then go to the page "page/1". I am doing:
describe("[![my first test][1]][1]", () => {
beforeEach(() => {
const LOGIN = "admin";
const PASSWORD = "12345";
cy.visit("/");
cy.login(LOGIN, PASSWORD);
});
it("displaying a page with mok data", () => {
cy.visit("/page/1");
//mock redux store
cy.window()
.its("store")
.invoke("dispatch", {
type: "set_data",
data: data,
});
but i get error
AssertionError
Timed out retrying after 4000ms: Expected to find element: [data-test-id=login-input], but never found it. Because this error occurred during a before each hook we are skipping the remaining tests in the current suite: my first test
Need help
login command
Cypress.Commands.add('login',
(
login,
password,
status
) => {
cy.intercept("POST", "/login").as("login");
cy.getByDataTestId("login-input").type(login);
cy.getByDataTestId("password-input").type(password);
cy.getByDataTestId("signin-button").click();
}
);

Cypress: Test fails if request from "cy.route()" is not found

I have a question about cypress testing.
I'm doing the following:
cy.route() to an url with alias
then cy.wait(#alias)
I know that the default action that cypress does it to fail the test if the there wasn't any request made to that url.
My problem is that I have multiple requests and one of them may not reach the request url. But I don't want that to fail my test, just to skip over it. How can I do this?
Basically, I'm asking how do you make your tests NOT to fail when you get this:
CypressError: Timed out retrying: cy.wait() timed out waiting 30000ms for the 221st response to the route: 'productRequest'. No response ever occurred.
If your usecase is to wait for requests and then continue with more commands, this solution might help you:
describe("route", () => {
it("hiting route", () => {
let req1 = false;
let req2 = false;
cy.server()
cy.route({
methdod: "GET",
onRequest: () => {
req1 = true;
},
url: "/will/eventually/called"
});
cy.route({
methdod: "GET",
onRequest: () => {
req2 = true;
},
url: "/will/eventually/called2"
});
setTimeout(() => {
req2 = true
}, 2000)
cy.visit("https://biehler-josef.de")
cy.get("body").should(() => {
if (req1) {
expect(req1).to.eq(true);
}
if (req2) {
expect(req2).to.eq(true);
}
if (!req1 && !req2) {
expect(false).to.eq(true)
}
});
cy.get("body").should("exist");
});
})
You define the routes and pass a onRequest function that sets a variable. This can be done with multiple routes. Then you use should with callback function. Within that you can check both variables and force to fail only if no request occurred. The setTimeoutin this example demonstrates a request that takes 2 seconds to finish.
If you want to check if a request is not hit, it is much easier. But this solution is not usable if you want to execute additional commands after the cy.wait(#alias):
describe("route", () => {
it("hiting route", (done) => {
cy.server()
cy.route("GET", "will/never/be/hit").as("requestalias");
cy.visit("https://biehler-josef.de")
cy.on("fail", (error) => {
if (error.name === "CypressError"
&& error.message.match(/.*Timed out retrying: cy.wait().*requestalias.*/)) {
// calling done forces cypress to turn test to green
done()
}
});
cy.wait("#requestalias")
});
})
With cy.on("fail") you can listen to the event that is thrown when a test fails. Caling done() within this will force the test to be green. But you can not continue with subsequent commands in your test. So the wait() must be the last command in your test

OSF.DDA.Error 9020 on Office.context.mailbox.item.body.getAsync()

Since a few days we get an error response 9020 on the call Office.context.mailbox.item.body.getAsync. This code used to work flawless. Is there a Microsoft problem?
getMessageBody() {
// call the async function to get the body
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Text,{},
(result) => {
this.entityAnalyzeData.EntityItem.Properties.ContactProperty.Body = String(result.value);
// inform listeners
this.mlBodySubject.next('ok');
}
);
The result is
OSF.DDA.AsyncResult {value: null, status: "failed", error: OSF.DDA.Error}
value: null
status: "failed"
error: OSF.DDA.Error
name: "GenericResponseError"
message: "Er is een interne fout opgetreden."
code: 9020
Thanks in advance for your attention!
Wilco

Mocha Chai HTTP post request not working

The following test is not working with mocha-chai, it is able to to get the input request but throws the error message.
it('/hb : ', function (done) {
return chai.request(app)
.post('/hb')
.send({"a":1 })
.then(function (res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
// { ah: { rt: [Object] }, ad: { mojo: 1 } } }
//console.log("CAlling DOne ........... +");
done();
}, function (err) {
//console.log(err);
throw err;
});
});
Output:
Web Requests : /hb : :
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
The functions that chai-http adds to chai return promises. In your code you return the promise, which is good. However, you also declare your test to take the a parameter: function (done). This would be fine if you did not return the promise, but returning the promise is really the better mechanism here. When you declare your test to take a parameter, Mocha ignores the return value from the test, and so the promise is ignored. So just remove your use of done.
Here's an example that reproduces the error you had in your original code with err being undefined in the function you pass to then.
'use strict';
var app = require('./server');
var chai = require('chai');
chai.use(require('chai-http'));
var expect = chai.expect;
it('/hb', function () {
return chai.request(app)
.post('/hb')
.send({a: 1})
.then(function (res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
});
});
If the server returns a 200 status, then you'll get this on the console:
1) /hb
0 passing (26ms)
1 failing
1) /hb:
ReferenceError: err is not defined
at test.js:13:20
If the server returns a 400 status, the output would be:
1) /hb
0 passing (24ms)
1 failing
1) /hb:
Error: Bad Request
at Test.Request.callback (node_modules/superagent/lib/node/index.js:792:17)
at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:990:12)
at endReadableNT (_stream_readable.js:913:12)
you need to add following:
.set('content-type', 'application/x-www-form-urlencoded')
you can reference this question over Post request via Chai

Resources