DWQA Ask QuestionCategory: SeleniumImplement Object Repository using Properties/Excel/XML formats
admin Staff asked 5 years ago

Purpose of the assignment: So far we have learnt only page object model approach to implement the repository component of the Framework. By this assignment we will implement other approached by which repository can be implement, the very basic one can be via properties file. Other than that we will see how to implement the same via other mechanisms like using excel file or xml. So this is a three part assignment.

  • Automate 5 test cases of para bank application.
  • URL: http://parabank.parasoft.com/parabank/index.htm
  • Automate Login page test cases and transfer funds test cases.
  • Keep all the objects in a Properties file in below format:(you can choose to make changes here but it has to be key and value pair)
    • LocaterName | Detail
    • UserName      | id:username
    • Password .      | id:password
  • Try to do the same thing using Excel and XML file. (try to complete the first one, if time permits than will try to do it)

 

 

5 Answers
Bindiya Patil Staff answered 5 years ago

/****Bindiya Patil*****/
//  code for reading propertyfile //
public class Propertyfile {
public static String getProperty(String key)
{
String propertyfilepath=”../RetailBank/src/com/Config/properties/EnvirmentalVariables”;

Properties p=new Properties(); //creating object of properties class
try
{
FileInputStream file = new FileInputStream(propertyfilepath);//read the propertyfile
p.load(file);
}
catch (Exception e1)
{
e1.printStackTrace();
}
return p.getProperty(key);
}
}
 
//***** config.properties file where xpaths are stored*****//
url=http://parabank.parasoft.com
username1=//input[@name=’username’]
Password1=//input[@name=’password’]
signbtn=//input[@class=’button’]
username=john
passward=demo
 
//*****test case for parabank login using propertyfile*****//
 
package com.PO;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.BaseTest.Propertyfile;
import com.Utility.methodforxpath;
public class parabank_login {
WebDriver driver;
@BeforeMethod
public void before_method()
{
System.setProperty(“webdriver.chrome.driver”, “E:\\chromedriver.exe”);
// driver.get(propertyfile1.getProperty(“ChromeExe”));
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
driver.get(Propertyfile.getProperty(“url”));
driver.manage().window().maximize();
}
@Test
public void validate_parabank_login()
{
WebElement para_username= driver.findElement(methodforxpath.xpathmethod(Propertyfile.getProperty(“username1”)));
para_username.sendKeys(Propertyfile.getProperty(“username”));
WebElement para_password=driver.findElement(methodforxpath.xpathmethod(Propertyfile.getProperty(“Password1”)));
para_password.sendKeys(Propertyfile.getProperty(“passward”));
WebElement para_signin=driver.findElement(methodforxpath.xpathmethod(Propertyfile.getProperty(“signbtn”)));
para_signin.click();
}
}

VaibhavD Staff answered 5 years ago

TC_PropertyFile_ObjectRepository.java
package com.tc;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.utils.BrowserManager;
import com.utils.PropertyFileReader;
public class TC_PropertyFile_ObjectRepository {

static WebDriver driver;
@BeforeMethod
void setUp() throws Exception {
//1. Initialize the Browser
driver = BrowserManager.GetBrowser(“chrome”);
driver.get(PropertyFileReader.getProperty(“url”));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Reporter.log(“Test Case Execution started…”, true);
}
@AfterMethod
void tearDown() throws Exception {
driver.close();
Reporter.log(“Test Case Excecuted.”, true);
}
@Test
void t01_verify_loginPage_title() {
String s_title_expected = “ParaBank | Welcome | Online Banking”;
String s_title_actual = driver.getTitle();
Assert.assertEquals(s_title_expected, s_title_actual);
Reporter.log(“Login Page title is verified”, true);
}
@Test
void t02_login_into_parabank(String username, String password) throws IOException {
driver.findElement(By.name(PropertyFileReader.getProperty(“txtbx_login”))).sendKeys(PropertyFileReader.getProperty(“username”));
driver.findElement(By.name(PropertyFileReader.getProperty(“txtbx_password”))).sendKeys(PropertyFileReader.getProperty(“password”));
driver.findElement(By.xpath(PropertyFileReader.getProperty(“btn_login”))).click();
// validation after login
String expected = “ParaBank | Accounts Overview”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
Reporter.log(“Login Successful”, true);
Reporter.log(“Login Successful”, true);
}
@Test
void t03_negative_tc_login_into_parabank() {
driver.findElement(By.name(PropertyFileReader.getProperty(“txtbx_login”))).sendKeys(PropertyFileReader.getProperty(“username1”));
driver.findElement(By.name(PropertyFileReader.getProperty(“txtbx_password”))).sendKeys(PropertyFileReader.getProperty(“password”));
driver.findElement(By.xpath(PropertyFileReader.getProperty(“btn_login”))).click();
// validation after entering wrong credentials
String expected = “ParaBank | Error”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
Reporter.log(“The usename and password could not be verified…”, true);
}
@Test
void t04_forget_login_info_parabank() {
driver.findElement(By.linkText(PropertyFileReader.getProperty(“link_forget_login_info”))).click();
// validation
String expected = “ParaBank | Customer Lookup”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
Reporter.log(“Forget Login Info verified…”, true);
}
@Test
void t05_register_to_parabank() {
driver.findElement(By.linkText(PropertyFileReader.getProperty(“link_register”))).click();
// validation
String expected = “ParaBank | Register for Free Online Account Access”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
Reporter.log(“Fill form to register…”, true);
}
}
———————————————————————————————————————————————–
login.properties
# LocaterName | Detail
txtbx_login = username
txtbx_password = password
btn_login = //input[@type=’submit’ and @value=’Log In’]
link_forget_login_info = Forgot login info?
link_register = Register

# User data
url= http://parabank.parasoft.com/parabank/index.htm
username =john
password = demo
username1=johny
———————————————————————————————————————————————–
PropertyFileReader.java
package com.utils;
import java.io.FileInputStream;
import java.util.Properties;
public class PropertyFileReader {
public static String getProperty(String arg)
{
String file_path = “C:\\Users\\Vaibhav\\login.properties”;
Properties oProp=new Properties();
try
{
FileInputStream file = new FileInputStream(file_path);
oProp.load(file);
}
catch (Exception e)
{
e.printStackTrace();
}
return oProp.getProperty(arg);
}
}

VaibhavD Staff answered 5 years ago

 
TC_XMLFile_ObjectRepository.java
static WebDriver driver;
@BeforeSuite
void setUp() throws Exception {
//1. Initialize the Browser
driver = BrowserManager.GetBrowser(“chrome”);
driver.get(“http://parabank.parasoft.com/parabank/index.htm”);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Reporter.log(“Test Case Execution started…”, true);
}

@AfterSuite
void tearDown() throws Exception {
driver.close();
Reporter.log(“Test Case Excecuted.”, true);
}
@Test
void t_login_into_parabank() throws IOException, DocumentException {
// Load the properties File
String file_path = “C:\\Users\\Vinni\\eclipse-workspace\\POM_without_pagefactory\\src\\test\\java\\com\\object_reposirory\\login.xml”;
// Reading XML File
File inputFile = new File(file_path);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputFile);
String uName = document.selectSingleNode(“//txtbx_username”).getText();
String pWord = document.selectSingleNode(“//txtbx_password”).getText();
String log_btn = document.selectSingleNode(“//btn_login”).getText();

driver.findElement(By.name(uName)).sendKeys(“john”);
driver.findElement(By.name(pWord)).sendKeys(“demo”);
driver.findElement(By.xpath(log_btn)).click();
Reporter.log(“Login Successful”, true);
}
}

 
login.xml
<Locator>
<txtbx_username>username</txtbx_username>
<txtbx_password>password</txtbx_password>
<btn_login>//input[@type=’submit’ and @value=’Log In’]</btn_login>
</Locator>

pom.xml 
(Add these dependencies )
<!– https://mvnrepository.com/artifact/jaxen/jaxen –>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<!– https://mvnrepository.com/artifact/dom4j/dom4j –>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
 

Tejal Gadewar Staff answered 5 years ago

//Properties File for Parabank
 
locatorname|detail
username=//input[@name=’username’and @type=’text’]
password=//input[@name=’password’]
login=//input[@class=’button’]
lnkxt_transferfunds=//a[contains(text(),’Transfer Funds’)]
txtbox_account=//input[@name=’input’]
drpdwn_frmacc=//select[@id=’fromAccountId’]
drpdwn_toacc=//select[@id=’toAccountId’]
btn_transfer=//input[@class=’button’]
lnktxt_updateinfo=Update Contact Info
txtbox_phoneno=//input[@id=’customer.phoneNumber’]
btn_updateProfile=//input[@value=’Update Profile’]
 
//Test Case For Properties File
 
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
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.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TC_Properties {

public WebDriver driver;

@BeforeMethod
public void launchBrowser()
{
System.setProperty(“webdriver.chrome.driver”, “F:\\automation\\chromedriver.exe”);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“https://parabank.parasoft.com&#8221;);
Reporter.log(“URL launched<br>”);
}
@Test
public void tc_01_login() throws IOException
{

//load properties file
Properties obj=new Properties();
FileReader fileobj=new FileReader(“objectrepo.properties”);
obj.load(fileobj);

driver.findElement(By.xpath(obj.getProperty(“username”))).sendKeys(“john”);
Reporter.log(“pass user name as john<br>”);
driver.findElement(By.xpath(obj.getProperty(“password”))).sendKeys(“demo”);
Reporter.log(“pass password as demo<br>”);
driver.findElement(By.xpath(obj.getProperty(“login”))).click();
Reporter.log(“login successfully<br>”);
}

@Test
public void tc_02_updateContactInfo() throws IOException
{

Properties obj=new Properties();
FileReader fileobj=new FileReader(“objectrepo.properties”);
obj.load(fileobj);
this.tc_01_login();
driver.findElement(By.linkText(obj.getProperty(“lnktxt_updateinfo”))).click();
Reporter.log(“clicked on Update Info<br>”);
WebElement phn_no=driver.findElement(By.xpath(obj.getProperty(“txtbox_phoneno”)));
WebDriverWait wt=new WebDriverWait(driver,60);
wt.until(ExpectedConditions.visibilityOf(phn_no));
phn_no.clear();
phn_no.sendKeys(“9876543679”);
Reporter.log(“Phone Number updated”);
driver.findElement(By.xpath(obj.getProperty(“btn_updateProfile”))).click();

}
@Test
public void tc_03_transferFunds() throws IOException, InterruptedException
{
//load properties file
Properties obj=new Properties();
FileReader fileobj=new FileReader(“objectrepo.properties”);
obj.load(fileobj);
this.tc_01_login();
WebElement transfund=driver.findElement(By.xpath(obj.getProperty(“lnkxt_transferfunds”)));
transfund.click();
Reporter.log(“clicked on transfer funds<br>”);
Thread.sleep(5000);

WebElement acc=driver.findElement(By.xpath(obj.getProperty(“txtbox_account”)));
acc.sendKeys(“5000”);
Reporter.log(“5000 amount is entered<br>”);

Select select=new Select(driver.findElement(By.xpath(obj.getProperty(“drpdwn_frmacc”))));
select.selectByIndex(2);
Reporter.log(“from account value is selected<br>”);

Select select1=new Select(driver.findElement(By.xpath(obj.getProperty(“drpdwn_toacc”))));
select1.selectByIndex(5);
Reporter.log(“to account value is selected<br>”);

driver.findElement(By.xpath(obj.getProperty(“btn_transfer”))).click();
Reporter.log(“transfer completed”);
}
}

Supriya Kitukale Staff answered 5 years ago

//Properties file for Parabank application Login & Transfer funds module
public class LoginParabank
{
static WebDriver driver=null;
public LoginParabank() throws IOException
{
System.setProperty(“webdriver.chrome.driver”, “E:\\Vision IT\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
}
@Test

public static void LoginForParabank_using_PropertiesFile() throws IOException
{
Properties p=new Properties();
FileInputStream fis=new FileInputStream(“E:\\Vision IT\\NewEclipseWorkSpace\\Assignment\\src\\test\\resources\\login.properties”);
p.load(fis);
driver.get(p.getProperty(“URL”));
driver.findElement(By.name(p.getProperty(“User_name”))).sendKeys(p.getProperty(“username”));
driver.findElement(By.name(p.getProperty(“Password_name”))).sendKeys(p.getProperty(“password”));
driver.findElement(By.xpath(“//input[@type=’submit’]”)).click();
Reporter.log(“Login successfully in parabank application”);
}

@Test
public static void TC_03_TransferFunds() throws IOException, InterruptedException
{
//load properties file
Properties obj=new Properties();
FileInputStream fis=new FileInputStream(“E:\\Vision IT\\NewEclipseWorkSpace\\Assignment\\src\\test\\resources\\login.properties”);
obj.load(fis);
driver.get(obj.getProperty(“URL”));
//this.LoginForParabank_using_PropertiesFile();
WebElement transferfund=driver.findElement(By.xpath(obj.getProperty(“link_transferfunds”)));
transferfund.click();
Reporter.log(“lick on transfer fund”);
Thread.sleep(5000);
WebElement account=driver.findElement(By.xpath(obj.getProperty(“txtbx_account”)));
account.sendKeys(“2000”);
Reporter.log(“amount enter as 2000”);
Select se=new Select(driver.findElement(By.xpath(obj.getProperty(“drp_frmacc”))));
se.selectByIndex(2);
Reporter.log(“value selected from frmdropdown”);
Select se1=new Select(driver.findElement(By.xpath(obj.getProperty(“drp_toacc”))));
se1.selectByIndex(5);
Reporter.log(“value selected from todropdown”);
driver.findElement(By.xpath(obj.getProperty(“transfer_butn”))).click();
Reporter.log(“Transfer completed”);
}

}