Skip to content

Commit

Permalink
ApiUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
An5hul-Choudhary committed Apr 16, 2024
1 parent 316b50d commit ae0e53b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
31 changes: 17 additions & 14 deletions tests/ApiTestFirst.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const {test, expect, request} = require('@playwright/test')

const {ApiUtils} = require('./Utils/ApiUtils')
const requestPayload = {userEmail: "anshika222@gmail.com", userPassword: "Iamking@000"};
let token;
const orderApiRequestPayload = {orders: [{country: "Cuba", productOrderedId: "6581ca979fd99c85e8ee7faf"}]};
let orderId;

let response;
test.beforeAll( async() => {
const apiContext = await request.newContext();
const loginResponse = await apiContext.post("https://rahulshettyacademy.com/api/ecom/auth/login",
{
data: requestPayload
})
expect(loginResponse.ok()).toBeTruthy();
const loginResponseJson = await loginResponse.json();
token = loginResponseJson.token;

console.log(token);
})
const apiContext = await request.newContext();
const apiUtils = new ApiUtils(apiContext, requestPayload);
response = await apiUtils.createOrder(orderApiRequestPayload);

});

test("Login Using API", async ({page}) => {
//js file- Login js, DashboardPage
Expand All @@ -23,7 +22,7 @@ test("Login Using API", async ({page}) => {
await page.addInitScript(value => {

window.localStorage.setItem('token', value);
}, token);
}, response.token);

const email = "anshika222@gmail.com";
const productName = 'ADIDAS ORIGINAL';
Expand All @@ -33,6 +32,8 @@ test("Login Using API", async ({page}) => {
// await page.locator("#userPassword").fill("Iamking@000");
// await page.locator("[value='Login']").click();
// await page.waitForLoadState('networkidle');

/*
const products = page.locator(".card-body");
const titles = await page.locator(".card-body b").allTextContents();
console.log(titles);
Expand Down Expand Up @@ -72,20 +73,22 @@ test("Login Using API", async ({page}) => {
const orderId = await page.locator(".em-spacer-1 .ng-star-inserted").textContent();
console.log(orderId);
*/

await page.locator("button[routerlink*='myorders']").click();
await page.locator("tbody").waitFor();
const rows = await page.locator("tbody tr");


for (let i = 0; i < await rows.count(); ++i) {
const rowOrderId = await rows.nth(i).locator("th").textContent();
if (orderId.includes(rowOrderId)) {
if (response.orderId.includes(rowOrderId)) {
await rows.nth(i).locator("button").first().click();
break;
}
}
const orderIdDetails = await page.locator(".col-text").textContent();
expect(orderId.includes(orderIdDetails)).toBeTruthy();
expect(response.orderId.includes(orderIdDetails)).toBeTruthy();


});
42 changes: 42 additions & 0 deletions tests/Utils/ApiUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class ApiUtils
{

constructor(apiContext, requestPayload){
this.apiContext = apiContext;
this.requestPayload = requestPayload;
}

async getToken(){
const loginResponse = await this.apiContext.post("https://rahulshettyacademy.com/api/ecom/auth/login",
{
data: this.requestPayload
})
// expect(loginResponse.ok()).toBeTruthy();
const loginResponseJson = await loginResponse.json();
const token = loginResponseJson.token;

console.log(token);
return token;
}

async createOrder(orderApiRequestPayload){
let response = {};
response.token = await this.getToken();
const OrderResponse = await this.apiContext.post("https://rahulshettyacademy.com/api/ecom/order/create-order",
{
data: orderApiRequestPayload,
headers: {
'Authorization' : response.token,
'Content-Type' : 'application/json'
}
})
const OrderResponseJson = await OrderResponse.json();
console.log(OrderResponseJson);
const orderId = OrderResponseJson.orders[0];
response.orderId = orderId;
return orderId;

}
}

module.exports = {ApiUtils};

0 comments on commit ae0e53b

Please sign in to comment.