简体   繁体   中英

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

I am having a static html page on local storage(hard disk drive). I am not using any kind of server here. Along with this I have an xml file named testng.xml which I need to edit from html on button click using javascript.

I need to replace the <include> tag with <exclude> tag or vice versa based on user selection through a checkbox on web page. And then execute the testng xml by a button click.

The function is called from the html button as below

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

Given below is my code for editing the 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>

The output on html page after clicking the button is as below

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

I am able to retrieve the xml file and elements in the z and y variables. Also in the 2nd for loop, the output containing xml node names and attributes is printed. But after getting xml data in y variable, I am not able to get or use any html page elements such as checkboxes.

The value of x.length is 140 initially and y.length is 7, but then x.length becomes 0 and my 1st for loop does not execute.

I am unable to understand why I can't access html elements after getting xml data.

I never tried but as per my understanding you can create a bash file and then exucute that bash file using jsp code. Add that jsp code in the HTML.

Refer:-

executing a bash script from a jsp page

Now as you said "I need to prepare a user interface in HTML with checkboxes to select or deselect the tests to run and execute them with click of a button"

For that may you need to create different testng suite and accordingly you need to create a more bash respectively.

Refer:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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