-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding dynamic locator and route to grab productId (#4)
* adding dynamic locator and route to grab productId * moved 2 of the 3 specs to use productPageRoute * updating to make page router get id by name as an option * adding a function on the page that does everything
- Loading branch information
Showing
8 changed files
with
89 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { HomePage } from "@pages"; | ||
|
||
/** | ||
* Sets up a route to retrieve the product ID from the API response. | ||
* @param page - The Playwright page object. | ||
* @param name - Optional name of the string to get the ID for. | ||
* @returns The product ID. | ||
* @example | ||
* const page = await browser.newPage(); | ||
* const productId = await productIdRoute(page); // Gets the second product ID | ||
* | ||
* const productId = await productIdRoute(page, "Pliers") // Gets the ID for the product named "Pliers" | ||
*/ | ||
export async function productIdRoute(page: any, name?: string) { | ||
let productId; | ||
|
||
await page.route( | ||
"https://api.practicesoftwaretesting.com/products?between=price,1,100&page=1", | ||
async (route) => { | ||
let body; | ||
const response = await route.fetch(); | ||
body = await response.json(); | ||
if (name) { | ||
productId = findIdByName(body, name); | ||
console.log("pid: " + productId); | ||
} else { | ||
// Get the second product in the list | ||
productId = body.data[1].id; | ||
} | ||
route.continue(); | ||
} | ||
); | ||
|
||
const homePage = new HomePage(page); | ||
await homePage.goto(); | ||
|
||
return productId; | ||
} | ||
|
||
function findIdByName(json: any, name: string): string | undefined { | ||
const data = json.data; | ||
for (let i = 0; i < data.length; i++) { | ||
if (data[i].name === name) { | ||
return data[i].id; | ||
} | ||
} | ||
return undefined; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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