33
loading...
This website collects cookies to deliver better user experience
github-user
. Make sure you have Node.js (version 12 or higher is preferred) installed.npm init -y
package.json
, which we will use later to install Cypress. Now, create a file named index.html
with the following content:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>GitHub User</title>
<link rel="stylesheet" href="main.css" />
</head>
<body>
<main>
<form>
<div>
<label for="uname">User Name</label>
<input type="text" required name="uname" id="uname" />
</div>
<div>
<button type="submit" id="submitBtn">Get Details</button>
</div>
</form>
</main>
</body>
<script defer src="github.js"></script>
</html>
npx serve
command to serve static sites or single-page applications (we will come back to this later).npx serve
main.css
at the root of the project folder with the following content:body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
button {
margin: 10px 0px auto 0px;
float: right;
padding: 10px;
background-color: #009aff;
color: #FFF;
border-radius: 4px;
cursor: pointer;
}
input[type="text"] {
height: 25px;
}
span {
margin: 10px;
background-color: #000;
color: #FFF;
padding: 20px;
font-size: 20px;
}
cypress
. Use this command from the root of your test folder to install it.npm install cypress
package.json
file and replace the existing scripts
section with the following content:"scripts": {
"e2e": "cypress open"
}
npm run e2e
cypress\\integration\\examples
. You can choose to delete this folder or keep it – whichever works for you. In addition, Cypress creates an empty configuration file called cypress.json
. Open that file and add the following content:{
"baseUrl": "<http://localhost:5000>"
}
github.spec.js
under the folder cypress\\integration
. This is our test spec file, and we will add all our tests to this file.describe("GitHub test", () => {
it("Get a GitHub user details", () => {
cy.visit("/");
cy.get("form");
cy.get('input[name="uname"]')
.type("atapas")
.should("have.value", "atapas");
});
});
describe
keyword allows you to write multiple tests in a test suite. It takes two arguments: the first is a string to describe the test suite and a callback for the actual test, which is written within the it
block.it
block takes two arguments. The first is the test name and the second is the callback where we write the test scenario. We have defined the test scenario as:/
. This is where the application page will be served.get
method. First, we get the form
element and then the input
element that matches a name attribute value, uname
.atapas
and check if it has the same value.github.js
at the root of the project folder with the following content:const form = document.forms[0];
form.addEventListener("submit", event => {
event.preventDefault();
new FormData(form);
});
document.addEventListener("formdata", event => {
const body = Object.fromEntries(event.formData.entries());
const jsonBody = JSON.stringify(body);
const request = new XMLHttpRequest();
request.open("GET", `https://api.github.com/users/${body.uname}`);
request.send(jsonBody);
request.onload = function() {
const json = JSON.parse(this.response);
console.log(json);
if (json.message && json.message === 'Not Found') {
console.log('user name not found');
} else {
document.body.innerHTML += `<span>Response from the server: ${json.name}</span>`;
}
};
});
XMLHttpRequest
to the GitHub API. Upon a successful completion, we are showing the name returned in the response. All this takes place when a user clicks on the ‘submit’ button.atapas
, to get the full name back from the server.cy.intercept()
to stub any type of HTTP request. This is an excellent feature addition as previously we had to follow multiple steps along with configuration changes to achieve the same end.cy.intercept({
url: "/github/**",
method: "GET"
});
cy.get("form").submit();
cy.contains("Tapas Adhikary");
/github/**
in the URL. Then we submit the form. Finally we test the response with cy.contains()
. Here is the entire spec file:describe("GitHub test", () => {
it("Get a GitHub user details", () => {
cy.visit("/");
cy.get("form");
cy.get('input[name="uname"]').type("atapas").should("have.value", "atapas");
cy.intercept({
url: "/github/**",
method: "GET"
});
cy.get("form").submit();
cy.contains("Tapas Adhikary");
});
});
atapas1
.cy.get('input[name="uname"]').type("atapas").should("have.value", "atapas1");
atapas
but the value is not matching the expectation. You will see the failed test output as well:index.html
file and add this script under the head section of the HTML file:<script defer src="<https://js.bugfender.com/bugfender.js>"></script>
github.js
file and add the following code at the top of the file:Bugfender.init({
appKey: 'VocUSy3tli1e9Hf8Z5wwQ6oO6qcbXfRR',
});
document.addEventListener("formdata", event => {
const body = Object.fromEntries(event.formData.entries());
const jsonBody = JSON.stringify(body);
const request = new XMLHttpRequest();
Bugfender.log(`Sending request to fetch info about ${jsonBody}`);
request.open("GET", `https://api.github.com/users/${body.uname}`);
request.send(jsonBody);
request.onload = function() {
const json = JSON.parse(this.response);
console.log(json);
if (json.message && json.message === 'Not Found') {
console.log('user name not found');
Bugfender.sendIssue("Something's wrong", "Details of the error here...");
Bugfender.fatal(`We couldn't make it work`);
} else {
document.body.innerHTML += `<span>Response from the server: ${json.name}</span>`;
Bugfender.log(`Got information successfully about ${json.name}`);
}
};
});
sendIssue
method. If the call is successful, we log that too. Let us now go to our app in the browser and try it with two GitHub ids: one valid, the other invalid.