project 2
project 2
AIM:
To build a simple testing project implementing page object model using
selenium.
PROCEDURE:
● Create two sample web pages for dashboard and login page
(SamplePOM.html and SampleLogin.html).
● Create page object classes for each web page under the PageObjectModel
package.
● Define methods in each page object class to interact with web elements using
WebDriver commands.
● Create a test class named TestExample under the PageObjectModel
package.
● Initialize the WebDriver (ChromeDriver) instance in the main method of
TestExample class.
● Use WebDriver to navigate to the URL of the application.
● Interact with Web Elements:
● Use methods from page object classes to interact with web elements on
the pages.
● Retrieve and print the welcome message from the dashboard page. ●
Click on the login button to navigate to the login page.
● Enter Credentials and Login:
● Enter username and password using methods from the LoginPage
class.
● Click on the login button to submit the credentials.
PROJECT CODE:
SamplePOM.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
width: 300px;
margin: 100px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
margin-bottom: 20px;
}
.welcome-message {
text-align: center;
font-size: 18px;
color: #333;
margin-bottom: 20px;
}
.logout-btn {
width: 100%;
padding: 10px;
background-color: #f44336;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.logout-btn:hover {
background-color: #d32f2f;
}
</style></head>
<body>
<div class="container">
<h2>Welcome to Dashboard</h2>
<div class="welcome-message">
<p>Welcome to POM!</p>
</div>
<button class="login-btn" onclick="login()">Login</button>
</div>
<script>
function login() {
// Perform login action, such as redirecting to the login page
window.location.href =
"file:///C://Users//Snowvita%20B//Downloads//PageObjectModel//SampleLogin.html";
}
</script>
</body>
</html>
SampleLogin.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
width: 300px;
margin: 100px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
input[type="text"], input[type="password"], input[type="submit"] {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #4caf50;
color: white;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
<form id="loginForm" action="/login" method="post">
<input type="text" id="username" name="username"
placeholder="Username" required><br>
<input type="password" id="password" name="password"
placeholder="Password" required><br>
<input type="submit" value="Login" id = "login-button">
</form>
</div>
</body>
</html>
TESTING CODE (PAGE OBJECT MODEL):
DashboardPage.java
package PageObjectModel;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class DashboardPage {
private WebDriver driver;
private By welcomeMessage = By.className("welcome-message");
private By login = By.className("login-btn");
public DashboardPage(WebDriver driver) {
this.driver = driver;
}
public String getWelcomeMessage() {
return driver.findElement(welcomeMessage).getText(); }
public void clickLogin(){
driver.findElement(login).click();
}
}
LoginPage.java
package PageObjectModel;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
private WebDriver driver;
private By usernameInput = By.id("username");
private By passwordInput = By.id("password");
private By loginButton = By.id("login-button");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String username) {
driver.findElement(usernameInput).sendKeys(username);
}
public void enterPassword(String password) {
driver.findElement(passwordInput).sendKeys(password);
}
public void clickLoginButton() {
driver.findElement(loginButton).click();
driver.quit();
}
}
TestExample.java
package PageObjectModel;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestExample {
public static void main(String[] args) {
// Initialize ChromeDriver
WebDriver driver = new ChromeDriver();
dashboardPage.clickLogin();
RESULT:
A simple testing project implementing page object model using selenium is built
and executed successfully.