繁体   English   中英

使用javascript从静态HTML页面编辑本地xml文件时出错

[英]Error in editing a local xml file from a static HTML page using javascript

我在本地存储(硬盘驱动器)上有一个静态html页面。 我在这里没有使用任何类型的服务器。 与此同时,我有一个名为testng.xml的xml文件,我需要使用javascript在按钮单击时从html编辑该文件。

我需要根据用户通过网页上的复选框进行的选择,将<include>标记替换为<include> <exclude>标记,反之亦然。 然后通过单击按钮执行testng xml。

从html按钮调用该函数,如下所示

<input type="submit" onclick="editTestNG()" value="Execute Selected Tests">

下面给出的是我用于编辑xml的代码。

<script type="text/javascript">

function editTestNG() 
{

  xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET", "testng.xml", false);
  xmlHttp.send();

  xmlDoc = xmlHttp.responseXML;
  txt = "";

  x = document.getElementsByName("check");
  document.write(x.length + "<br>");  // x.length is number of checkboxes on html page

  z = xmlDoc.getElementsByTagName("run")[1];
  y = z.children;
  document.write(y.length + "<br>"); // y.length is the number of xml child nodes 

  document.write(x.length + "<br>");  // x.length is number of checkboxes on html page

  for(i=0;i<x.length;i++)
  {
    document.write("abc");

    if(x[i].type == 'checkbox')
    {  
        document.writeln("def");

        if(x[i].checked == "true" && y[i].nodeName == "exclude")
        {
            document.writeln("ghi");

            txt = y[i].getAttribute("name");          

            newTag = xmlDoc.createElement("include");
            newTag.setAttribute("name",txt);
            p = z.replaceChild(newTag, y[i]); 
            document.write(p.nodeName+"<br>")
            document.write(newTag.nodeName + "::"+ newTag.getAttribute("name")+"<br>");
        }

        if(x[i].checked == "false" && y[i].nodeName == "include")
        {
            document.writeln("jkl");

            txt = y[i].getAttribute("name");          

            newTag = xmlDoc.createElement("exclude");
            newTag.setAttribute("name",txt);
            p = z.replaceChild(newTag, y[i]); 
            document.write(p.nodeName+"<br>")
            document.write(newTag.nodeName + "::"+ newTag.getAttribute("name")+"<br>");
        }
    }
}
  y = z.children;

  for(i=0;i<y.length;i++)
  {
    document.writeln("<br>"+y[i].nodeName + " ## "+ y[i].getAttribute("name")+"<br>");
  }

}

</script>

单击按钮后,html页面上的输出如下

140  
7    
0    

exclude ## Group_CCMS_Login

exclude ## Group_CCR_Create_New

exclude ## Group_CCR_Create_New_EngType_M

exclude ## Group_CCR_Create_New_EngType_P

exclude ## Group_Full_ID_Search

include ## Group_Run_Query

exclude ## Group_Dup_Undup

我能够在z和y变量中检索xml文件和元素。 同样在第二个for循环中,将输出包含xml节点名称和属性的输出。 但是在y变量中获取xml数据后,我无法获取或使用任何html页面元素(例如复选框)。

x.length的值最初为140, y.length值为7,但随后x.length变为0,并且我的1st for循环不执行。

我无法理解为什么获取XML数据后无法访问html元素。

我从未尝试过,但是据我了解,您可以创建一个bash文件,然后使用jsp代码将其删除。 在HTML中添加该jsp代码。

参考:-

从jsp页面执行bash脚本

现在,正如您所说的:“我需要准备一个带有复选框的HTML用户界面,以选择或取消选择要运行的测试,只需单击一个按钮即可”

为此,您可能需要创建不同的testng套件,因此需要分别创建更多的bash。

参考:-

http://grokbase.com/t/gg/selenium-users/139r0nszp3/how-to-run-multiple-xml-files-with-testng

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM