-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split login+create order API calls to separate files
- Loading branch information
1 parent
9da3624
commit ea578ea
Showing
2 changed files
with
31 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
@@ -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(); | ||
}) |