Skip to content

Code refactor #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "cucumber-js --config=config/cucumber.js --tags @RegisterUser"
"test": "cucumber-js --config=config/cucumber.js"
},
"keywords": [],
"author": "",
Expand Down
13 changes: 13 additions & 0 deletions src/test/features/UserLogin.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@LoginUser

Feature: Verify that the user is able to login into an already registered account.

User enters their details to login.

Background: User is landed on the webpage.
Given The user lands at the webpage.

Scenario: User is able to login with correct credentials.
Given The user clicks on the Sign In button on the header.
When The user enters correct credentials.
Then The user is logged in.
1 change: 1 addition & 0 deletions src/test/hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ After(async function ({pickle, result}) {

AfterAll(async function () {
await pageFixture.page.waitForTimeout(3000);
await browser.close();
});
23 changes: 15 additions & 8 deletions src/test/pages/AccountRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { Page, expect } from "@playwright/test";
import { pageFixture } from "../hooks/pageFixture";
import { PageElement } from "../resources/interfaces/iPageElement";
import * as registrationPageLocators from "../resources/registrationPage.json";
import { LoginUser } from "./UserLogin";

function getResource(resourceName: string) {
return registrationPageLocators.webElements.find((element: PageElement) => element.elementName == resourceName) as PageElement;
};

let userLogin = new LoginUser(pageFixture.page);

export class AccountRegister {

constructor (public page: Page){
Expand Down Expand Up @@ -34,19 +37,23 @@ export class AccountRegister {
await expect(this.registrationPageLocators.createAccHeading()).toHaveText('Create New Customer Account');
};

public async enterUserDetails ():Promise<any> {
await this.registrationPageLocators.firstName().fill('Test');
await this.registrationPageLocators.lastName().fill('Test 2');
await this.registrationPageLocators.emailAddress().first().fill('testemail@test.com')
public async enterUserDetails ():Promise<void> {
await this.registrationPageLocators.firstName().fill('Test 2');
await this.registrationPageLocators.lastName().fill('Test 3');
await this.registrationPageLocators.emailAddress().first().fill('tslldasddas@test.com')
await this.registrationPageLocators.password().first().fill('Te345435345345!@#!@#st');
await this.registrationPageLocators.confirmPass().fill('Te345435345345!@#!@#st');
await pageFixture.page.keyboard.press('PageDown');
await this.registrationPageLocators.createAccConfirmBtn().first().click();
const pageTextElement = this.registrationPageLocators.pageMessage();
if (await pageTextElement.isVisible()) {
console.log('True');
await pageFixture.page.keyboard.press('PageUp');
const pageAssert = await this.registrationPageLocators.pageMessage().innerText();
if (pageAssert.includes('There is already an account with this email address.')) {
await userLogin.goToSignIn();
await userLogin.userEntersCorrectCredentials();
await userLogin.assertUserIsLoggedIn();
} else {
console.log('False');
console.log('Else is running')
expect(this.registrationPageLocators.pageMessage()).toHaveText('Thank you for registering with Main Website Store.')
}

};
Expand Down
38 changes: 38 additions & 0 deletions src/test/pages/UserLogin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { pageFixture } from "../hooks/pageFixture";
import * as userLoginPage from "../../../src/test/resources/userLoginPage.json"
import * as registrationPage from "../../../src/test/resources/registrationPage.json"
import { PageElement } from "../resources/interfaces/iPageElement";
import { Page, expect } from "@playwright/test";

function getResource(resourceName: string) {
return userLoginPage.webElements.find((element: PageElement) => element.elementName == resourceName) as PageElement;
};

export class LoginUser {

constructor (public page: Page){
pageFixture.page = page;
};

userLoginLocators = {
singInBtn:() => pageFixture.page.locator(getResource('signInPageBtn').selectorValue),
emailAddress:() => pageFixture.page.locator(getResource('emailAddress').selectorValue),
password:() => pageFixture.page.locator(getResource('password').selectorValue),
signInUserBtn:() => pageFixture.page.locator(getResource('createAccountConfirmBtn').selectorValue),
welcomeMessage:() => pageFixture.page.locator(getResource('welcomeMessage').selectorValue).first()
};

public async goToSignIn():Promise<void>{
await this.userLoginLocators.singInBtn().click();
};

public async userEntersCorrectCredentials():Promise<void>{
await this.userLoginLocators.emailAddress().first().fill('testemail@test.com');
await this.userLoginLocators.password().first().fill('Te345435345345!@#!@#st');
await this.userLoginLocators.signInUserBtn().first().click();
};

public async assertUserIsLoggedIn():Promise<void>{
await expect(this.userLoginLocators.welcomeMessage()).toContainText('Welcome');
};
};
2 changes: 1 addition & 1 deletion src/test/resources/registrationPage.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
{
"elementName": "pageMessage",
"selectorValue": "//div[contains(@class,'pagasde messages')]"
"selectorValue": "//div[contains(@class,'page messages')]"
}
]
}
25 changes: 25 additions & 0 deletions src/test/resources/userLoginPage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "Login User Locators",
"webElements": [
{
"elementName": "signInPageBtn",
"selectorValue": "//div[contains(@class,'panel header')]//ul//li[contains(.,'Sign In')]"
},
{
"elementName": "emailAddress",
"selectorValue": "//input[contains(@id,'email')]"
},
{
"elementName": "password",
"selectorValue": "//input[contains(@id,'pass')]"
},
{
"elementName": "createAccountConfirmBtn",
"selectorValue": "//div[contains(@class,'primary')]//button[contains(@type,'submit')]"
},
{
"elementName": "welcomeMessage",
"selectorValue": "//span[contains(@class,'logged-in')]"
}
]
}
18 changes: 18 additions & 0 deletions src/test/steps/userLogin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Given, Then, When, setDefaultTimeout } from "@cucumber/cucumber";
import { LoginUser } from "../pages/UserLogin";
import { pageFixture } from "../hooks/pageFixture";

setDefaultTimeout(60000);
let userLogin = new LoginUser(pageFixture.page);

Given("The user clicks on the Sign In button on the header.", async function () {
await userLogin.goToSignIn();
});

When("The user enters correct credentials.", async function (){
await userLogin.userEntersCorrectCredentials();
});

Then("The user is logged in.", async function (){
await userLogin.assertUserIsLoggedIn();
});
Loading