Skip to content

Commit

Permalink
Merge pull request #14 from maharrshi/first_branch
Browse files Browse the repository at this point in the history
completed the workflow
  • Loading branch information
maharrshi authored Mar 3, 2024
2 parents 439f17a + 5199211 commit f12a888
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 27 deletions.
88 changes: 88 additions & 0 deletions pageModel/amazonPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Page, expect } from "@playwright/test";

export class AmazonPage {
private page: Page;
private cartPage: Page | null;
private wishListPage: Page | null;

constructor(page: Page) {
this.page = page;
this.cartPage = null;
this.wishListPage = null;
}

async goToHomePage() {
await this.page.goto("https://www.amazon.in");
}

async isLoginButtonPresent() {
const loginButton = await this.page.$("#nav-link-accountList");
return loginButton !== null;
}

async searchProduct(productName: string) {
await this.page.fill("#twotabsearchtextbox", productName);
await this.page.keyboard.press("Enter");
await this.page.waitForSelector(
'//div[@data-cy="title-recipe"]//span[text()="Alchemist"]'
);
}

async addProductToCart() {
const bookTitle =
'//div[@data-cy="title-recipe"]//span[text()="Alchemist"]';
const product = await this.page.$(bookTitle);
if (product) {
await product.click();
const [cartPage] = await Promise.all([
await this.page.waitForEvent("popup"),
]);
this.cartPage = cartPage;
await this.cartPage.waitForSelector("#add-to-cart-button");
await this.cartPage.click("#add-to-cart-button");
console.log("Product added to cart successfully.");
} else {
console.error("No product found to add to cart.");
}
}

async proceedToCheckoutPresent() {
if (this.cartPage) {
await this.cartPage.waitForSelector("#sc-buy-box-ptc-button");
await expect(
this.cartPage.locator("#sc-buy-box-ptc-button")
).toBeVisible();
} else {
console.error(
"Cart page is not available. Please add a product to the cart first."
);
}
}

async searchResultsCount() {
const searchResults = await this.page.$$(
'div[data-component-type="s-search-result"]'
);
return searchResults.length;
}

async isWishListButtonPresent() {
const bookTitle =
'//div[@data-cy="title-recipe"]//span[text()="Alchemist"]';
const product = await this.page.$(bookTitle);
if (product) {
await product.click();
const [wishListPage] = await Promise.all([
await this.page.waitForEvent("popup"),
]);
this.wishListPage = wishListPage;

await this.wishListPage.waitForTimeout(3000);
await expect(
this.wishListPage.locator('//a[contains(text(),"Add to Wish List")]')
).toBeVisible();
} else {
console.error("Unable to find Wishlist on the page");
}
}
}
12 changes: 3 additions & 9 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig, devices } from '@playwright/test';
import { defineConfig, devices,PlaywrightTestConfig } from '@playwright/test';

/**
* Read environment variables from file.
Expand All @@ -9,6 +9,7 @@ import { defineConfig, devices } from '@playwright/test';
/**
* See https://playwright.dev/docs/test-configuration.
*/

export default defineConfig({
testDir: './tests',
/* Run tests in files in parallel */
Expand All @@ -27,6 +28,7 @@ export default defineConfig({
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
headless:false,
trace: 'on-first-retry',
},

Expand All @@ -37,15 +39,7 @@ export default defineConfig({
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
Expand Down
33 changes: 33 additions & 0 deletions tests/amazonWorkflow.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { test, expect } from "@playwright/test";
import { AmazonPage } from "../pageModel/amazonPage";

test("Amazon Page", async ({ page }) => {
const amazonPage = new AmazonPage(page);

try {
await amazonPage.goToHomePage();

// Validate Login
const isLoginButtonPresent = await amazonPage.isLoginButtonPresent();
expect(isLoginButtonPresent).toBeTruthy();

// Product Checkout
await amazonPage.searchProduct("the alchemist by paulo coelho");
await amazonPage.addProductToCart();

//Validate Proceed to checkout
await amazonPage.proceedToCheckoutPresent();

// Search Functionality
const searchQuery = "the alchemist by paulo coelho";
await page.bringToFront();
await amazonPage.searchProduct(searchQuery);
const searchResultsCount = await amazonPage.searchResultsCount();
console.log(`Search results for '${searchQuery}': ${searchResultsCount}`);

// Validate Wishlist
await amazonPage.isWishListButtonPresent();
} catch (error) {
console.error("Error occurred during test execution:", error);
}
});
18 changes: 0 additions & 18 deletions tests/example.spec.ts

This file was deleted.

0 comments on commit f12a888

Please sign in to comment.