Friday 24 January 2014

Java program to read xml file-Object-Repository.xml


OR.xml is below:
<?xml version="1.0" encoding="UTF-8"?>


<OR>
    <Page name="page1">
        <Object name = 'pg1obj1'>
            <propertytype>id </propertytype>
             <propertyvalue>//*[@id='pg1obj1'] </propertyvalue>
        </Object>
        <Object name = 'pg1obj2'>
            <propertytype>classname </propertytype>
             <propertyvalue>//*[@classname='pg1obj2'] </propertyvalue>
        </Object>
    </Page>
   
    <Page name="page2">
         <Object name = 'pg2obj1'> 
                <propertytype>xpath </propertytype>
                <propertyvalue>//*[@xpath='pg2obj1']</propertyvalue>         
            </Object>
            <Object name = 'pg2obj2'>
            <propertytype>linktext </propertytype>
             <propertyvalue>//*[@linktext='pg2obj2'] </propertyvalue>
        </Object>   
    </Page>
</OR>


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

}



Back to selenium interview questions and Answers

Back to Java interview questions and Answers


Tuesday 7 January 2014

difference between single and double slash in Xpath



/
1.It starts selection from the document node
2. It Allows you to create 'absolute' path expressions
3. e.g “/html/body/p” matches all the paragraph elements
 //
1. It starts selection matching anywhere in the document
2. It Allows you to create 'relative' path expressions
3. e.g“//p” matches all the paragraph elements


Selenium Mobile Automation



Some Good urls's till the time i write custom document for it.
https://code.google.com/p/selenium/wiki/AndroidDriver
http://manojhans.blogspot.in/2013/08/native-android-apps-automation-with.html


Back to Selenium Interview Questions and Answers


exceptions in selenium and code to resolve it

Name 5 different exceptions you had in selenium web driver and mention .What instance you got it and how do you resolve it?
·           WebDriverException
·           NoAlertPresentException
·           NoSuchWindowException
·           NoSuchElementException
·           TimeoutException
 • WebDriverException
 WebDriver Exception comes when we try to perform any action on the non-existing
driver.
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.close();
driver.quit();

• NoAlertPresentException
When we try to perform an action i.e., either accept() or dismiss() which is not required
at a required place; gives us this exception.
try{
driver.switchTo().alert().accept();
}
catch (NoAlertPresentException E){
E.printStackTrace();
}
• NoSuchWindowException
 When we try to switch to an window which is not present gives us this exception:
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.switchTo().window("Yup_Fail");
driver.close();
In the above snippet, line 3 throws us an exception, as we are trying to switch to an
window that is not present.• NoSuchFrameException
• Similar to Window exception, Frame exception mainly comes during switching between
the frames.
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.switchTo().frame("F_fail");
driver.close();
In the above snippet, line 3 throws us an exception, as we are trying to switch to an
frame that is not present.
• NoSuchElementException
 This exception is thrown when we WebDriver doesn’t find the web-element in the DOM.
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.findElement(By.name("fake")).click();
• TimeoutException
 Thrown when a command does not complete in enough time.
All the above exceptions were handled using try catch exceptions.

  

How to ZIP files in Selenium




// Sample Function to make zip of reports
public static void zip(String filepath){
try
{
File inputFolder=new File('Mention file path her");
File outputFolder=new File("Reports.zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFolder)));
BufferedInputStream in = null;
byte[] data  = new byte[1000];
String files[] = inputFolder.list();
for (int j=0; j<files.length; i++)
{
in = new BufferedInputStream(new FileInputStream
(inputFolder.getPath() + "/" + files[j]), 1000);
out.putNextEntry(new ZipEntry(files[j]));
int totalcount;
while((totalcount= in.read(data,0,1000)) != -1)
{
out.write(data, 0, totalcount);
}
out.closeEntry();
  }
  out.flush();
  out.close();
}
  catch(Exception e)
  {
 e.printStackTrace();
           return "Fail - " + e.getMessage();
  }
 }


Back to Selenium Interview questions and Answers

set the execution order for TestNg Testcases.

In TestNG I have some test's  Test1-Test2-
Test3-Test4-Test5I want to run my execution 
order is Test5-Test1-Test3-Test2-Test4.How 
do you set the execution order can you explain 
for that?

package programs;import org.testng.annotations.Test;public class testngexecution { @Test(priority=2)public void test1(){System.out.print("Inside Test1"); }@Test(priority=4)public void test2(){System.out.print("Inside Test2"); }@Test(priority=3)public void test3(){System.out.print("Inside Test3"); }@Test(priority=5)public void test4(){System.out.print("Inside Test4"); }@Test(priority=1)public void test5(){System.out.print("Inside Test5"); }


Back to Selenium Interview Questions and Answers

How to take the screen shots in seelnium 2.0



// store screenshots
public static void captureScreenShot(String filePath) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
   try {
FileUtils.copyFile(scrFile, new File(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
   }

Back to Interview Questions and Answers