Skip to content

Commit 78559ab

Browse files
committed
Test Case of Basic Auth & Broken Images
1 parent 94865eb commit 78559ab

3 files changed

Lines changed: 60 additions & 16 deletions

File tree

constants/configuration.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// URL'S
22
export const URLs = {
3-
URL:'https://the-internet.herokuapp.com/',
4-
ADD_REMOVE_ELEMENTS: '/add_remove_elements',
5-
BASIC_AUTH: '/basic_auth'
6-
7-
};
3+
URL: 'https://the-internet.herokuapp.com/',
4+
ADD_REMOVE_ELEMENTS: '/add_remove_elements/',
5+
BASIC_AUTH: '/basic_auth',
6+
BROKEN_IMAGES: '/broken_images'
7+
};

playwright.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default defineConfig({
1818
/* Fail the build on CI if you accidentally left test.only in the source code. */
1919
forbidOnly: !!process.env.CI,
2020
/* Retry on CI only */
21-
retries: process.env.CI ? 2 : 0,
21+
retries: 0,
2222
/* Opt out of parallel tests on CI. */
23-
workers: process.env.CI ? 1 : undefined,
23+
workers: 1,
2424
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
2525
reporter: 'html',
2626
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */

tests/TC01.spec.ts

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,61 @@ test('Test Add/Remove Elements', async ({ page }) => {
1515
await page.locator(".example button:has-text('Add Element')").waitFor({ state: 'visible' });
1616
await page.locator(".example button:has-text('Add Element')").click();
1717
await page.locator("#elements button:has-text('Delete')").click();
18-
await expect(await page.locator(".added-manually").count()).toBeGreaterThanOrEqual(0);
18+
19+
const count = await page.locator(".added-manually").count();
20+
expect(count).toBeGreaterThanOrEqual(0);
21+
});
22+
23+
test('Test login with basic auth', async ({ browser }) => {
24+
25+
// When Loging in with Basic Auth
26+
const context = await browser.newContext({
27+
httpCredentials: {
28+
username: 'admin',
29+
password: 'admin',
30+
}
31+
});
32+
33+
const page = await context.newPage();
34+
await page.goto(URLs.BASIC_AUTH); // site that triggers basic auth
35+
await expect(page).toHaveURL(URLs.BASIC_AUTH); // or any assertion
36+
await expect(page.locator('#content p')).toHaveText('Congratulations! You must have the proper credentials.');
37+
});
38+
39+
test('Test cancel on basic auth', async ({ browser }) => {
40+
const context = await browser.newContext({
41+
httpCredentials: {
42+
username: 'wronguser',
43+
password: 'wrongpass',
44+
}
45+
});
46+
47+
const page = await context.newPage();
48+
await page.goto(URLs.BASIC_AUTH);
49+
50+
// The page will show a 401 error or some unauthorized message
51+
await expect(page.locator('body')).toContainText('Not authorized'); // Adjust this
1952
});
2053

21-
test('Test Basic Auth', async ({ page }) => {
54+
test('Test Broken Images', async ({ page }) => {
2255
await page.goto('/');
23-
await page.locator("a:has-text('Basic Auth')").click();
24-
await expect(page).toHaveURL(URLs.BASIC_AUTH);
56+
await page.locator("a:has-text('Broken Images')").click();
57+
await expect(page).toHaveURL(URLs.BROKEN_IMAGES);
2558

26-
await expect(page.locator(".example button:has-text('Add Element')")).toBeVisible();
27-
await page.locator(".example button:has-text('Add Element')").waitFor({ state: 'visible' });
28-
await page.locator(".example button:has-text('Add Element')").click();
29-
await page.locator("#elements button:has-text('Delete')").click();
30-
await expect(await page.locator(".added-manually").count()).toBeGreaterThanOrEqual(0);
59+
const images = page.locator('img');
60+
const count = await images.count();
61+
62+
let brokenCount = 0;
63+
64+
for (let i = 0; i < count; i++) {
65+
const img = images.nth(i);
66+
const isBroken = await img.evaluate((imgEl) => {
67+
return (imgEl as HTMLImageElement).naturalWidth === 0;
68+
});
69+
70+
console.log(`Image ${i + 1}: ${isBroken ? 'BROKEN' : 'OK'}`);
71+
if (isBroken) brokenCount++;
72+
}
73+
74+
expect(brokenCount).toBeGreaterThan(0);
3175
});

0 commit comments

Comments
 (0)