Skip to content

Commit

Permalink
split login+create order API calls to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi-Fogel committed Aug 29, 2024
1 parent 9da3624 commit ea578ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
37 changes: 19 additions & 18 deletions tests/utils/APiUtils.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
class APIUtils{

baseURL = "https://rahulshettyacademy.com";
loginURL = baseURL + "/api/ecom/auth/login";
landingURL = baseURL + "/client";
createOrderURL = baseURL + "/api/ecom/order/create-order";

class APiUtils{


constructor(apiContext)
constructor(apiContext, loginPayload)
{
this.apiContext = apiContext; //context is class-level
this.loginPayload = loginPayload;
this.baseURL = "https://rahulshettyacademy.com";
this.loginURL = this.baseURL + "/api/ecom/auth/login";
this.landingURL = this.baseURL + "/client";
this.createOrderURL = this.baseURL + "/api/ecom/order/create-order";
}

async getToken(){
const loginResponse = await this.apiContext.post(loginURL, {
const loginResponse = await this.apiContext.post(this.loginURL, {
data: this.loginPayload
});
// expect(loginResponse.ok()).toBeTruthy();
const loginResponseJson = await loginResponse.json();
token = loginResponseJson.token;
const token = loginResponseJson.token;
console.log("Token is: " + token);
return token;
};

async getOrder(orderPayload){
const orderResponse = await this.apiContext.post(createOrderURL, {
async createOrder(orderPayload){
let response = {};
response.token = await this.getToken();

const orderResponse = await this.apiContext.post(this.createOrderURL, {
data: orderPayload,
headers: {
'Authorization': this.getToken(),
'Authorization': response.token,
'Content-Type' : 'application/json'
},
})
const orderResponseJson = await orderResponse.json();
console.log(orderResponseJson);
orderID = orderResponseJson.orders[0];
console.log("grabbed order id: "+ orderID);
return orderID;
const orderId = orderResponseJson.orders[0];
// console.log("grabbed order id: "+ orderID);
response.orderId = orderId;
return response;
}
}

module.exports = {APIUtils};
module.exports = {APiUtils};
26 changes: 12 additions & 14 deletions tests/webApiPart1.spec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
const {test, expect, request} = require('@playwright/test');
const {ApiUtils} = require('./utils/APiUtils');
const {APiUtils} = require('./utils/APiUtils');

const loginPayload = {userEmail:"[email protected]",userPassword:"Password1"};
const orderPayload = {orders:[{country:"Romania",productOrderedId:"6581ca399fd99c85e8ee7f45"}]};

let orderID;
let token;
// let orderID;
// let token;
let response;

test.beforeAll(async () => {
const apiContext = await request.newContext();
const apiUtils = new ApiUtils(apiContext, loginPayload);
apiUtils.createOrder(orderPayload);
const apiUtils = new APiUtils(apiContext, loginPayload);
response = await apiUtils.createOrder(orderPayload);
});

// test.beforeEach( ()=>{
// //code
// })

test('@API Place the order', async ({page})=>
{
const apiUtils = new ApiUtils(apiContext, loginPayload);
const orderId = crateOrder(orderPayload);
{

await page.addInitScript(value => {
window.localStorage.setItem('token',value);
}, token );
await page.goto(landingURL);
}, response.token );
await page.goto("https://rahulshettyacademy.com/client");


const products = page.locator(".card-body");
Expand All @@ -37,15 +36,14 @@ test('@API Place the order', async ({page})=>
await page.locator("tbody").waitFor(); //waiting for whole table-body to loadup, prior to making the evaluation #async breakage
const rows = await page.locator("tbody tr"); //waiting for table data to be loaded up

for(let i=0; i<await rows.count(); i++){
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();
})

0 comments on commit ea578ea

Please sign in to comment.