Selenium Webdriver : Reusable xml parsing class method is not working due to return type unknown

旧时模样 提交于 2019-12-06 15:29:46

问题


My objective is to Create Reusable xml parsing class concerning that return type could be array or arraylist

My code is working but I wanted reusablity I am unable to create reusable class/method due to return type which could array or arraylist is not working.**

1) I have created a xml file as follows:

 <SearchStrings>
    <Search id="1111" type="high">
        <Questions>What is software Testing?</Questions>
        <Tips>How to connect database with eclipse ?</Tips>
        <Multiple>Who was the first prime minister of India? </Multiple>
        <Persons>Who is Dr.APJ Abdul Kalam </Persons>
    </Search>
   <Search id="2222" type="low">
        <Questions>What is Automation Testing?</Questions>
        <Tips>How to use selenium webdriver </Tips>
        <Multiple>Who was the fourth prime minister of India? </Multiple>
        <Persons>Who is Superman? </Persons>
    </Search>    
    <Search id="3333" type="medium">
        <Questions>What is Selenium ide  Testing?</Questions>
        <Tips>How to use selenium webdriver with eclipse  ? </Tips>
        <Multiple>Who was the ninth prime minister of India? </Multiple>
        <Persons>Who is Spiderman? </Persons>
    </Search>  
     <Search id="4444" type="optional">
        <Questions>What is database Testing?</Questions>
        <Tips>How to use Class in java ? </Tips>
        <Multiple>Who was the eight prime minister of India? </Multiple>
        <Persons>Who is motherindia? </Persons>
    </Search>  
</SearchStrings>

2) Creating a class which fetch nodes of tags at once and store all of them in a String [] SearchString and then use this array to fetch the values and by .sendKeys(value) attribute search them at google.

Simplified:

1) Store elements tag element in an reusable datatype my knowledge is limited so using string array. 2) Fetch string array elements and search them using the .sendkeys(element) at google.

my code is as below:

 package searchexperiment;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.concurrent.TimeUnit;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;

    public class Experiment implements Paths
    {
    public static WebDriver driver;
    static Document document;
    static DocumentBuilder db;
    public static void main(String args[])
    {
        String[] SearchStrings;
        driver=new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("https://www.google.com/");

        //loading xml as test data

        WebElement googlebox=driver.findElement(By.id("gbqfq"));
        try {
            FileInputStream file = new FileInputStream(new File(test_xml));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder =  builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            XPath xPath =  XPathFactory.newInstance().newXPath();

            System.out.println("*************************");
            String expression="/SearchStrings/Search/Questions";
            System.out.println("This is ordered expression \n"+expression);
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            for(int i=0;i< nodeList.getLength();i++)
            {
                 // Node nNode = emailNodeElementList.item(j);
                //  Element eElement = (Element) nNode;
                System.out.println("Taking the loop value");
// below push is not working.
                Object array = push(SearchStrings[i],nodeList.item(i).getFirstChild().getNodeValue());
                  String text=nodeList.item(i).getFirstChild().getNodeValue();  
                  googlebox.clear();
                  googlebox.sendKeys(text);
                  System.out.println("Closing the loop value");

            }

I am using the string array in order to make xml parsing class reusable. I have used an interface to get file name

public interface Paths {
String test_xml="XML/Searchtext.xml";
}

回答1:


Reusable method along with class was :

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import searchexperiment.Paths;
public class DocBuilderClass implements Paths
{

public static String[] username() 
{
    String[] SearchElements=new String[4];
    try
    {
        FileInputStream file = new FileInputStream(new File(test_xml));

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder =  builderFactory.newDocumentBuilder();
         Document xmlDocument = builder.parse(file);

        XPath xPath =  XPathFactory.newInstance().newXPath();

        System.out.println("*************************");
        String expression="/SearchStrings/Search/Tips";
        System.out.println("This is ordered expression \n"+expression);
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
        //int size=
        for(int i=0;i< nodeList.getLength();i++)
        {
             // Node nNode = emailNodeElementList.item(j);
            //  Element eElement = (Element) nNode;
            System.out.println("Taking the loop value");
            //Object array = push(SearchStrings[i],nodeList.item(i).getFirstChild().getNodeValue());
              String text=nodeList.item(i).getFirstChild().getNodeValue();  
              //googlebox.clear();
             // googlebox.sendKeys(text);
              SearchElements[i]=text;
              System.out.println("Closing the loop value");

        }

    }
    catch(Exception ex)
    {
    System.out.println("This is a exception" + ex);
    }
    finally
    {

    }
    return SearchElements;

}   
}

and then way to call the class was as follows:

String [] namelist=DocBuilderClass.username();
    for(int i=0;i<namelist.length;i++)
    {

        String abc=namelist[i];

        googlebox.sendKeys(abc);
        googlebox.clear();
        googlebox.sendKeys(namelist[i]);


    }

References were Reference Link String[] array

Reference Link XML Parsing

All I learn that Your basics should be strong to solve a strong and complex problems.



来源:https://stackoverflow.com/questions/25746375/selenium-webdriver-reusable-xml-parsing-class-method-is-not-working-due-to-ret

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!