Tuesday, January 7, 2014

Reading from XML to trigger a Selenium driver

Reading from XML to trigger a Selenium driver


xml:















Java code:
/*

* @ Author : Sumit Mittal

* @ Description : A simple program to read XML and retrieve data to manipulate Selenium webdriver

* @ Date : 1/03/2014

*/

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.*;

import org.w3c.dom.*;



public class GmailXMLTest {



public static void main(String argv[]) {



try {

File file = new File("C:\\TestData.xml"); //file location should be specified correctly

// Prepare XML

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document document = db.parse(file);

document.getDocumentElement().normalize();

System.out.println("Debug: Root element"+ document.getDocumentElement().getNodeName());

NodeList node = document.getElementsByTagName("TestData");

System.out.println("TestData Details");

System.out.println("________________________________________________");



//Read XML to get test data



for (int i = 0; i < node.getLength(); i++) {

Node currentNode = node.item(i);



if (currentNode.getNodeType() == Node.ELEMENT_NODE) {



Element element = (Element) currentNode;

NodeList mailServerElemntList = element.getElementsByTagName("service");

Element mailServer = (Element) mailServerElemntList.item(0);

NodeList mailServer1 = mailServer.getChildNodes();

String Service = ((Node)mailServer1.item(0)).getNodeValue();

System.out.println("Mail Servername:"+ Service);



NodeList emailNodeElementList = element.getElementsByTagName("email-id");

Element emailNodeElement = (Element)emailNodeElementList.item(0);

NodeList details = emailNodeElement.getChildNodes();

String emailAddress=((Node) details.item(0)).getNodeValue();

System.out.println("email :" + emailAddress);



NodeList passwordNodeElementList = element.getElementsByTagName("password");

Element passwordNodeElement = (Element) passwordNodeElementList.item(0);

NodeList address = passwordNodeElement.getChildNodes();

String passCode = ((Node) address.item(0)).getNodeValue();

System.out.println("Password : "+passCode);



NodeList destFolder = element.getElementsByTagName("folder");

Element destElement = (Element) destFolder.item(0);

NodeList city = destElement.getChildNodes();

String destnFolder = ((Node) city.item(0)).getNodeValue();

System.out.println("Folder : " + destnFolder);



FirefoxDriver wd = new FirefoxDriver();

wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

wd.get(Service);

wd.findElement(By.id("Email")).sendKeys(emailAddress);

wd.findElement(By.id("Passwd")).sendKeys(passCode);

wd.findElement(By.id("signIn")).click();

Thread.sleep(8000);

System.out.println(wd.getTitle());

System.out.println(wd.getCurrentUrl());

if (!wd.getCurrentUrl().equals("https://mail.google.com/mail/?shva=1#inbox")) {

wd.close();

System.out.println("Failed to navigate to inbox");

throw new RuntimeException("assertTitle failed");

}

System.out.println("Test Completed - login passed and navigated to inbox");

wd.close();



}

}}

catch (Exception e) {e.printStackTrace();}

}

}

No comments:

Post a Comment