Total Pageviews

Friday, 19 September 2014

Read XML file using java

Java Program to read or.xml file
package programs;

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 ORxml {
    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();
        }
    }

}

Database Connection using Java and Selenium

Database Connection using Java and Selenium

// import sql package
import java.sql.*;

//http://docsrv.sco.com/JDK_guide/jdbc/getstart/callablestatement.doc.html
public class Database_connection {


public static void main(String[] args) throws SQLException {

 Connection conn = null;
 String url = "jdbc:mysql://localhost:3306/";
 String dbName = "test";
 String driver = "com.mysql.jdbc.Driver";
 String userName = "root";
 String password = "root";

 try{
 Class.forName(driver).newInstance();// create object of Driver
 conn = DriverManager.getConnection(url+dbName,userName,password);
 // connection will be established

 // *******************Statement******************
 Statement stmt = conn.createStatement();
 ResultSet rs = stmt.executeQuery("select * from users");

//  rs.next(); // 1st row
//  System.out.println(rs.getString(2));
//  rs.next(); // 2nd row
//  System.out.println(rs.getString(1));
 while(rs.next()){
System.out.println(rs.getString(1) + "-- "+rs.getString(2)+" -- "+rs.getString(3));
 }

 System.out.println("*********************************");
 // *****************PREPARED STATEMENT**************
 PreparedStatement pstmt = conn.prepareStatement("select * from users where name = ? and sex=?");
 pstmt.setString(1, "B");
 pstmt.setString(2, "F");
 ResultSet rs1 = pstmt.executeQuery();

 while(rs1.next()){
System.out.println(rs1.getString(1) + "-- "+rs1.getString(2)+" -- "+rs1.getString(3));
 }


//***************Callable Statement************************
 //CallableStatement cstmt = conn.prepareCall("{call getTestData(?,?,?,?)}");
   //cstmt.registerOutParameter(1, java.sql.Types.DECIMAL, 3);
   //cstmt.setString(2, "xxxxx");
 
 
   //cstmt.executeUpdate();
  // double d =cstmt.getDouble(1);

//     //********************Add row Insert************************
   pstmt = conn.prepareStatement("insert into users values (?,?,?)");
   pstmt.setString(1, "Tom");
   pstmt.setString(2, "London");
   pstmt.setString(3, "M");
 
   int i=pstmt.executeUpdate();
   if(i==1){
    System.out.println("inserted the record");
   }
 

 }catch(Exception e){
  e.printStackTrace();
 }finally{
 conn.close();
 }
}

}

happy learning...

Comparing 2 images in java-Selenium

Here is a function to Compare two images in JAVA.


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();
}

}

Web Site URL 's for selenium informaiton.....

1. http://www.studyselenium.com/

2.http://automationtricks.blogspot.in/

visit these blogsposts for more informaiton.....

Wednesday, 10 September 2014

How to take a screenshot in selenium webdriver

Topic: How to take a screenshot in selenium web driver


Code: i have implemented in TestNG method

 @Test
  public void takeScreenShotTest() throws IOException
  {
 WebDriver driver=new FirefoxDriver();
driver.get("https://google.co.in");
File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:\\Users\\Dell\\Desktop\\GoogleScreenShot.jpg"));
  }


output:

How To record the Test Execution as a video

Topic: How to record the Test Execution as a .mov file

steps: we just have to add 3 lines of code for

1. Setting up a recorder

2. Starting the recorder

3. Stopping the recorder


code:

ATUTestRecorder recorder = new ATUTestRecorder("Directory","filename",false);

recorder.start();

WebDriver browser = new FirefoxDriver();

browser.navigate().to("http://www.w3schools.com/ajax/ajax_database.asp");

browser.manage().window().maximize();

browser.quit();

recorder.stop();





Precondition:    Import the .JAR file and add to Eclipse

download link: https://drive.google.com/folderview?id=0B7rZvkq9tkwPUHRXNU8wQjRYVDA&usp=sharing&tid=0B7rZvkq9tkwPRy1HLVJCdWtNekE
procedure:
Right click on project -- Build Path -- >  configure Build Path ---- > Libraries  -----> Click on "Add external jars " --- > select ATUTestrecorder.jar fille -----> click on "ok"


HAPPY RECORDING......





How to read xml file using java.

Example XML fiile shown bellow.

<class> //It is root element

<student rollno='001'>
<firstname>Parimi</firstname>
<lastname>satish</lastname>
<nickname>pandu</nickname>
</student>

<student rollno='002'>
<firstname>maha</firstname>
<lastname>madi</lastname>
<nickname>dinga</nickname>
</student>

</class>



----------------------------------------------------------------------------------------------------------------------
use the bellow code, and change the file path..

import java.io.File;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadCustomXMLFile
{

public static void main(String [] args)
{
try
{
File fXmlFile = new File("E:\\tutorials\\Selenium Videos\\xml filehandling\\testdata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

//To get Root element i.e class  ---- in testda.xml file
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());


// get no of sub - nodes i.e "student" nodes in the xml file
NodeList nList = doc.getElementsByTagName("student");
System.out.println(nList.getLength());


//get all the attribute values in the both sub nodes...

for(int temp=0;temp<=nList.getLength();temp++)
{

//to Get all sub-nodes names use bello code....
Node nNode = nList.item(temp);
System.out.println("Sub Node Name is :  "+nNode.getNodeName());


//

if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;


//to get roll no of the  student
System.out.println("Student ID is :"+eElement.getAttribute("rollno"));


//to get firstname of the student
System.out.println("First Name is: "+eElement.getElementsByTagName("firstname").item(0).getTextContent());

//To get Last name of the Student
System.out.println("Last Name is :"+eElement.getElementsByTagName("lastname").item(0).getTextContent());

//To get Nick Name of the Student is
System.out.println("Nick Name is :"+eElement.getElementsByTagName("nickname").item(0).getTextContent());

}

}

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

}
}