add tests for login using page object

This commit is contained in:
nabim777 2023-10-18 11:38:45 +05:45
parent 9aac99cbaf
commit 537ac2b37a
No known key found for this signature in database
GPG Key ID: BD68084BD2B283E7
4 changed files with 35 additions and 15 deletions

View File

@ -9,7 +9,7 @@ setDefaultTimeout(60000)
BeforeAll(async function () {
global.browser = await chromium.launch({
headless: false,
slowMo: 0,
slowMo: 1000,
});
});

View File

@ -0,0 +1,25 @@
class LoginPage {
constructor() {
//url
this.url = 'http://localhost:8080'
this.loginUrl = 'http://localhost:8080/login?redirect=%2Ffiles%2F'
this.fileUrl = this.url + '/files/'
//define selectors
this.usernameSelector = '//input[@placeholder="Username"]'
this.passwordSelector = '//input[@placeholder="Password"]'
this.loginSelector = '//input[@type="submit"]'
}
async goToLoginUrl() {
await page.goto(this.url);
}
async login(username, password) {
await page.fill(this.usernameSelector, username);
await page.fill(this.passwordSelector, password);
await page.click(this.loginSelector);
}
}
module.exports = LoginPage

View File

@ -2,27 +2,22 @@ const {Given, When, Then} = require('@cucumber/cucumber')
// import expect for assertion
const { expect } = require("@playwright/test");
//launch url
const url = 'http://localhost:8080'
//import assert
const assert = require("assert")
//define selectors
const usernameSelector = '//input[@placeholder="Username"]'
const passwordSelector = '//input[@placeholder="Password"]'
const loginSelector = '//input[@type="submit"]'
const loginUrl = 'http://localhost:8080/login?redirect=%2Ffiles%2F'
const fileUrl = 'http://localhost:8080/files/'
//import page
const LoginPage = require("../pageObjects/LoginPage.js")
const loginPage = new LoginPage;
Given('the user has browsed to the login page', async function () {
await page.goto(url);
await expect(page).toHaveURL(loginUrl);
await loginPage.goToLoginUrl();
await expect(page).toHaveURL(loginPage.loginUrl);
});
When('user logs in with username {string} and password {string}', async function (username, password) {
await page.fill(usernameSelector, username);
await page.fill(passwordSelector, password);
await page.click(loginSelector);
await loginPage.login(username,password);
});
Then('user should redirect to the homepage', async function () {
await expect(page).toHaveURL(fileUrl);
await expect(page).toHaveURL(loginPage.fileUrl);
});