Skip to content

Commit

Permalink
ApiLogin
Browse files Browse the repository at this point in the history
  • Loading branch information
An5hul-Choudhary committed Apr 15, 2024
1 parent fae379e commit 316b50d
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/ApiTestFirst.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const {test, expect, request} = require('@playwright/test')

const requestPayload = {userEmail: "anshika222@gmail.com", userPassword: "Iamking@000"};
let token;
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);
})

test("Login Using API", async ({page}) => {
//js file- Login js, DashboardPage

//Hitting login via api and storing token in dev console under application tab local storage

await page.addInitScript(value => {

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

const email = "anshika222@gmail.com";
const productName = 'ADIDAS ORIGINAL';

await page.goto("https://rahulshettyacademy.com/client/");
// await page.locator("#userEmail").fill(email);
// 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);
const count = await products.count();
for (let i = 0; i < count; ++i) {
if (await products.nth(i).locator("b").textContent() === productName) {
//add to cart
await products.nth(i).locator("text= Add To Cart").click();
break;
}
}

await page.locator("[routerlink*='cart']").click();
//await page.pause();

await page.locator("div li").first().waitFor();
const bool = await page.locator("h3:has-text('ADIDAS ORIGINAL')").isVisible();
expect(bool).toBeTruthy();
await page.locator("text=Checkout").click();

await page.locator("[placeholder*='Country']").pressSequentially("ind");

const dropdown = page.locator(".ta-results");
await dropdown.waitFor();
const optionsCount = await dropdown.locator("button").count();
for (let i = 0; i < optionsCount; ++i) {
const text = await dropdown.locator("button").nth(i).textContent();
if (text === " India") {
await dropdown.locator("button").nth(i).click();
break;
}
}

await expect(page.locator(".user__name [type='text']").first()).toHaveText(email);
await page.locator(".action__submit").click();
await expect(page.locator(".hero-primary")).toHaveText(" Thankyou for the order. ");
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)) {
await rows.nth(i).locator("button").first().click();
break;
}
}
const orderIdDetails = await page.locator(".col-text").textContent();
expect(orderId.includes(orderIdDetails)).toBeTruthy();


});

0 comments on commit 316b50d

Please sign in to comment.