Saturday, March 27, 2021

How to create Re-usable Method for Verify getText(),Click()and SendKeys() and Add your framework

Step1:- Create Helper class  

package com.businesslib;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


public class Helper {

public static WebDriver driver;

/**

* This Method is used to launch app

*/

public static void lauchApp(){

System.setProperty("webdriver.chrome.driver","D:\\driver1016\\driver\\chromedriver.exe");

driver=new ChromeDriver();

driver.get("https://in.yahoo.com/");

driver.manage().window().maximize();

}

/**

* This Method is used to verify the title of page

* @param ExpectedTite is String Type

*/

public static String verifyTiteOfPage(String expectedtitle){

String titleOfPage=driver.getTitle();

System.out.println("TitleOf Page::" + titleOfPage);

if(titleOfPage.equals(expectedtitle)){

System.out.println("Title is matched");

}

else{

System.out.println("Title is not matched");

}

return titleOfPage;


}

/**

* This Method is used to maxmize browser

*/

public static void maxmizeBroser(){

driver.manage().window().maximize();

}

/**

* This Method is used to getCurrentUrl

*/

public static String getcurrentUrl(){

String getcurrenturl=driver.getCurrentUrl();

System.out.println("getcurrenturl:::" + getcurrenturl);

return getcurrenturl;

}

/**

* This Method is used to getpageSourse

*/


public static String getpageSourse(){

String getpageSourse=driver.getPageSource();

System.out.println("getpageSourse:::" + getpageSourse);

return getpageSourse;

}

/**

* This Method is used to getTitleofpage

*/


public static String gettitleOfpage(){

String titleofpage=driver.getTitle();

System.out.println("titleofpage::" + titleofpage);

return titleofpage;

}

/**

* This Method is used to close the browser

*/

public static void closeBrowser(){

driver.close();

}

/**

* This Method is used to capture text on webpage

*/

public static String getTextMessage(String xpath) throws InterruptedException{

Thread.sleep(2000);

String gettext=driver.findElement(By.xpath(xpath)).getText();

System.out.println(gettext);

return gettext;

}

/**

* This Method is used to click on webpage

*/

public static void clickOnPage(String xpath){

driver.findElement(By.xpath(xpath)).click();

}

/**

* This Method is used to Enter vale in text Field

*/

public static void enterValueInTextField(String xpath,String str){

driver.findElement(By.xpath(xpath)).sendKeys(str);

}

public static String verifyStringText(String xpath,String expcted){

String actualValue=driver.findElement(By.xpath(xpath)).getText();

if(expcted.equals(actualValue)){

System.out.println("Text is Matched & Test case passed");

}

else{

System.out.println("Text is Matched & Test case passed");

}

return actualValue;

}


}

=======================================================================

Step 2:- Test Runner class 

package com.seleniumpractice;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


import com.businesslib.Helper;


public class VerifyErrorMessage extends Helper {


public static void main(String[] args) throws InterruptedException {

/*

System.setProperty("webdriver.chrome.driver","D:\\driver1016\\driver\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("https://in.yahoo.com/");

driver.manage().window().maximize();

*/

Helper.lauchApp();

Helper.clickOnPage("//span[contains(text(),'Sign in')]");

//driver.findElement(By.id("login-username")).sendKeys("mohit123");

Helper.enterValueInTextField("//input[@id='login-username']","mohit123");

//driver.findElement(By.id("login-signin")).click();

Helper.clickOnPage("//input[@id='login-signin']");

//String errorMessage=driver.findElement(By.id("username-error")).getText();

//System.out.println("errorMessage::" + errorMessage);

//WebElement element=driver.findElement(By.id("username-error"));

Helper.getTextMessage("//*[@id='username-error']");

Helper.verifyStringText("//*[@id='username-error']","Sorry, we don't recognise this email address.");

}


}








No comments:

Post a Comment

Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...