225 lines
7.7 KiB
JavaScript
225 lines
7.7 KiB
JavaScript
|
|
const RegistrationPage = require('../pages/RegistrationPage');
|
||
|
|
const LoginPage = require('../pages/LoginPage');
|
||
|
|
const DashboardPage = require('../pages/DashboardPage');
|
||
|
|
const users = require('../fixtures/users.json');
|
||
|
|
|
||
|
|
describe('Authentication E2E Tests', () => {
|
||
|
|
let driver;
|
||
|
|
let registrationPage;
|
||
|
|
let loginPage;
|
||
|
|
let dashboardPage;
|
||
|
|
|
||
|
|
beforeAll(async () => {
|
||
|
|
driver = await global.createDriver();
|
||
|
|
registrationPage = new RegistrationPage(driver);
|
||
|
|
loginPage = new LoginPage(driver);
|
||
|
|
dashboardPage = new DashboardPage(driver);
|
||
|
|
});
|
||
|
|
|
||
|
|
afterAll(async () => {
|
||
|
|
await global.quitDriver(driver);
|
||
|
|
});
|
||
|
|
|
||
|
|
beforeEach(async () => {
|
||
|
|
// Navigate to home page before each test to ensure clean state
|
||
|
|
await driver.get(global.testConfig.baseUrl);
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('User Registration', () => {
|
||
|
|
test('should successfully register a new user', async () => {
|
||
|
|
const testUser = users.registrationTestUser;
|
||
|
|
|
||
|
|
await registrationPage.navigateToRegistration();
|
||
|
|
|
||
|
|
expect(await registrationPage.isRegistrationFormDisplayed()).toBe(true);
|
||
|
|
expect(await registrationPage.getRegistrationHeading()).toBe('Register');
|
||
|
|
|
||
|
|
await registrationPage.register(
|
||
|
|
testUser.name,
|
||
|
|
testUser.email,
|
||
|
|
testUser.password
|
||
|
|
);
|
||
|
|
|
||
|
|
await registrationPage.waitForSuccessfulRegistration();
|
||
|
|
|
||
|
|
const successMessage = await registrationPage.getSuccessMessage();
|
||
|
|
expect(successMessage).toContain('Registration successful');
|
||
|
|
expect(await registrationPage.isFormCleared()).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show validation errors for invalid registration data', async () => {
|
||
|
|
const invalidUser = users.invalidUsers.shortPassword;
|
||
|
|
|
||
|
|
await registrationPage.navigateToRegistration();
|
||
|
|
await registrationPage.register(
|
||
|
|
invalidUser.name,
|
||
|
|
invalidUser.email,
|
||
|
|
invalidUser.password
|
||
|
|
);
|
||
|
|
|
||
|
|
await registrationPage.waitForRegistrationError();
|
||
|
|
|
||
|
|
// Check that form shows validation errors
|
||
|
|
const hasErrors = await registrationPage.hasPasswordFieldError() ||
|
||
|
|
await registrationPage.getPasswordErrorMessage() !== null ||
|
||
|
|
await registrationPage.getGeneralErrorMessage() !== null;
|
||
|
|
|
||
|
|
expect(hasErrors).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show error for password mismatch', async () => {
|
||
|
|
const mismatchUser = users.invalidUsers.passwordMismatch;
|
||
|
|
|
||
|
|
await registrationPage.navigateToRegistration();
|
||
|
|
await registrationPage.register(
|
||
|
|
mismatchUser.name,
|
||
|
|
mismatchUser.email,
|
||
|
|
mismatchUser.password,
|
||
|
|
mismatchUser.passwordConfirmation
|
||
|
|
);
|
||
|
|
|
||
|
|
await registrationPage.waitForRegistrationError();
|
||
|
|
|
||
|
|
const hasPasswordConfirmationError = await registrationPage.hasPasswordConfirmationFieldError() ||
|
||
|
|
await registrationPage.getPasswordConfirmationErrorMessage() !== null;
|
||
|
|
|
||
|
|
expect(hasPasswordConfirmationError).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show error for empty required fields', async () => {
|
||
|
|
await registrationPage.navigateToRegistration();
|
||
|
|
await registrationPage.clickSubmit();
|
||
|
|
|
||
|
|
// The form should prevent submission with empty required fields
|
||
|
|
// or show validation errors
|
||
|
|
const submitButtonText = await registrationPage.getSubmitButtonText();
|
||
|
|
expect(submitButtonText).toBe('Register'); // Button text shouldn't change to "Registering..."
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show error for invalid email format', async () => {
|
||
|
|
const invalidEmailUser = users.invalidUsers.invalidEmail;
|
||
|
|
|
||
|
|
await registrationPage.navigateToRegistration();
|
||
|
|
await registrationPage.register(
|
||
|
|
invalidEmailUser.name,
|
||
|
|
invalidEmailUser.email,
|
||
|
|
invalidEmailUser.password
|
||
|
|
);
|
||
|
|
|
||
|
|
// The HTML5 email validation should prevent form submission
|
||
|
|
// or backend should return validation error
|
||
|
|
const currentUrl = await registrationPage.getCurrentUrl();
|
||
|
|
expect(currentUrl).toContain('/register');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('User Login', () => {
|
||
|
|
test('should successfully login with valid credentials', async () => {
|
||
|
|
const validCredentials = users.loginTestCases.validCredentials;
|
||
|
|
|
||
|
|
await loginPage.navigateToLogin();
|
||
|
|
|
||
|
|
expect(await loginPage.isLoginFormDisplayed()).toBe(true);
|
||
|
|
expect(await loginPage.getLoginHeading()).toBe('Login');
|
||
|
|
|
||
|
|
await loginPage.login(
|
||
|
|
validCredentials.email,
|
||
|
|
validCredentials.password
|
||
|
|
);
|
||
|
|
|
||
|
|
await loginPage.waitForSuccessfulLogin();
|
||
|
|
|
||
|
|
// Should see dashboard after successful login
|
||
|
|
const dashboardVisible = await dashboardPage.isDashboardDisplayed();
|
||
|
|
expect(dashboardVisible).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show error for invalid credentials', async () => {
|
||
|
|
const invalidCredentials = users.loginTestCases.invalidCredentials;
|
||
|
|
|
||
|
|
await loginPage.navigateToLogin();
|
||
|
|
await loginPage.login(
|
||
|
|
invalidCredentials.email,
|
||
|
|
invalidCredentials.password
|
||
|
|
);
|
||
|
|
|
||
|
|
await loginPage.waitForLoginError();
|
||
|
|
|
||
|
|
const generalError = await loginPage.getGeneralErrorMessage();
|
||
|
|
expect(generalError).toContain('Invalid email or password');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show validation errors for empty fields', async () => {
|
||
|
|
await loginPage.navigateToLogin();
|
||
|
|
await loginPage.clickSubmit();
|
||
|
|
|
||
|
|
// The form should prevent submission with empty required fields
|
||
|
|
const submitButtonText = await loginPage.getSubmitButtonText();
|
||
|
|
expect(submitButtonText).toBe('Login'); // Button text shouldn't change to "Logging in..."
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should show error for invalid email format', async () => {
|
||
|
|
const invalidEmailCredentials = users.loginTestCases.invalidEmailFormat;
|
||
|
|
|
||
|
|
await loginPage.navigateToLogin();
|
||
|
|
await loginPage.login(
|
||
|
|
invalidEmailCredentials.email,
|
||
|
|
invalidEmailCredentials.password
|
||
|
|
);
|
||
|
|
|
||
|
|
// The HTML5 email validation should prevent form submission
|
||
|
|
// or backend should return validation error
|
||
|
|
const currentUrl = await loginPage.getCurrentUrl();
|
||
|
|
expect(currentUrl).toContain('/login');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('should disable submit button while login is in progress', async () => {
|
||
|
|
const validCredentials = users.loginTestCases.validCredentials;
|
||
|
|
|
||
|
|
await loginPage.navigateToLogin();
|
||
|
|
await loginPage.enterEmail(validCredentials.email);
|
||
|
|
await loginPage.enterPassword(validCredentials.password);
|
||
|
|
|
||
|
|
// Click submit and immediately check if button is disabled
|
||
|
|
await loginPage.clickSubmit();
|
||
|
|
|
||
|
|
// Check if button text changes to "Logging in..." (indicating loading state)
|
||
|
|
const submitButtonText = await loginPage.getSubmitButtonText();
|
||
|
|
const isButtonDisabled = await loginPage.isSubmitButtonDisabled();
|
||
|
|
|
||
|
|
expect(submitButtonText === 'Logging in...' || isButtonDisabled).toBe(true);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('Authentication Flow Integration', () => {
|
||
|
|
test('should allow registration followed by immediate login', async () => {
|
||
|
|
// Use a unique email for this test to avoid conflicts
|
||
|
|
const timestamp = Date.now();
|
||
|
|
const testUser = {
|
||
|
|
name: 'Integration Test User',
|
||
|
|
email: `integration.test.${timestamp}@example.com`,
|
||
|
|
password: 'password123'
|
||
|
|
};
|
||
|
|
|
||
|
|
// Register new user
|
||
|
|
await registrationPage.navigateToRegistration();
|
||
|
|
await registrationPage.register(
|
||
|
|
testUser.name,
|
||
|
|
testUser.email,
|
||
|
|
testUser.password
|
||
|
|
);
|
||
|
|
|
||
|
|
await registrationPage.waitForSuccessfulRegistration();
|
||
|
|
|
||
|
|
// Navigate to login and use the same credentials
|
||
|
|
await loginPage.navigateToLogin();
|
||
|
|
await loginPage.login(testUser.email, testUser.password);
|
||
|
|
|
||
|
|
await loginPage.waitForSuccessfulLogin();
|
||
|
|
|
||
|
|
// Should be successfully logged in
|
||
|
|
const currentUrl = await loginPage.getCurrentUrl();
|
||
|
|
expect(currentUrl).not.toContain('/login');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|