Properties File Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
//To create own page object repository. file_name : config.properties #########@ Global properties @######## browser= chrome url= http://parabank.parasoft.com username=john password=demo ###########Login object############# user_name = username password_name = password Login_btn =//input[@type='submit'] ################################### //write a code using properties file to login para_bank application. package com.SeleniumTest; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.safari.SafariDriver; // Read properties File: public class ReadClass { public static void main(String[] args) throws IOException { WebDriver driver = null; //step1: how to read properties file: Properties prop = new Properties(); //step2 :create file I/p class: and load file path. FileInputStream ip = new FileInputStream("E:/NewJavaProject/SeleniumPractice/SeleniumPractice/config.properties"); //step3: load this file I/p class reference loaded. prop.load(ip); //step4: access browser.(establish connection) System.out.println(prop.getProperty("browser")); String browserName = prop.getProperty("browser"); if(browserName.equals("chrome")){ System.setProperty("webdriver.chrome.driver","C:\\selenium\\chromedriver.exe"); driver = new ChromeDriver(); } else if(browserName.equals("FF")){ driver = new FirefoxDriver(); } else if(browserName.equals("safari")){ driver = new SafariDriver(); } else if(browserName.equals("IE")){ driver = new InternetExplorerDriver(); } else { System.out.println("no browser value is given"); } driver.get(prop.getProperty("url")); driver.findElement(By.name(prop.getProperty("user_name"))).sendKeys(prop.getProperty("username")); driver.findElement(By.name(prop.getProperty("password_name"))).sendKeys(prop.getProperty("password")); driver.findElement(By.xpath("//input[@type='submit']")).click(); } } |