Wednesday, 27 August 2014

Read an xml Testdata in Selenium

  We can create an element repository for storing the elements in xml,we have to retrieve those elements in the specified area.

Required steps:

Step 1: We have to create an xml and save in the definite location.
Step 2: We have to use attributes like <ElementReopository/>to save the elements.
Step 3: We have to use Document Builder methods to retrieve data from the xml .
Step 4: We have to name the path where the xml file has been saved.

Here  is the piece of code.

package HDFC;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;

import org.openqa.selenium.By;

public class Mainpage{

private WebDriver driver;
private static DocumentBuilder db;
private static Document doc;
private static NodeList nList;
private static File file;

public static String getFromhffc(String element) throws ParserConfigurationException, SAXException,

  IOException{

   try{

    file=new File("D:\\seleniumworkspace\\SeleniumWebapplications\\src\\TestNG\\XML\\hfc.xml");

    db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    doc = db.parse(file);

    nList = doc.getElementsByTagName(element);


   }catch(NullPointerException e){

    System.out.println("IOException in sssssss"+ e);
  }
  return nList.item(0).getTextContent();

  }
       public static void main(String[] args) {
             
               driver = new FirefoxDriver();
           
               driver.get("http://www.hdfcbank.com/");
           
               driver.findElement(By.className(getFromhffc("Logo"))).click();

        driver.findElement(By.linkText(getFromhffc("homme"))).click();

         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
           
              driver.close();

}
}

hfc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ElementRepository>
<Logo>logo</Logo>

<homme>Home</homme>
</ElementRepository>




To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

No comments:

Post a Comment