diff --git a/package.json b/package.json index af068fe..e08df49 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/src/test/features/UserLogin.feature b/src/test/features/UserLogin.feature new file mode 100644 index 0000000..3575910 --- /dev/null +++ b/src/test/features/UserLogin.feature @@ -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. \ No newline at end of file diff --git a/src/test/hooks/hooks.ts b/src/test/hooks/hooks.ts index faf33a6..f45fd38 100644 --- a/src/test/hooks/hooks.ts +++ b/src/test/hooks/hooks.ts @@ -26,4 +26,5 @@ After(async function ({pickle, result}) { AfterAll(async function () { await pageFixture.page.waitForTimeout(3000); + await browser.close(); }); \ No newline at end of file diff --git a/src/test/pages/AccountRegister.ts b/src/test/pages/AccountRegister.ts index 42492ea..2002eab 100644 --- a/src/test/pages/AccountRegister.ts +++ b/src/test/pages/AccountRegister.ts @@ -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){ @@ -34,19 +37,23 @@ export class AccountRegister { await expect(this.registrationPageLocators.createAccHeading()).toHaveText('Create New Customer Account'); }; - public async enterUserDetails ():Promise { - 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 { + 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.') } }; diff --git a/src/test/pages/UserLogin.ts b/src/test/pages/UserLogin.ts new file mode 100644 index 0000000..a568bef --- /dev/null +++ b/src/test/pages/UserLogin.ts @@ -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{ + await this.userLoginLocators.singInBtn().click(); + }; + + public async userEntersCorrectCredentials():Promise{ + 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{ + await expect(this.userLoginLocators.welcomeMessage()).toContainText('Welcome'); + }; +}; \ No newline at end of file diff --git a/src/test/resources/registrationPage.json b/src/test/resources/registrationPage.json index d2d7bbe..2017a93 100644 --- a/src/test/resources/registrationPage.json +++ b/src/test/resources/registrationPage.json @@ -35,7 +35,7 @@ }, { "elementName": "pageMessage", - "selectorValue": "//div[contains(@class,'pagasde messages')]" + "selectorValue": "//div[contains(@class,'page messages')]" } ] } \ No newline at end of file diff --git a/src/test/resources/userLoginPage.json b/src/test/resources/userLoginPage.json new file mode 100644 index 0000000..475267d --- /dev/null +++ b/src/test/resources/userLoginPage.json @@ -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')]" + } + ] +} \ No newline at end of file diff --git a/src/test/steps/userLogin.ts b/src/test/steps/userLogin.ts new file mode 100644 index 0000000..f041222 --- /dev/null +++ b/src/test/steps/userLogin.ts @@ -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(); +}); \ No newline at end of file