Commit 485142cb authored by iamabdulsattar92's avatar iamabdulsattar92

Add All files of Cypress API Testing

parents
Feature: Deleting Data with DELETE Request
Scenario: Sending a DELETE request to delete data
Given I have the endpoint "/posts/1"
And I set the request headers as:
| Authorization | Bearer myAccessToken |
When I make a DELETE request
Then I should see a response with status code 200
const { Given, When, Then } = require("cypress-cucumber-preprocessor/steps");
Given('I have the endpoint {string}', (url) => {
cy.wrap(url).as('apiEndpoint');
});
Given('I set the request headers as:', (table) => {
const headers = table.hashes();
cy.wrap(headers).as('requestHeaders');
});
When('I make a DELETE request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.get('@requestHeaders').then((headers) => {
cy.request({
method: 'DELETE',
url,
headers,
}).as('response');
});
});
});
Then('I should see a response with status code {int}', (statusCode) => {
cy.get('@response').its('status').should('equal', statusCode);
});
Feature: call the API and see the response
using dummy api calls to verify the assertions
Scenario: Verify a successful API GET request for All Users
Given I make a GET request to "/posts"
Then the response status code should be 200
And the response should contain body
Scenario Outline: Verify a successful API GET request against specific post id
Given I make a GET request to "/posts/<postId>"
Then the response status code should be <statusCode>
Examples:
| postId | statusCode |
| 1 | 200 |
| 2 | 200 |
\ No newline at end of file
import { Given, When, Then } from "cypress-cucumber-preprocessor/steps";
// This function is For Get Calls
Given("I make a GET request to {string}", (url) => {
cy.request("GET", url).as("apiResponse");
});
Then("the response status code should be {int}", (statusCode) => {
cy.get("@apiResponse").should("have.property", "status", statusCode);
});
Then("the response should contain body" , ()=> {
cy.get("@apiResponse").should("have.property", "body");
});
Given("I make a GET request to {string}", (url) => {
cy.request("GET", url).as("apiResponse");
});
Then("the response status code should be {int}", (statusCode) => {
cy.get("@apiResponse").should("have.property", "status", statusCode);
});
Then("the response body should contain the following data:", (dataTable) => {
const expectedData = dataTable.rowsHash();
cy.get("@apiResponse").should("deep.include", expectedData);
});
Feature: Updating Data with PATCH Request
Scenario: Sending a PATCH request to update data
Given I have the endpoint "/posts/1"
And I set the request headers as:
| Content-Type | application/json |
And I set the request body as:
"""
{
"title": "Updated Post Title"
}
"""
When I make a PATCH request
Then I should see a response with status code 200
const { Given, When, Then } = require("cypress-cucumber-preprocessor/steps");
Given('I have the endpoint {string}', (url) => {
cy.wrap(url).as('apiEndpoint');
});
Given('I set the request headers as:', (table) => {
const headers = table.hashes();
cy.wrap(headers).as('requestHeaders');
});
Given('I set the request body as:', (body) => {
cy.wrap(body).as('requestBody');
});
When('I make a PATCH request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.get('@requestHeaders').then((headers) => {
cy.get('@requestBody').then((body) => {
cy.request({
method: 'PATCH',
url,
headers,
body: JSON.parse(body),
}).as('response');
});
});
});
});
Then('I should see a response with status code {int}', (statusCode) => {
cy.get('@response').its('status').should('equal', statusCode);
});
Feature: call the POST API and see the response
using dummy api calls to verify the assertions
Scenario Outline: Verify a successful API GET request against specific post id
Given I have the endpoint "/posts"
And I set the request headers as:
| Content-Type | application/json |
And I set the request body as:
"""
{
"title": "Sample Post",
"body": "This is a sample post.",
"userId": 1
}
"""
When I make a POST request
Then I should see a response with status code 201
Examples:
| postId | statusCode |
| 1 | 200 |
| 2 | 200 |
\ No newline at end of file
const { Given, When, Then } = require("cypress-cucumber-preprocessor/steps");
Given('I have the endpoint {string}', (url) => {
cy.wrap(url).as('apiEndpoint');
});
Given('I set the request headers as:', (table) => {
const headers = table.hashes();
cy.wrap(headers).as('requestHeaders');
});
Given('I set the request body as:', (body) => {
cy.wrap(body).as('requestBody');
});
When('I make a POST request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.get('@requestHeaders').then((headers) => {
cy.get('@requestBody').then((body) => {
cy.request({
method: 'POST',
url,
headers,
body: JSON.parse(body),
}).as('response');
});
});
});
});
Then('I should see a response with status code {int}', (statusCode) => {
cy.get('@response').its('status').should('equal', statusCode);
});
Feature: Performing CRUD Operations on an API
Scenario: Create, Read, Update, and Delete a Resource
Given I have the endpoint "/posts"
And I set the request headers as:
| Content-Type | application/json |
And I set the request body as:
"""
{
"userId": 1,
"id": 1,
"title": "Abdul Sattar",
"body": "Updating the Resource by Abdul Sattar..."
}
"""
When I make a POST request
Then I should see a response with status code 201
And I save the response body as "newPostResponse"
Given I have the endpoint "/posts/1"
When I make a GET request
Then I should see a response with status code 200
When I make a PUT request
Then I should see a response with status code 200
When I make a DELETE request
Then I should see a response with status code 200
const { Given, When, Then } = require("cypress-cucumber-preprocessor/steps");
// Variables to store data between steps
let newPostResponse = null;
Given('I have the endpoint {string}', (url) => {
cy.wrap(url).as('apiEndpoint');
});
Given('I set the request headers as:', (table) => {
const headers = table.hashes();
cy.wrap(headers).as('requestHeaders');
});
Given('I set the request body as:', (body) => {
cy.wrap(body).as('requestBody');
});
When('I make a POST request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.get('@requestHeaders').then((headers) => {
cy.get('@requestBody').then((body) => {
cy.request({
method: 'POST',
url,
headers,
body: JSON.parse(body),
}).as('response');
});
});
});
});
Then('I should see a response with status code {int}', (statusCode) => {
cy.get('@response').its('status').should('equal', statusCode);
});
Then('I save the response body as {string}', (varName) => {
cy.get('@response').then((response) => {
newPostResponse = response.body;
cy.wrap(newPostResponse).as(varName);
});
});
When('I make a GET request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.request({
method: 'GET',
url,
}).as('response');
});
});
When('I make a PUT request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.get('@requestHeaders').then((headers) => {
cy.get('@requestBody').then((body) => {
cy.request({
method: 'PUT',
url,
headers,
body: JSON.parse(body),
}).as('response');
});
});
});
});
When('I make a DELETE request', () => {
cy.get('@apiEndpoint').then((url) => {
cy.request({
method: 'DELETE',
url,
}).as('response');
});
});
/// <reference types="cypress" />
// using https://opensource-demo.orangehrmlive.com/ website to verify the codde
// to Run specific TC then use npx cypress run --spec cypress/e2e/Name
describe('Verify IT blocks', () => {
it('should execute', () => {
cy.visit('https://opensource-demo.orangehrmlive.com/');
cy.title().should('eq','OrangeHRM');
})
})
\ No newline at end of file
//Qesution 1 Reverse String
function reverseString(inputString) {
return inputString.split('').reverse().join('');
}
const originalString = "Automation Task";
const reversedString = reverseString(originalString);
console.log(reversedString);
// Quesiton 2 Logging Multiples
for (let i = 1; i <= 99; i++) {
if (i % 2 === 0 && i % 5 === 0) {
console.log("BOTH");
} else if (i % 2 === 0) {
console.log("TWO");
} else if (i % 5 === 0) {
console.log("FIVE");
} else {
console.log(i);
}
}
\ No newline at end of file
// Quesiton =2
let obj = {
foo: { a: "hello", b: "world" },
bar: ["example", "mem", null, { xyz: 6 }, 88],
qux: [4, 8, 12]
};
function replaceValue(obj, target, replacement) {
if (typeof obj === 'object' && obj !== null) {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
replaceValue(obj[i], target, replacement);
}
} else {
for (const key in obj) {
replaceValue(obj[key], target, replacement);
}
}
} else if (obj === target) {
obj = replacement;
}
}
replaceValue(obj, 6, 606);
console.log(obj);
\ No newline at end of file
// Question 3 part 1
async function getPostCounts() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
const userPostCounts = {};
data.forEach(post => {
const userId = post.userId;
userPostCounts[`user${userId}`] = (userPostCounts[`user${userId}`] || 0) + 1;
});
console.log(userPostCounts);
} catch (error) {
console.error('An error occurred:', error);
}
}
getPostCounts();
// Question 3 part 2
async function fetchAndLogTitles() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
const result = {};
data.forEach(post => {
if (post.title.length < 31) {
result[`user${post.userId}`] = post.title.toUpperCase();
}
});
console.log(result);
} catch (error) {
console.error('An error occurred:', error);
}
}
fetchAndLogTitles();
\ No newline at end of file
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
\ No newline at end of file
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
// Import commands.js using ES2015 syntax:
import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment