Monday, December 22, 2014

Read XML file trigger Selenium-WebDriver

In this tutorial, we will show you how to read an XML file via DOM XML parser. DOM parser parses the entire XML document and loads it into memory.And trigger Selenium Webdriver

XML file look like this

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<PROJECT>
<ROOTDIR>D:\test.xml</ROOTDIR>
<APP_URL>http://www.google.com</APP_URL>
</PROJECT>

Save this as xml somewhere in your pc


In this tutorial am going to Parse <APP_URL>http://www.google.com</APP_URL> to selenium webdriver to navigate to www.google.com

Source code :

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class readxml  {

public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException
{

File xmlread=new File("D:\\test.xml");
//creating object for DocumentBuilderFactory
DocumentBuilderFactory dbfocrory=DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder=dbfocrory.newDocumentBuilder();
Document doc=dBuilder.parse(xmlread);

NodeList lxml=doc.getChildNodes();
Node nxml=lxml.item(0);
Element element=(Element)nxml;
//get element by tagname
System.out.println(element.getElementsByTagName("APP_URL").item(0).getTextContent());

//Navigate to google from data retrived from xml
WebDriver driver=new FirefoxDriver();
driver.get(element.getElementsByTagName("APP_URL").item(0).getTextContent());
driver.close();
}

}

Hope you like this post..
Hit Like button on facebook,



No comments:

Post a Comment