Reading data from an xml file with selenium web driver

Reading data from an XML file with selenium web driver


Data driven testing is a type of software testing where data are created in the excel sheet/xml file or a json file , and is then read into automation testing tools to invoke to the software under test.

Most of the time we use excel spreadsheets to read data for data driven testing. However, in this post lets look at how we can read data from an XML file.

Before we start we need to get some dependencies downloaded into our selenium project. As a result, we need to add the below listed dependency into the pom.xml file within the <dependencies> </dependencies> tags.


<dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy</artifactId>
        <version>2.5.0-alpha-1</version>
    </dependency>
</dependencies>
<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>



Now lets look at the XML files structure from which we will read the data into the selenium script. In our sample XML file we have the URL pointing to google.lk and we are going to search for the term Ballerina.

<data>
<url>http://www.google.lk</url>
<search_word>Ballerina lang</search_word>
</data>


In the code block under @BeforeTest, we are calling the Chrome driver from a folder location and then we are initializing the DocumentBuilderFactory, DocumentBuilder and Document objects.  We then call the URL and the search_word tags from the XML file in order to read the data from the respective tags.


Finally, within the  @Test method we then call the google search pages ui objects (search filed and the the button) and pass the values from the XML file as inputs. Once the search term is submitted, the script will perform a click on the identified link and navigate into the page.

package project;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import jdk.internal.org.xml.sax.SAXException;

import java.io.File;

public class ReadXML {
    WebDriver driver;
    String url,searchWord;
    String filePath="/home/shavantha/eclipse-workspace/project/data/data.xml";
  

@BeforeTest
public void setup() throws SAXException, IOException, ParserConfigurationException, org.xml.sax.SAXException
{
    System.setProperty("webdriver.chrome.driver", "/home/shavantha/Documents/chromedriver");

    File fXmlFile = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
  
    url = doc.getElementsByTagName("url").item(0).getTextContent();
    searchWord = doc.getElementsByTagName("search_word").item(0).getTextContent();
    driver = new ChromeDriver();
    driver.get(url);
}


@Test
public void testSearch()
{
 
    driver.findElement(By.id("lst-ib")).sendKeys(searchWord);
    driver.findElement(By.name("btnK")).click();
    driver.findElement(By.linkText("Ballerina.io: Home")).click();
  
    String webTitle = driver.getTitle();
  
}

@AfterTest
public void tearDown()
{
    driver.close();
    driver.quit();
}
}

Comments