Tuesday, April 15, 2014

XML File Reading, Generation and Modification in Java

Reading:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXML {
public static void main(String arg[]) {
try {
File inputfile = new File(System.getProperty("user.dir") + "\\OR.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = dbFactory.newDocumentBuilder();
Document doc = docbuilder.parse(inputfile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nodelist = doc.getElementsByTagName("Object");
System.out.println("##################################");
for (int tmp = 0; tmp < nodelist.getLength(); tmp++) {
Node nNode = nodelist.item(tmp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("object name : " + eElement.getAttribute("name"));
System.out.println(
"propertytype : " + eElement.getElementsByTagName("propertytype").item(0).getTextContent());
System.out.println("propertyvalue: "
+ eElement.getElementsByTagName("propertyvalue").item(0).getTextContent());
System.out.println("--------------------------------------");
}
}
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
}
}
 

Generation:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLFileGeneration {

public static void main(String argv[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("suite");
doc.appendChild(rootElement);
Attr attributeSuite = doc.createAttribute("name");
attributeSuite.setValue("Suite");
rootElement.setAttributeNode(attributeSuite);
attributeSuite = doc.createAttribute("parallel");
attributeSuite.setValue("none");
rootElement.setAttributeNode(attributeSuite);
// staff elements
Element test = doc.createElement("test");
rootElement.appendChild(test);
// set attribute to staff element
Attr attr = doc.createAttribute("name");
attr.setValue("Test");
test.setAttributeNode(attr);
attr = doc.createAttribute("preserve-order");
attr.setValue("true");
test.setAttributeNode(attr);
// firstname elements
Element classes = doc.createElement("classes");
test.appendChild(classes);
// lastname elements
Element class1 = doc.createElement("class");
classes.appendChild(class1);
attr = doc.createAttribute("name");
attr.setValue("com.abc.testscript.javafile");
class1.setAttributeNode(attr);
// nickname elements methods
Element methods = doc.createElement("methods");
class1.appendChild(methods);
// lastname elements
Element include = doc.createElement("include");
methods.appendChild(include);
attr = doc.createAttribute("name");
attr.setValue("MethodName");
include.setAttributeNode(attr);
Element listeners = doc.createElement("listeners");
rootElement.appendChild(listeners);
Element listener = doc.createElement("listener");
listeners.appendChild(listener);
attr = doc.createAttribute("class-name");
attr.setValue("com.abc.listener.TestListener");
listener.setAttributeNode(attr);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(System.getProperty("user.dir") + "/testNG.xml"));
// Output to console for testing
transformer.transform(source, result);
System.out.println(" XML File saved!");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}

Modification:

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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 ModifyXMLFile {

public static void main(String[] args) {
String filePath = "employee.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

// update attribute value
updateAttributeValue(doc);
// update Element value
updateElementValue(doc);
// delete element
deleteElement(doc);
// add new element
addElement(doc);

// write the updated document to file or console
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("employee_updated.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
System.out.println("XML file updated successfully");

} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {

e1.printStackTrace();

}
}

private static void addElement(Document doc) {
NodeList employees = doc.getElementsByTagName("Employee");
Element emp = null;
// loop for each employee
for (int i = 0; i < employees.getLength(); i++) {
emp = (Element) employees.item(i);
Element salaryElement = doc.createElement("salary");
salaryElement.appendChild(doc.createTextNode("10000"));
emp.appendChild(salaryElement);
}
}

private static void deleteElement(Document doc) {
NodeList employees = doc.getElementsByTagName("Employee");
Element emp = null;
// loop for each employee
for (int i = 0; i < employees.getLength(); i++) {
emp = (Element) employees.item(i);
Node genderNode = emp.getElementsByTagName("gender").item(0);
emp.removeChild(genderNode);
}
}

private static void deleteNodes(Document doc) {
NodeList steps = doc.getElementsByTagName("Employee");
for (int i = 0; i < steps.getLength(); i++) {
Node node = steps.item(i);
node.getParentNode().removeChild(node);
}
}

private static void deleteChildNodes(Document doc) {
NodeList steps = doc.getElementsByTagName("Employee");
for (int i = 0; i < steps.getLength(); i++) {
Node node = steps.item(i);
NodeList nList = node.getChildNodes();
for (int j = 0; j < nList.getLength(); j++) {
Node cnode = nList.item(j);
cnode.getParentNode().removeChild(cnode);
}
}
}

private static void updateElementValue(Document doc) {
NodeList employees = doc.getElementsByTagName("Employee");
Element emp = null;
// loop for each employee
for (int i = 0; i < employees.getLength(); i++) {
emp = (Element) employees.item(i);
Node name = emp.getElementsByTagName("name").item(0).getFirstChild();
name.setNodeValue(name.getNodeValue().toUpperCase());
}
}

private static void updateAttributeValue(Document doc) {
NodeList employees = doc.getElementsByTagName("Employee");
Element emp = null;
// loop for each employee
for (int i = 0; i < employees.getLength(); i++) {
emp = (Element) employees.item(i);
String gender = emp.getElementsByTagName("gender").item(0).getFirstChild().getNodeValue();
if (gender.equalsIgnoreCase("male")) {
// prefix id attribute with M
emp.setAttribute("id", "M" + emp.getAttribute("id"));
} else {
// prefix id attribute with F
emp.setAttribute("id", "F" + emp.getAttribute("id"));
}
}
}

}

Comparing 2 images in Java-Selenium

import java.awt.Image; 
import java.awt.Toolkit;  
import java.awt.image.PixelGrabber;  
import java.io.File;

public static String processImage() {
System.out.println("Executing processImage Keyword");
try {
String file1 = "D:\\Images\\images.JPEG";
String file2 = "D:\\Images\\images2.JPEG";
Image pic1= Toolkit.getDefaultToolkit().getImage(file1);
Image pic2= Toolkit.getDefaultToolkit().getImage(file2);
try {
PixelGrabber grab11 = new PixelGrabber(pic1, 0, 0, -1, -1,
false);
PixelGrabber grab21 = new PixelGrabber(pic2, 0, 0, -1, -1,
false);
int[] array1= null;
if (grab11.grabPixels()) {
int width = grab11.getWidth();
int height = grab11.getHeight();
array1= new int[width * height];
array1= (int[]) grab11.getPixels();
}
int[] array2 = null;
if (grab21.grabPixels()) {
int width = grab21.getWidth();
int height = grab21.getHeight();
array2 = new int[width * height];
array2 = (int[]) grab21.getPixels();
}
System.out.println("Pixels equal: "
+ java.util.Arrays.equals(array1, array2 ));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
return "Pass";
} catch (Throwable t) {
// report error
return "Fail - " + t.getMessage();
}
}

Friday, April 4, 2014

Automation Test Estimations




1. Knowledge transfer

Is your team new to the Application under test or would they need training or time with the application to be comfortable with it?
Is your team new to the chosen test automation tool(s)? Would they need training on the tool before they are productive with it?

2. Test automation environment

How long would it take to set up the test environment with the application and the tool for each team member in your test automation team?

3. The chosen tool's compatibility with your application

Do you need to perform a test tool evaluation before you begin automation?
How compatible is the chosen test automation tool with your application's technologies (e.g. does the tool recognize each type of object in your application, does the tool work fast with your application and so on)?

4. Test automation framework

Would you need to create a test automation framework from scratch?
Or
Would you need to use an existing test automation framework? How simple or complex is it to learn to use this existing test automation framework?

5. Test cases to be automated

Would you have the test cases available for automation? Are these test cases automatable?

6. Size of the automation

Considering the size of the test case, the speed of your application and the speed of the chosen test automation tool, how long would it take to automate each test case?
By how much would the usage of your test automation framework affect the effort of automating each test case?

7. Test data requirements

Would test data be available or would your team need to generate own test data?
Where would the test data be located?

8. Types of testing required

What kind of unit tests would be performed on your test automation script?
What kind of integration tests would be performed on the integrated automation scripts?
What kind of tests would be performed to check the validity of test data?
Do you need to create automation only for functional tests? Or for other tests as well e.g. performance tests?

9. Supporting automation

Would your team need to create automation for non-testing activities (e.g. creating a lot of dummy data within the application or emailing test logs to users)?

10. Version control and test automation builds

How slow or fast is the version control?
How long would it take to integrate the test automation created by each team member? How frequently would the test automation build be created?

11. Reviews

What test automation items would be reviewed e.g. test scripts, test data?
What will be the frequency of these reviews?
How long would it take to complete the reviews and re-work?

12. Maintenance

How frequently would the base lined test cases be updated? How long would it take to update your test automation suite in line with these changes?
How frequently would your team receive an updated application build? (Keeping your framework and your chosen tool's capabilities in mind), how long would it take to update your existing test automation?