Saturday 5 December 2020

Example of POM in java selenium

 package com.test.webpages;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;

import org.openqa.selenium.support.ui.*;

import org.assertj.core.api.Assertions;




public class RegistrationPage {

private WebDriver driver;

//private static String url="https://www.cisiveglobal.com/Registration.html";


@FindBy(xpath = "//*[@id='name']")

WebElement elementName;


@FindBy(xpath = "//*[@id='dateofbirth']")

WebElement elementDOB;


@FindBy(xpath = "//*[@id='male']")

WebElement elementMale;


@FindBy(xpath="//*[@id='female']")

WebElement elementFemale;


@FindBy(xpath="//*[@id='qualification']")

WebElement elementQualification;


@FindBy(xpath="//*[@id='mobile']")

WebElement elementMobile;


@FindBy(xpath="//*[@id='emailid']")

WebElement elementEmail;


@FindBy(xpath="//*[@id='address']")

WebElement elementAddress;




public RegistrationPage(WebDriver driver) {

this.driver=driver;

PageFactory.initElements(driver, this);

}


public void enterName(String name) {


Assertions.assertThat(name)

.hasSizeGreaterThan(1)

.hasSizeLessThan(25)

.doesNotContainOnlyWhitespaces();

elementName.sendKeys(name);


}

public void enterDOB(String dateofbirth)

{

Assertions.assertThat(dateofbirth)

.doesNotContainAnyWhitespaces()

.isLessThan(java.time.LocalDateTime.now().toString())

.isGreaterThan("01/01/1900");

elementDOB.sendKeys(dateofbirth);


}

public void enterMale(String male) {

Assertions.assertThat(male)

.doesNotContainAnyWhitespaces()

.isEqualTo("male");

elementMale.click();

}


public void enterFemale(String female) {

Assertions.assertThat(female)

.doesNotContainAnyWhitespaces()

.isEqualTo("female");

elementFemale.click();

}


public void enterQualification(String qualification) {

Assertions.assertThat(qualification)

.doesNotContainAnyWhitespaces();

Select select=new Select(elementQualification);

select.selectByValue(qualification);


}


public void enterMobile(String mobile) {

Assertions.assertThat(mobile)

.doesNotContainAnyWhitespaces()

.hasSizeGreaterThan(0)

.hasSizeLessThan(11)

.containsOnlyDigits();

elementMobile.sendKeys(mobile);


}

public void enterEmail(String email) {

Assertions.assertThat(email)

.doesNotContainAnyWhitespaces()

.hasSizeGreaterThan(3)

.hasSizeLessThan(25);

elementEmail.sendKeys(email);


}

public void enterAddress(String address) {

Assertions.assertThat(address)

.hasSizeGreaterThan(0)

.hasSizeLessThan(255);

elementEmail.sendKeys(address);


}



}



--====================


package com.test.webpages;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;


public class RegistrationPageOperations {


private WebDriver driver;

//private static String url="https://www.cisiveglobal.com/Registration.html";

@FindBy(xpath="//*[@id='btnSubmit']")

WebElement eleBtnSubmit;

@FindBy(xpath="//*[@id='btnReset']")

WebElement eleBtnReset;

public RegistrationPageOperations(WebDriver driver) {

this.driver=driver;

PageFactory.initElements(driver, this);

}

public void clickOnSubmit() {

eleBtnSubmit.click();

}

public void clickOnReset() {

eleBtnReset.click();

}

}


--=================================

package com.test.webpages;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

public class RegistrationSetup {

String driverPath="E:/geocodedriver/chorme/chromedriver.exe";
WebDriver driver;
RegistrationPage regPage;
RegistrationPageOperations regPageOpt;
@BeforeClass
public void Setup() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.cisiveglobal.com/Registration.html");
}
@Test(priority=1)
public void details_to_registration_page() {
regPage=new RegistrationPage(driver);
regPage.enterName("M Shahnawaz");
regPage.enterDOB("10-10-1990");
regPage.enterMale("male");
regPage.enterQualification("MCA");
regPage.enterMobile("9650953515");
regPage.enterEmail("msnazia2012@gmail.com");
regPage.enterAddress("Shaheen Bagh, New Delhi,Inida - 110065");
regPageOpt=new RegistrationPageOperations(driver);
regPageOpt.clickOnSubmit();
}
@Test(priority=1)
public void reset_to_registration_page()
{
regPageOpt=new RegistrationPageOperations(driver);
regPageOpt.clickOnReset();
}
@AfterClass
public void Teardown() {
driver.quit();
}
}


No comments:

Post a Comment