Creating A Properties File in Eclipse
Creating A Properties File in Eclipse
1. To start with, the below java project structure needs to be created
in eclipse. Project name and package name can be any valid names.
2. Right-click on the main project folder and Select New-> Other
3. In the next window, select General -> File and click on 'Next'
button
4. Provide a valid file name with the extension '.properties' on the
new file resource window and click on 'Finish' button
5. A file named 'application.properties' must be displayed on Project
Structure
The string 'mobileTesting' will contain the XPATH to identify the Mobile
Testing link within the webpage.
driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click();
driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("testguru99@gm
ail.com
");
driver.findElement(By.id(obj.getProperty("SignUpButton"))).click();
The below is the complete code used for the above test scenario.
package com.objectrepository.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DemoOR {
public static void main(String[] args) throws IOException
{
// Create WebDriver Instance
WebDriver driver;
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/guru99home/");
driver.manage().window().maximize();
// Load the properties File
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")
+"\\application.properties");
obj.load(objfile);
// Nagigate to link Mobile Testing and Back
driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click();
driver.navigate().back();
// Enter Data into Form
driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("testguru99@gm
ail.com");
driver.findElement(By.id(obj.getProperty("SignUpButton"))).click();
}
}
<menu>
<mobiletesting>//a[text()='MOBILE TESTING']</mobiletesting>
<email> philadelphia-field-email</email>
<signup> philadelphia-field-submit </signup>
</menu>
jaxen.jar
dom4j-1.6.jar
driver.findElement(By.xpath(mobileTesting)).click();
driver.findElement(By.id(emailTextBox)).sendKeys("[email protected]");
driver.findElement(By.id(signUpButton)).click();
The below code demonstrates the use of XML file in selenium WebDriver
package com.objectrepository.demo;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DemoORXML {
public static void main(String[] args) throws DocumentException
{
// Creating WebDriver Instance
WebDriver driver;
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/guru99home/");
driver.manage().window().maximize();
// Reading XML File
File inputFile = new File(System.getProperty("user.dir")
+"\\properties.xml");
SAXReader saxReader = new SAXReader();
Document document =
saxReader.read(inputFile);
String mobileTesting =
document.selectSingleNode("//menu/mobiletesting").getText();
String emailTextBox =
document.selectSingleNode("//menu/email").getText();
String signUpButton =
document.selectSingleNode("//menu/signup").getText();
//Navigating to Mobile Testing and back
driver.findElement(By.xpath(mobileTesting)).click();
driver.navigate().back();
//Entering Form Data
driver.findElement(By.id(emailTextBox)).sendKeys("[email protected]");
driver.findElement(By.id(signUpButton)).click();
}
}