Forum Discussion

sinduja_venkata's avatar
sinduja_venkata
Occasional Contributor
10 years ago

extract text from webpage and store it in a variable

Hi, I need to use TC to extract text from a web page control and store it in a variable, so that I can check for that same text elsewhere in the test. I am using JScript and not keyword driven tests. Anmy help on this would be much appreciated. Thanks.

6 Replies

  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor

    Hi



    I've had a look at the SmartBear web page you referred to and created the script below as an example of how to store the text from the bulleted items on the left-hands side of the page.  Please forgive the long line of code where I've created a new Enumerator - I didn't want to start mapping items, etc. to make this shorter, but you can do this for your own script.



    function myFunction()
    {
       //I am using an array to store the bulleted text items.
      
    //This will allow for different numbers of items on each run.

       va
    r itemsEnum = new Enumerator(Aliases.browser.Page("http://smartbear.com/product/testcomplete/overview/").Form("form").Panel("container").Panel("main").Panel("content").Panel(0).Panel(2).Panel(0).Panel(0).Panel(0).Panel(0).TextNode(2));
      
    var item; //a reference to each item in the Enum
      
    var arrItems = []; //array to hold the ContentText property value of each item.

      
    //populate the array
      
    while (! itemsEnum.atEnd())
      
    {
         
    item = itemsEnum.item();
         
    arrItems[arrItems.length] = item.contentText;
         
    itemsEnum.moveNext();
      
    }

      
    //output the array contents
       f
    or (var i = 0; i < arrItems.length; i++)
      
    {
         
    Log.message(arrItems[i]);
      
    }
    }





    Hope this is helpful.



    Regards

    Stephen.

  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor
    Hi



    You've not given us anything to work with unfortunately, i.e. what type of control, but typically, you need to create a variable and simply assign the 'Text', 'Value' or ContentText value to it.  For example:





    var myControl = <a reference to the control here>;

    var myVar = myControl.Text;   //or var myVar = myControl.Value, etc.





    You will also need to consider the scope of the variable in deciding where you declare it.  If you will be needing to use it in other methods, for example, you need to declare it outside of any method.  If you need it to hold its value between test runs, etc. you will need to use project suite variable declaration, etc., etc.  You should be able to get information on this quite easily in the help files.



    Hopefully this is of some assistance.



    Regards

    Stephen.
  • sinduja_venkata's avatar
    sinduja_venkata
    Occasional Contributor
    Thanks for the response and sorry for not being specific. Here are much more details as what i was looking for:

     

    Let’s take an example:



    1.       Navigate to http://smartbear.com/product/testcomplete/overview/

     

    2.       In this web page, there is a paragraph of text displayed under ‘Overview’ section along with bulleted items. Imagine these are dynamic and changes every now&then. Now I want to store the value displayed here into a variable and also each one of the bullet points [Ex: Test recording, Automated test execution] into separate variables

     

    This is what I meant by saying “extracting text from webpage and storing it in a project variable using Jscript".

     

    Please let me know if this clarifies.



    Thanks in advance.
  • So you need to identify the elements you want - using some sort of search if they're dynamic - and then extract their text content as outlined in post #2.



    This is pretty basic stuff. If you're not sure what you're doing here, you may want to do a little more reading and work through some examples ....