Forum Discussion

brooksd's avatar
brooksd
Occasional Contributor
11 years ago
Solved

Issues with Firefox EvaluateXPath on First Paragraph Only!






Hello TC forums! First time poster here





Consider the code below.  This code is meant to search any wikipedia page and find all of the links in the first paragraph (in this case I hardcoded the URL in and specified the first paragraph directly).  The code works by using the findAllChildren method to find all of the elements in the main area with text in then.  Near the end of the array is the first paragraph, on which I use an XPath expression to create an array with the links.

 

function Test()

{

   // Obtain the Page object

   var url = "http://en.wikipedia.org/wiki/Siyaram_Gangwar";

 

   Browsers.Item(btFirefox).Run(url);

 

   var page = Sys.Browser("*").Page("*").Panel("content").Panel("bodyContent").Panel("mw_content_text");

 

   var tmp = page.FindAllChildren("ObjectType", "TextNode", 1);

  

   // Check the result

   if (tmp != null)

   {

     // Convert the array to the JScript-compatible format

     var arr = (new VBArray(tmp)).toArray();

    

     for (var i = 0; i< arr.length; i++)

      Log.Message(arr.innerText);

 

      //for this particular page the element we want is the 13th item

     var newArray = arr[12].EvaluateXPath("(/a|/a/b)[contains(@href, 'wiki')]");

   

     var newArray2 = (new VBArray(newArray)).toArray();

    

     for (var i = 0; i< newArray2.length; i++){

        Log.Message("Link " + i +":" + newArray2.innerText);

     }

   }

   else

   {

     // If nothing was found, post a message to the log

     Log.Error("Nothing was found.");

   }

}



The Result:  “An unexpected error has occurred”.  Now try changing btFirefox to btChrome and it will work fine!  Try switching back to FF then using the second paragraph instead of the first (use arr[8] instead of arr[12]) and it will find all of the links in that paragraph!  If you throw a breakpoint in there you can see that newArray2 is an array of Objects each one is a link with many members.  Now switch it back to the first paragraph and you can see these Objects have no members or information in them at all!  The right number of links is there (4) but there is no info in each one.



Is this something I am coding wrong, an issue with Firefox, or an issue with TestComplete10?  I am perplexed as to why this works in some cases and not others.  Try other paragraphs on other wikis and you will see what I mean.

Any help with this is much appreciated!



Additional info: Windows 7, TestComplete10, Firefox 26.0, Chrome 32

  • Hi,


     


    To answer all your questions, as far as I can see, the Support Team created an issue in the bug-tracking system. Our R&D team will investigate this behavior. At the moment, it's possible to say that it's specific only to Firefox.


     

10 Replies

  • Hello Brooks,



    What is exactly the error it returns? And in which line? You can see the full description in the log.



    Also, make sure that the DOM tree is the same in both firefox and chrome, it may be different.
  • brooksd's avatar
    brooksd
    Occasional Contributor


    Try it! The line that generates the error is here:



    Log.Message("Link " + i +":" + newArray2.innerText);



    The error is generated because the arrays objects have no data or members in them, even though they definitely should be fine. The exact message: "An unspecified error has occured"



    From the log's additional info:




    Unspecified error

    Error location:

    Unit: "TestTests\PhilosophyWiki\Script\XPathTest"

    Line: 73 Column: 9.




    And yes, same exact page structure in the object browser.



































    ...



     

  • Hello Mark,



    Sorry I didn't try it myself, I didn't have TC accessible.



    I've been taking a look into it. I've only tried on IE10 and FF26 as I have TC9, and I was able to reproduce it.



    The thing is, if you use the Object Browser and check the arr[12] object (its FullName is Sys.Browser("*").Page("*").Panel("content").Panel("bodyContent").Panel("mw_content_text").TextNode(1), and it's arr[14] element in IE), you will see that according to Test Complete it has no children.



    So, it looks like a bug in Test Complete, for some reason I don't really understand. You should get in contact with Test Complete support team and let them know.



    Attached you will find a screenshot with those objects marked.



    Let us know what happens!
  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi,


     


    Let me share the solution from the Support:


     


    This happens because, in the Firefox object model, the TextNode type of items doesn't have child objects. As a solution, you can use the following code which will search for both paragraphs, and links in them:


     




    function Test()


    {


       // Obtain the Page object



     


       Browsers.Item(btFirefox).Run(url);


     


       var page = Sys.Browser("*").Page("*").Panel("content").Panel("bodyContent").Panel("mw_content_text");


     


      {


         var newArray = page.EvaluateXPath("p[1]/a[contains(@href, 'wiki')]");


     


         var newArray2 = (new VBArray(newArray)).toArray();


     


         for (var i = 0; i< newArray2.length; i++){


            Log.Message("Link " + i +":" + newArray2.innerText);


         }


       }


       else


       {


         // If nothing was found, post a message to the log


         Log.Error("Nothing was found.");


       }


    }




     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Tanya,



    > [...]  in the Firefox object model, the TextNode type of items doesn't have child objects.

    Could you please clarify, should this be read as

    - It is not possible from the FF object model to get TextNode children (i.e. the problem is on the FF side)

    or

    - For FF TestComplete does not provide children for the nodes of TextNode type (i.e. the problem is on TestComplete side)

    ?
  • brooksd's avatar
    brooksd
    Occasional Contributor
    Let me just preface this by saying: this was not a super important test case, just a learning excersize.  My goal was to find the first non italicized, non spanning link on the page.  Sometimes this link occurs in the first paragraph, sometimes in the second paragraph, sometimes in a List Item tag.



    Consider the page: http://en.wikipedia.org/wiki/Problem



    If you used the code provided by smart bear you not be able to get the desired link, which is "Mathematical Problem".  The link is in the first list item after the first paragraph.



    My solution origionally was a big, long XPATH expression that found all of the links on the page and decided which one to click but this is extremely slow, especially on large pages (like Germany's page). My solution then was to get all of the paragraphs and listItems with substantial text in them by findallchildren on textNode, then use xpath on each individual textNode element to see if it has what we needed.  I greatly simplified my code for this posting, but this method was much faster.



    SmartBear support: this is not what I was trying to do at all. Also, the textNode elements DO HAVE children.  In my OP, if you change the array's index to evaluateXPath on the second paragraph you will see it finds the links there perfectly okay!  You can see the children in the debugger! Why does the first paragraph, and only the first one, give you this strange error?  That is the question.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi,


     


    To answer all your questions, as far as I can see, the Support Team created an issue in the bug-tracking system. Our R&D team will investigate this behavior. At the moment, it's possible to say that it's specific only to Firefox.


     

  • brooksd's avatar
    brooksd
    Occasional Contributor
    It appears this has been fixed in the latest Update... Thanks!  will test soon...
  • redwoodi's avatar
    redwoodi
    New Contributor
    I have an Evaluate XPath error in Internet Explorer too!