Commit 0e1105f1 authored by Qazi Zain's avatar Qazi Zain

Page object model code is added in repo in scr/main

parent 95fdb042
......@@ -46,6 +46,8 @@ pom.xml
# Ignore build directory (target)
target/
# Ignore src/main directory (if you want to ignore specific files/folders in src/main)
src/main/
test.txt
allure-results
package PageObjectClasses;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class HomePage {
// Web driver object create
protected WebDriver obj;
private By WelcomeMessage = By.xpath("//div[@class='login-container']//h2");
private By LogoutButton = By.className("logout-btn");
// create constructor
public HomePage(WebDriver obj)
{
this.obj=obj;
}
// methods.
public String getText()
{
String fulltext = obj.findElement(WelcomeMessage).getText();
String []text= fulltext.split(" ");
return text[0];
}
public void clickLogout()
{
obj.findElement(LogoutButton).click();
}
}
package PageObjectClasses;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
// driver object
WebDriver obj;
private By usernameF = By.id("inputUsername");
private By passwordF = By.xpath("//input[@placeholder='Password']");
private By loginButton = By.cssSelector(".submit.signInBtn"); // Updated this line to use cssSelector instead of className
// Constructor
public LoginPage(WebDriver obj) {
this.obj = obj; // this.obj means class obj assigning the parameter obj value in it.
}
// Methods
public void enterUsername(String username) {
obj.findElement(usernameF).sendKeys(username);
}
public void enterPassword(String password) {
obj.findElement(passwordF).sendKeys(password);
}
public void clickSignButton() {
obj.findElement(loginButton).click();
}
}
package TestClasses;
import PageObjectClasses.HomePage;
import PageObjectClasses.LoginPage;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Utils.BaseTest;
public class LoginTest extends BaseTest {
@BeforeMethod
public void setUpTest()
{
setUp();
}
@Test(dataProvider = "sendData")
public void LoginTest(String Username, String Password)
{
obj.get("https://rahulshettyacademy.com/locatorspractice/");
// create object of page classes for use;
LoginPage login = new LoginPage(obj);
HomePage home = new HomePage(obj);
//-------------> now write test.
login.enterUsername(Username);
login.enterPassword(Password);
login.clickSignButton();
// --------------> Add Assertion.
Assert.assertEquals(Password,"rahulshettyacademy");
}
@DataProvider
public Object[][] sendData()
{
// Correct the index order for username and password in the array
Object[][] data = new Object[3][2];
data[0][0] = "Zain"; // username
data[0][1] = "rahulshettyacademy"; // password
data[1][0] = "yawar"; // username
data[1][1] = "rahulshettyacademy"; // password
data[2][0] = "Haseeb"; // username
data[2][1] = "hi"; // password
return data;
}
@AfterMethod
public void tearDownTest()
{
tearDown();
}
}
package Utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class BaseTest {
protected WebDriver obj;
public void setUp()
{
obj = new FirefoxDriver();
//Applying wait for synchronization
obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
WebDriverWait wait = new WebDriverWait(obj,Duration.ofSeconds(8));
}
public void tearDown()
{
if(obj!=null)
obj.close();
}
}
package DataProvider;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
public class DataProviderfromExcel {
WebDriver obj = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(20));
@BeforeClass
void setup() {
obj.get("https://rahulshettyacademy.com/locatorspractice/");
obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test(dataProvider = "dataStorage")
void LoginTest(String username, String password)
{
WebElement usernameField = obj.findElement(By.id("inputUsername"));
wait.until(ExpectedConditions.visibilityOf(usernameField));
usernameField.sendKeys(username);
usernameField.clear();
WebElement passwordField = obj.findElement(By.xpath("//input[@placeholder='Password']"));
passwordField.sendKeys(password);
usernameField.clear();
obj.findElement(By.className("submit")).click();
}
@DataProvider(name = "dataStorage")
public Object[][] dataStorage() throws IOException
{
String filePath = "//Users//zain//Documents//dataproviderfile.xlsx";
FileInputStream fileInputStream = new FileInputStream(filePath);
// Load workbook and sheet
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
int cols = sheet.getRow(0).getPhysicalNumberOfCells();
Object[][] data = new Object[rows - 1][cols];
for (int i = 1; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
data[i - 1][j] = sheet.getRow(i).getCell(j).toString();
}
}
workbook.close();
fileInputStream.close();
return data;
}
@AfterClass
void Exit() {
obj.quit();
}
}
package Techniques_to_automate_web_Element;
package DropDownsTypes;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
......@@ -6,9 +6,8 @@ import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.ArrayList;
import java.util.List;
public class WebElementAutomation {
public class DropDowns {
public static void main(String[] args) throws InterruptedException {
WebDriver obj = new FirefoxDriver();
// for maximizing the window.
......
package RobotClass;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class RobotClass {
public static void main(String[] args)
{
WebDriver obj = new ChromeDriver();
try {
// Navigate to the URL
obj.get("https://demo.automationtesting.in/Register.html");
// Set implicit wait
obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(50));
// Explicit wait for the file upload button
WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(50));
WebElement FileUpload = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='imagesrc']")));
// Use sendKeys() to upload the file
FileUpload.sendKeys("/Users/zain/Documents/SeleniumTraining/screenshot.png");
// Optionally use Robot class for additional interactions if needed
try {
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
obj.quit();
}
}
}
package FunctionalTesting;
package Synchronization;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
......@@ -10,7 +10,7 @@ import org.testng.asserts.SoftAssert;
import java.time.Duration;
public class Practice {
public class Waits {
public static void main(String[] args) {
WebDriver obj = new FirefoxDriver();
......
......@@ -38,6 +38,5 @@ public class TakeScreenShot {
{
System.out.println("File not found");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
<test verbose="2" preserve-order="true"
name="/Users/zain/IdeaProjects/SeleniumTraining/src/test/java/AllureReporting">
<test verbose="2" preserve-order="true" name="/Users/zain/Documents/SeleniumTraining/src/main/java">
<classes>
<class name="AllureReporting.Tests">
<class name="TestClasses.LoginTest">
<methods>
<include name="correctRedirect"/>
<include name="login"/>
<include name="reg"/>
<include name="LoginTest"/>
</methods>
</class>
</classes>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment