cypress-crud
generate-datafaker
generate-datafaker
Required
| NodeJs
| Cypress version 10 >
Installation
To install the package, run the following command in your Cypress project:
npm i cypress-crud
Use Snippets in cy.action
test_bdd
test_action
test_bdd_BR
scenario
given
when
and
then
cenario
dado
quando
e
entao
Settings
cy.crud automatically adds dependencies to the project in e2e.js file
export {
Scenario,
Given,
When,
And,
Then,
Cenario,
Dado,
Quando,
E,
Entao,
describes,
its,
crudStorage,
} from "cypress-crud/src/gherkin/bdd.js";
import "cypress-plugin-steps";
export const faker = require("generate-datafaker");
import "cypress-crud";
import "cypress-plugin-api";
Use
š© create a json in the fixtures folder, which can be inside a subfolder "fixtures/token/createToken.json"
{
"request": {
"method": "GET",
"url": "https://reqres.in/api/users/2",
"body": null,
"qs": null,
"headers": {
"Content-Type": "application/json"
}
}
}
cy.crud({ payload: "requests/createToken.json" })
cy.crud({ payload: "requests/createToken.json", alias:"body" })
Or
let obj = {
request: {
method: "GET",
url: "https://reqres.in/api/users/2",
body: null,
qs: null,
headers: {
"Content-Type": "application/json",
},
},
};
cy.crud({ payload: obj });
cy.crud whit endpoint param
> JSON
{
"request": {
"method": "GET",
"url": "https://reqres.in/api/users/2",
"path": "save"
"body": null,
"qs": null,
"headers": {
"Content-Type": "application/json"
}
}
}
it('Generate id ', ()=>{
cy.crud({ payload: "token/createToken.json" })
})
Use in different environments
in cypress.config.js
env: {
environment: "QA",
QA: {
getUser: "https://reqres.in/api/users?page=2",
},
DEV: {
getUser: "https://reqres.in/api/users?page=2"
},
PROD: {
getUser: "https://reqres.in/api/users?page=2"
},
}
in JSON
{
"endpoint": "getUser",
"request": {
"method": "GET",
"url": "https://reqres.in/api/users/2",
"body": null,
"qs": null,
"headers": {
"Content-Type": "application/json"
}
}
}
Use whit validations
š» with validations you just need to inform the path and the value, regardless of which part of your JSON it is.
{
"request": {
"method": "GET",
"url": "https://reqres.in/api/users/2",
"body": null,
"qs": null,
"headers": {
"Content-Type": "application/json"
}
},
"validation": [{ "path": "status", "value": 200 }, { "path": "first_name" }, ...]
}
cy.crud({ payload: "token/createToken.json" })
assert expected 200 to exist
assert expected 200 to deeply equal 200
assert expected Michael to exist
.bodyResponse()
š» If you don't want to use validations directly in JSON, you can use .bodyResponse to validate the return.
cy.crud({ payload: "token/createToken.json" })
.bodyResponse({path: "name", value:"Jam Batista"})
code hiden bodyResponse
expect(search).to.exist;
if (equal) {
expect(search).to.eql(equal);
}
return expect(search);
.save()
š» save will set aside a value entered to be used in the future when you request it.
cy.crud({ payload: "token/createToken.json" }).save({ path: "name" });
console.log("storage", crudStorage.save.save);
get data in .save()
cy.save({ log: false }).then((rescue) => {
expect(rescue).to.eql("Weaver");
});
});
cy.save({ log: false , alias:"name"}).then((rescue) => {
expect(rescue).to.eql("Weaver");
});
});
OR
console.log('save', crudStorage.save.save)
.write()
š» Uses cy.writeFile requirements
cy.crud({ payload: "token/createToken.json" }).write({ path: "user/getUser" });
.read()
š» Uses cy.writeFile requirements
cy.crud({ payload: "token/createToken.json" }).read({ path: "user/getUser" });
cy.read({ path: "user/getUser" }).then((json) => {
console.log(json);
});
Use by inheritance
it('Generate access_token', ()=>{
cy.crud({ payload: "requests/getUser" })
.save({ path: "access_token", alias: "token" });
})
š„ in another file or in the same test
let json require('../fixtures/requests/createToken.json')
let data = {...json};
it('Get token in use headers', ()=>{
data.request.headers = {Authorization: `Bearer ${crudStorage.save.token}`}
cy.crud({ payload: data })
.bodyResponse({path: "status", equal: 201})
.bodyResponse({path: "name", equal:"Jam"})
.save({ path: "data"})
.write({ path: "user/getUser" });
})
Contributions
Contributions are always welcome. Feel free to open issues or send pull requests.
License
This project is licensed under the terms of the MIT License.