Commit 3ccb3ed7 authored by Qazi Zain's avatar Qazi Zain

added cucumber wrapped code in POM_Structure_code

parent 0e1105f1
package POM_Assigment.PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
private By emailField = By.id("userEmail");
private By passwordField = By.id("userPassword");
private By loginButton = By.id("login");
WebDriver obj;
//---------------------------------> Constructor
public LoginPage(WebDriver obj)
{
this.obj=obj;
}
//----------------------------------> Action Methods.
public void inputEmail(String email)
{
obj.findElement(emailField).sendKeys(email);
}
public void inputPassword(String password)
{
obj.findElement(passwordField).sendKeys(password);
}
public void clickLogin()
{
obj.findElement(loginButton).click();
}
}
package POM_Assigment.PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class RegisterPage {
WebDriver obj;
//---------------------------------------------> defining Elements with locators.
private By firstnameFeild = By.id("firstName");
private By lastNameField= By.id("lastName");
private By email = By.id("userEmail");
private By phoneNo= By.id("userMobile");
private By dropDown= By.className("form-group col-md-6");
private By occupationDoctor= By.xpath("//option[text()='Doctor']");
private By occupationEngineer= By.xpath("//option[text()='Engineer']");
private By occupationStudent= By.xpath("//option[text()='Student']");
private By occupationScientist= By.xpath("//option[text()='Scientist']");
private By genderMale = By.xpath("//input[@value='Male']");
private By genderFemale= By.xpath("//input[@value='Female']");
private By passwordField= By.id("userPassword");
private By confirmPasswordField = By.id("confirmPassword");
private By checkBoxButton = By.xpath("//input[@type='checkbox']");
private By loginButton = By.id("login");
private By successMessage = By.cssSelector(".headcolor");
//--------------------------------------------------------> constructor defining.
public RegisterPage(WebDriver obj)
{
this.obj= obj;
}
//-------------------------------------------------------> action methods.
public void inputFirstname(String firstname)
{
obj.findElement(firstnameFeild).sendKeys(firstname);
}
public void inputLastname(String lastname)
{
obj.findElement(lastNameField).sendKeys(lastname);
}
public void inputEmail(String emailValue)
{
obj.findElement(email).sendKeys(emailValue);
}
public void inputPhoneNo(String pno)
{
obj.findElement(phoneNo).sendKeys(pno);
}
public void selectDoctor()
{
obj.findElement(dropDown).click();
obj.findElement(occupationDoctor).click();
}
public void selectStudent()
{
obj.findElement(dropDown).click();
obj.findElement(occupationStudent).click();
}
public void selectEngineer()
{
obj.findElement(dropDown).click();;
obj.findElement(occupationEngineer).click();
}
public void selectScientist()
{
obj.findElement(dropDown).click();
obj.findElement(occupationScientist).click();
}
public void selectGenderMale()
{
obj.findElement(genderMale).click();
}
public void selectGenderFemale()
{
obj.findElement(genderFemale).click();
}
public void inputPassword(String pass)
{
obj.findElement(passwordField).sendKeys(pass);
}
public void inputConfirmPassword(String pass)
{
obj.findElement(confirmPasswordField).sendKeys(pass);
}
public void clickCheckBox()
{
obj.findElement(checkBoxButton).click();
}
public void pressLoginButton()
{
obj.findElement(loginButton).click();
}
public String getSuccessMessage() {
return obj.findElement(successMessage).getText();
}
}
package POM_Assigment.PageTest;
import POM_Assigment.PageObjects.LoginPage;
import POM_Assigment.Utils.BaseTest;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LoginTest extends BaseTest {
@Test
void testEmptyField() {
obj.get("https://rahulshettyacademy.com/client/auth/login");
LoginPage loginObject = new LoginPage(obj);
loginObject.inputEmail("");
loginObject.inputPassword("Password123");
loginObject.clickLogin();
String actualTitle = obj.getTitle();
String expectedTitle = "Login";
Assert.assertEquals(actualTitle, expectedTitle);
}
@Test
void validateSuccessfulLogin() {
obj.get("https://rahulshettyacademy.com/client/auth/login");
LoginPage loginObject = new LoginPage(obj);
loginObject.inputEmail("kazizain2001@gmail.com");
loginObject.inputPassword("Password123");
loginObject.clickLogin();
String actualTitle = obj.getTitle();
String expectedTitle = "Dashboard";
Assert.assertEquals(actualTitle, expectedTitle);
}
@Test
void validateToNotLoginInvalidUser() {
obj.get("https://rahulshettyacademy.com/client/auth/login");
LoginPage loginObject = new LoginPage(obj);
// Enter invalid credentials
loginObject.inputEmail("kazizain201@gmail.com");
loginObject.inputPassword("Password123");
loginObject.clickLogin();
String actualTitle = obj.getTitle();
String expectedTitle = "Login";
Assert.assertEquals(actualTitle, expectedTitle);
}
}
package POM_Assigment.PageTest;
import POM_Assigment.PageObjects.RegisterPage;
import POM_Assigment.Utils.BaseTest;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class RegisterationTest extends BaseTest {
@Test
void successFullRegistration()
{
obj.get("https://rahulshettyacademy.com/client/auth/register");
RegisterPage reg = new RegisterPage(obj);
reg.inputFirstname("Qazi");
reg.inputLastname("Zain");
reg.inputEmail("kazizain2001@gmail.com");
reg.inputPhoneNo("3020022247");
reg.selectEngineer();
reg.selectGenderMale();
reg.inputPassword("Password123");
reg.inputConfirmPassword("Password123");
reg.clickCheckBox();
reg.pressLoginButton();
String actualMessage = reg.getSuccessMessage();
Assert.assertEquals(actualMessage, "Account Created Successfully", actualMessage);
}
@Test
void testOnEmptyFields()
{
obj.get("https://rahulshettyacademy.com/client/auth/register");
RegisterPage reg = new RegisterPage(obj);
reg.inputFirstname("san");
reg.inputLastname("xsa");
reg.inputEmail("kazizain2001@gmail.com");
reg.inputPhoneNo("3020022247");
reg.selectEngineer();
reg.selectGenderMale();
reg.inputPassword("123");
reg.inputConfirmPassword("123");
reg.clickCheckBox();
reg.pressLoginButton();
String actualMessage = reg.getSuccessMessage();
Assert.assertEquals(actualMessage, "Password must be 8 Character Long!", actualMessage);
}
}
package POM_Assigment.Utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import java.time.Duration;
public class BaseTest {
protected WebDriver obj;
@Parameters("Browser")
@BeforeMethod
public void setUp(String Browser) {
if (Browser.equalsIgnoreCase("chrome")) {
obj = new ChromeDriver();
} else if (Browser.equalsIgnoreCase("safari")) {
obj = new SafariDriver();
} else if (Browser.equalsIgnoreCase("firefox")) {
obj = new FirefoxDriver();
}
obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterMethod
public void tearDown() {
if (obj != null) {
obj.quit();
}
}
}
@tag
Feature: Validating the login Functionality
Background: I landed on rahul shetty academy website login page
@tag2
Scenario Outline: Validating login with correct credentials
Given Present on login page
When I enter username "<name>" and password "<pass>" and click on login button
Then user should login
Examples:
| name | pass |
| zain | rahulshettyacademy |
| yawar | rahulshettyacademy |
package POM_Structure_code.Cucumber;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
// features='path set of that file which is have to run'
//glue have the package name of stepDefination.
// monochrome readable format or not? true or false by default true.(for print the result in readable format).
//plugin={"html:target/cucumber.html"} // genrate report in html format in target folder with the name of cucumber.
@CucumberOptions(features ="src/main/java/POM_Structure_code/Cucumber",glue="POM_Structure_code.StepDefination",monochrome = true,plugin = {"html:target/cucumber.html"})
//---------------------------------------------------> extends bcz in built cucumber do not have the power of scanning your testNG code<------------------------------
// TestNG is not in built in cucumber that is why we have to extend this class (for JUnit it is not required)
public class TestRunner extends AbstractTestNGCucumberTests
{
}
\ No newline at end of file
package PageObjectClasses; package POM_Structure_code.PageObjectClasses;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HomePage { import java.time.Duration;
public class HomePage
{
// Web driver object create // Web driver object create
...@@ -18,6 +24,7 @@ public class HomePage { ...@@ -18,6 +24,7 @@ public class HomePage {
public HomePage(WebDriver obj) public HomePage(WebDriver obj)
{ {
this.obj=obj; this.obj=obj;
} }
...@@ -25,10 +32,26 @@ public class HomePage { ...@@ -25,10 +32,26 @@ public class HomePage {
public String getText() public String getText()
{ {
String fulltext = obj.findElement(WelcomeMessage).getText(); WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(10));
String []text= fulltext.split(" "); try
{
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(WelcomeMessage));
String fullText = element.getText();
if (fullText.isEmpty())
{
throw new RuntimeException("Element text is empty!");
}
String[] text = fullText.split(" ");
return text[0]; return text[0];
} }
catch (Exception e)
{
System.err.println("Error occurred while retrieving text: " + e.getMessage());
throw e;
}
}
public void clickLogout() public void clickLogout()
{ {
......
package PageObjectClasses; package POM_Structure_code.PageObjectClasses;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class LoginPage { public class LoginPage {
...@@ -13,6 +16,9 @@ public class LoginPage { ...@@ -13,6 +16,9 @@ public class LoginPage {
private By passwordF = By.xpath("//input[@placeholder='Password']"); private By passwordF = By.xpath("//input[@placeholder='Password']");
private By loginButton = By.cssSelector(".submit.signInBtn"); // Updated this line to use cssSelector instead of className private By loginButton = By.cssSelector(".submit.signInBtn"); // Updated this line to use cssSelector instead of className
private By Checkbox = By.id("chkboxTwo");
WebDriverWait wait = new WebDriverWait(obj, Duration.ofSeconds(10));
// Constructor // Constructor
public LoginPage(WebDriver obj) { public LoginPage(WebDriver obj) {
...@@ -34,4 +40,23 @@ public class LoginPage { ...@@ -34,4 +40,23 @@ public class LoginPage {
obj.findElement(loginButton).click(); obj.findElement(loginButton).click();
} }
public void clickCheckBox()
{
wait.until(ExpectedConditions.invisibilityOfElementLocated(Checkbox));
wait.until(ExpectedConditions.elementToBeClickable(Checkbox));
obj.findElement(Checkbox).click();
}
public boolean isCheckedMethod()
{
wait.until(ExpectedConditions.invisibilityOfElementLocated(Checkbox));
wait.until(ExpectedConditions.elementToBeClickable(Checkbox));
return obj.findElement(Checkbox).isSelected();
}
} }
package POM_Structure_code.StepDefination;
import POM_Structure_code.TestClasses.LoginTest;
import POM_Structure_code.Utils.BaseTest;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.cucumber.java.Before;
public class StepDefination extends BaseTest {
LoginTest test = new LoginTest();
@Given("Present on login page")
public void presentOnLoginPage() {
setUp();
obj.get("https://rahulshettyacademy.com/locatorspractice/"); // Navigate to the login page
}
@When("I enter username {string} and password {string} and click on login button")
public void enteringData(String username, String password) {
test.loginTest(username, password); // Using the test class's method to perform login
}
@Then("user should login")
public void verifyLogin() {
test.validateLogin(); // Verifying if login was successful
}
}
package TestClasses; package POM_Structure_code.TestClasses;
import PageObjectClasses.HomePage; import POM_Structure_code.PageObjectClasses.HomePage;
import PageObjectClasses.LoginPage; import POM_Structure_code.PageObjectClasses.LoginPage;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import Utils.BaseTest; import POM_Structure_code.Utils.BaseTest;
public class LoginTest extends BaseTest { public class LoginTest extends BaseTest {
...@@ -16,14 +16,14 @@ public class LoginTest extends BaseTest { ...@@ -16,14 +16,14 @@ public class LoginTest extends BaseTest {
setUp(); setUp();
} }
@Test(dataProvider = "sendData") @Test(dataProvider = "sendData" , priority = 1)
public void LoginTest(String Username, String Password) public void loginTest(String Username, String Password)
{ {
obj.get("https://rahulshettyacademy.com/locatorspractice/"); obj.get("https://rahulshettyacademy.com/locatorspractice/");
// create object of page classes for use; // create object of page classes for use;
LoginPage login = new LoginPage(obj); LoginPage login = new LoginPage(obj);
HomePage home = new HomePage(obj);
//-------------> now write test. //-------------> now write test.
login.enterUsername(Username); login.enterUsername(Username);
...@@ -36,6 +36,28 @@ public class LoginTest extends BaseTest { ...@@ -36,6 +36,28 @@ public class LoginTest extends BaseTest {
} }
@Test (priority = 2)
public void validateLogin()
{
HomePage home = new HomePage(obj);
LoginPage lg = new LoginPage(obj);
obj.get("https://rahulshettyacademy.com/locatorspractice/");
lg.enterUsername("maaz");
lg.enterPassword("rahulshettyacademy");
lg.clickSignButton();
String validate=home.getText();
Assert.assertEquals(validate,"Hello");
}
@DataProvider @DataProvider
public Object[][] sendData() public Object[][] sendData()
{ {
......
package Utils; package POM_Structure_code.Utils;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration; import java.time.Duration;
...@@ -16,7 +15,6 @@ public class BaseTest { ...@@ -16,7 +15,6 @@ public class BaseTest {
//Applying wait for synchronization //Applying wait for synchronization
obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); obj.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
WebDriverWait wait = new WebDriverWait(obj,Duration.ofSeconds(8));
} }
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite"> <suite name="All Test Suite">
<test verbose="2" preserve-order="true" name="/Users/zain/Documents/SeleniumTraining/src/main/java"> <test verbose="2" preserve-order="true"
name="/Users/zain/Documents/SeleniumTraining/src/main/java/POM_Structure_code">
<classes> <classes>
<class name="TestClasses.LoginTest"> <class name="POM_Structure_code.TestClasses.LoginTest">
<methods> <methods>
<include name="LoginTest"/> <include name="LoginTest"/>
<include name="checked"/>
</methods> </methods>
</class> </class>
</classes> </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