硒的webdriver:可重复使用的XML解析类的方法是行不通的,由于返回类型未知 [英] Selenium Webdriver : Reusable xml parsing class method is not working due to return type unknown

查看:216
本文介绍了硒的webdriver:可重复使用的XML解析类的方法是行不通的,由于返回类型未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建有关的返回类型可以是数组或ArrayList的可重复使用的XML解析类

我的code是工作,但我想reusablity我无法创建可重用的,由于类/方法返回可能数组或ArrayList是不工作的类型。**

1)我创建了一个xml文件如下:

 < SearchStrings>
    <搜索ID =1111TYPE =高>
        <问题>什么是软件测试<?/问题与GT;
        <提示和GT;如何连接,用eclipse数据库<?/提示>
        <多>谁是印度首任总理? < /多>
        <&人士GT;谁是Dr.APJ卡拉姆< /人>
    < /搜索>
   <搜索ID =2222式=低>
        <问题>什么是自动化测试<?/问题与GT;
        <提示和GT;如何使用硒的webdriver< /提示>
        <多>谁是印度的第四个首相? < /多>
        <&人士GT;谁是超人? < /人>
    < /搜索>
    <搜索ID =3333式=中等>
        <问题>什么是硒的IDE测试与LT;?/问题与GT;
        <提示和GT;如何使用硒的webdriver与Eclipse? < /提示>
        <多>谁是印度的第九总理? < /多>
        <&人士GT;谁是蜘蛛侠? < /人>
    < /搜索>
     <搜索ID =4444TYPE =可选>
        <问题>什么是数据库测试<?/问题与GT;
        <提示和GT;如何在Java中使用类? < /提示>
        <多>谁是印度的八个首相? < /多>
        <&人士GT;谁是印度母亲? < /人>
    < /搜索>
< / SearchStrings>

2)创建这一次获取的标签节点,他们都存储在一个类
一个String []搜索字符串,然后用这个数组来获取价值观和.sendKeys(值)属性在谷歌搜索。

简体:

在我的知识有限,因此使用字符串数组的可重复使用的数据类型的 1)店铺元素标记元素。
2)获取字符串数组的元素,并在谷歌使用.sendkeys(元素)搜索。

我的code是如下:

 包searchexperiment;
    进口的java.io.File;
    进口java.io.FileInputStream中;
    进口java.io.FileNotFoundException;
    进口java.io.IOException异常;
    进口java.util.concurrent.TimeUnit中;    进口javax.xml.parsers.DocumentBuilder中;
    进口javax.xml.parsers.DocumentBuilderFactory中;
    进口javax.xml.parsers.ParserConfigurationException;
    javax.xml.xpath.XPath中的进口;
    进口javax.xml.xpath.XPathConstants;
    进口javax.xml.xpath.XPathEx pressionException;
    进口javax.xml.xpath.XPathFactory;    进口org.openqa.selenium.By;
    进口org.openqa.selenium.WebDriver;
    进口org.openqa.selenium.WebElement;
    进口org.openqa.selenium.firefox.FirefoxDriver;
    进口org.w3c.dom.Document中;
    进口org.w3c.dom.NodeList;
    进口org.xml.sax.SAXException;    公共类实验实现路径
    {
    公共静态的webdriver驱动器;
    静态文档文件;
    静态的DocumentBuilder分贝;
    公共静态无效的主要(字符串ARGS [])
    {
        的String [] SearchStrings;
        司机=新FirefoxDriver();
        driver.manage()超时()implicitlyWait(50 TimeUnit.SECONDS)。
        driver.get(https://www.google.com/);        //加载XML作为测试数据        WebElement googlebox = driver.findElement(By.id(gbqfq));
        尝试{
            的FileInputStream文件=新的FileInputStream(新文件(TEST_XML));            的DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();            的DocumentBuilder建设者= builderFactory.newDocumentBuilder();            文档的XmlDocument = builder.parse(文件);            XPath的XPATH = XPathFactory.newInstance()newXPath()。            的System.out.println(********);
            字符串前pression =/ SearchStrings /搜索/问题;
            的System.out.println(这是命令前pression \\ n+ EX pression);
            节点列表节点列表=(节点列表)xPath.compile(如pression)评估(XMLDOCUMENT,XPathConstants.NODESET);
            的for(int i = 0; I< nodeList.getLength();我++)
            {
                 //节点nNode = emailNodeElementList.item(J);
                //元素eElement =(元)nNode;
                的System.out.println(采取循环值);
//下面推不工作。
                对象数组=推(SearchStrings [I],nodeList.item(ⅰ).getFirstChild()getNodeValue());
                  串文本= nodeList.item(ⅰ).getFirstChild()getNodeValue();
                  googlebox.clear();
                  googlebox.sendKeys(文本);
                  的System.out.println(结束循环值);            }

我使用的字符串数组,以使XML解析类重用。
我用一个接口来获取文件名

 公共接口路径{
字符串TEST_XML =XML / Searchtext.xml
}


解决方案

可重复使用的带班一起方法:

 进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.IOException异常;进口javax.xml.parsers.DocumentBuilder中;
进口javax.xml.parsers.DocumentBuilderFactory中;
进口javax.xml.parsers.ParserConfigurationException;
javax.xml.xpath.XPath中的进口;
进口javax.xml.xpath.XPathConstants;
进口javax.xml.xpath.XPathFactory;进口org.w3c.dom.Document中;
进口org.w3c.dom.NodeList;
进口org.xml.sax.SAXException;进口searchexperiment.Paths;
公共类DocBuilderClass实现路径
{公共静态的String []用户名()
{
    的String [] = SearchElements新的String [4];
    尝试
    {
        的FileInputStream文件=新的FileInputStream(新文件(TEST_XML));        的DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();        的DocumentBuilder建设者= builderFactory.newDocumentBuilder();
         文档的XmlDocument = builder.parse(文件);        XPath的XPATH = XPathFactory.newInstance()newXPath()。        的System.out.println(********);
        字符串前pression =/ SearchStrings /搜索/提示;
        的System.out.println(这是命令前pression \\ n+ EX pression);
        节点列表节点列表=(节点列表)xPath.compile(如pression)评估(XMLDOCUMENT,XPathConstants.NODESET);
        // INT大小=
        的for(int i = 0; I< nodeList.getLength();我++)
        {
             //节点nNode = emailNodeElementList.item(J);
            //元素eElement =(元)nNode;
            的System.out.println(采取循环值);
            // Object数组=推(SearchStrings [I],nodeList.item(ⅰ).getFirstChild()getNodeValue());
              串文本= nodeList.item(ⅰ).getFirstChild()getNodeValue();
              //googlebox.clear();
             // googlebox.sendKeys(文本);
              SearchElements [I] =文本;
              的System.out.println(结束循环值);        }    }
    赶上(异常前)
    {
    的System.out.println(这是一个例外,+ EX);
    }
    最后
    {    }
    返回SearchElements;}
}

然后方式来调用类如下:

 的String [] =的namelist DocBuilderClass.username();
    的for(int i = 0; I< namelist.length;我++)
    {        字符串ABC =名称列表[I]        googlebox.sendKeys(ABC);
        googlebox.clear();
        googlebox.sendKeys(名称列表[我]);
    }

的参考文献参考链接String []数组

参考链接XML解析

我只知道你的基础知识要牢固,解决一个强大而复杂的问题。

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";
}

解决方案

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.

这篇关于硒的webdriver:可重复使用的XML解析类的方法是行不通的,由于返回类型未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆