Forum Discussion

TanyaYatskovska's avatar
TanyaYatskovska
SmartBear Alumni (Retired)
8 years ago
Solved

Code Contest: Export/Importing Project Variables

Hello Community,

 

We are announcing the first Code Contest in our TestComplete Community. All of you can take part in the contest to check/improve your skills and win the main prize. I hope for fair play and serious competition :)

 

Contest Details

Create a script extension that will allow exporting/importing project variables.

 

To implement the task, you need to have basic coding skills, know how to use the TestComplete objects and be able to work with files from script.

You can use any script language to create a script extension. This is one of the advantages of using script extensions – they don’t depend on a particular script language used in a project.

 

Contest Duration

We will be waiting for your solutions till August 31. The winner will be announced shortly after that.

 

Contest Prize

The winner gets a $50 gift card.

 

Contest Storage

Once you create a ready script extension, post it as a reply to this thread. Others will be able to vote for your solution.

 

Contest Guideline

To help you create a script extension, I will specify some tips below. However, if you want to implement that task on your own – feel free to do so. You will need to post a working solution in the end.

 

Here are the steps of how this can be done:

  1. Code Creation:

You will need to declare at least two functions. You can do this in Code Editor and, then, move them to a script extension later.

* Export variables to an external storage (for example, to a file) – you will need to export their types and values via  GetVariableType and VariableByName. Here is pseudo code written by cunderw:

 

 

function outputVars() {
   var count = Project.Variables.VariableCount;
   var name, type,value;
   for(var i = 0; i < count; i++){
      name = Project.Variables.GetVariableName(i);
      value = Project.Variables.VariableByName(name);
      type = Project.Variables.GetVariableType(Project.Variables.VariableByName(name))
     //code to write to file
   }
}

 

* Import variables from the storage and add them to your project via AddVariable. Here is pseudo code written by cunderw:

 

  1. function insertVars() {
       //code to open file
      for(lines in file) {
        if(!Project.Variables.VariableExists(varNameFromFile){
          //add var and type
       }
       //set var value etc..
      }
    }
    2. Script Extension Creation:
    1. Create a script extension and move the code there
    2. Create a GUI for your script extension by using TestComplete’s User Form
  2. Adding the script extension to TestComplete’s toolbar.

You can find the detailed description of the past two steps in the Creating Actions Tutorial article.

 

Good luck and may the odds be ever in your favor!

  • TanyaYatskovska's avatar
    TanyaYatskovska
    8 years ago

    Hello Community,

     

    At the beginning of my speech, I want to say big thanks to everyone who took part in our first Code Contest!

    It was difficult for us to choose the winner as all of your works are great and worth using in TestComplete. To make the ultimate decision, we took into account these three criteria: functionality, extension’s GUI and usability.

     

    Here are the extensions that made it to the final posted by:

    cunderw

    StevenN

    baxatob

     

     

    And, the Oscar goes to… oops, sorry… and, the winner is StevenN!

     

    Congrats, Steven!

    I will contact you shortly to describe how you can get the main prize – a $50 gift card.

     

    Please join me in saying thank you to all the participants and congratulating Steven.

     

     

    And, be ready for the next contest…

51 Replies

  • Attached an update after receiving the feedback:

     

    1. Now importing from and exporting to an XML file so there should be no issues with commas. 

    2. You can already select a folder to export to by double clicking the text box.

    3. You can now select which variables you want to export. I used a listbox rather than checkboxes as its better for selecting multiple variables:

     

     

    Any more challenges?

     

    Thanks,

    Steven

  • baxatob's avatar
    baxatob
    Community Hero

    Hi guys!

     

    Please meet my beta :)

     

    Code to export variables:

     

    function export_variables()
    {
        var var_count = Project.Variables.VariableCount;
        var name, type, value;
        var path, fso, file;
        
        UserForms.ctcForm.SaveDialog1.Execute();
        path = UserForms.ctcForm.SaveDialog1.FileName;
        
        fso = new ActiveXObject("Scripting.FileSystemObject");
        file = fso.OpenTextFile(path, 2, true);    
        
        for (var i=0; i<var_count; i++)
        {
            name = Project.Variables.GetVariableName(i);
            value = Project.Variables.VariableByName(name);
            type = Project.Variables.GetVariableType(i);
            var record = new Array(name, value, type);
            file.WriteLine(record);
        }
        
        file.Close();
    }

    Code to import variables:

     

    function import_variables()
    {
        var dialog = UserForms.dialog
        var path, fso, file, set;
        
        UserForms.ctcForm.OpenDialog1.Execute();
        path = UserForms.ctcForm.OpenDialog1.FileName;
        
        if (path)
        {
            fso = new ActiveXObject("Scripting.FileSystemObject");
            file = fso.OpenTextFile(path, 1); 
        
            while (!file.AtEndOfStream)
            {
                record = file.ReadLine().split(",");
                try
                {
                    Project.Variables.AddVariable(record[0], record[2]);
                }
                catch(exception)
                {
                    gui_dialog("Unable to import variable: "+record[0], exception);
                }
            }
        
            file.Close(); 
        } 
        else
        {
            gui_dialog("You have not selected the file for import!", "");
        } 
    }

     

    Code to provide gui dialogues:

     

    function gui_dialog(label1, label2)
    {
        var dialog = UserForms.dialog
        dialog.Caption = "Import variables";
        dialog.cxLabel1.Caption = label1;
        dialog.cxLabel2.Caption = label2;
        dialog.ShowModal();    
    }

     

    .tcx file to install the script extension you can find in the attachment.

     

    It should looks like:

     

     

    Waiting for your feedback and good luck for others!

     

    P.S. Feature request to TC developers - it's better to have all custom buttons in the separate toolbar section.

     

    P.P.S. Updated to v.1.1

  • Might as well have a go. 

     

    You can export either Project variables or Project Suite variables by selecting them from the dropdown. You can then choose where to export the file to (double click to bring up the browse dialog). When importing just select the exported text file, again, double click to browse for the file. 

     

     

     

     

    Good luck!

    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      Hi Guys,

       

      Thanks for your scripts! That’s so exciting to see you taking part in the competition.

        

      We have reviewed your extensions with our Gurus from the TestComplete Customer Care Team – AlexanderM and Dmitry_Nikolaev. And, here are some notes:

      1. Some of you use a CSV storage. However, what is going to happen if the value of a string variable contains a comma? I can tell you – none of your extensions will import such variables :(
      2. That would be great if it was possible to select a location for a storage file – here is an example of the Save As dialog.

      1. cunderw – can you use two buttons to export and import variables instead of the dropdown control?
      2. baxatob – we recommend that you use TestComplete’s User Form to see both operations in one dialog and control this procedure better

       

      And, we have one additional task that you may find interesting to implement :) - add the possibility of choosing variables you want to export.

      You can do this by displaying all possible variables with checkboxes next to them. Do you accept the challenge?

      • cunderw's avatar
        cunderw
        Community Hero

        TanyaYatskovska - Can definitely add multiple buttons instead of the drop down! Will also try to work on being able to select the variables!

  • cunderw's avatar
    cunderw
    Community Hero

    Version 1.0 attached.

     

    Note: Variable types Table, DB Table, and Object are not supported because of the limit TC has when adding variables via script.

  • ChrisPerth's avatar
    ChrisPerth
    Occasional Contributor

    Thanks for all the replies :)

     

    As mentioned, I was able to change 'Msxml2.DOMDocument.4.0' to 'Msxml2.DOMDocument' in a couple of places and it all worked fine!

     

    Thanks for the help :smileyhappy:

    • ChristofS's avatar
      ChristofS
      Staff

      Hey there Community!

       

      Just wondering if these script extensions work with TestComplete 14/15? From my initial attempts I was able to install StevenN's version 1.3 script extension however I didn't see any new buttons or toolbars become available.

       

      Thanks 🙂

    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      tristaanogre wrote:

      Challenge accepted.


      Hi tristaanogre, you have only several days left to prepare your version of the extension. We are still waiting for it :)

       

      StevenNcunderw, we have reviewed your extensions, and our team has several comments in this regard - I'll send them to you via PM.

  • cunderw's avatar
    cunderw
    Community Hero

    Does this mean I can't enter the contest? haha. 

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3
    Are these going to be a straight import export or is there a merge happening too?
  • nwuser_testcomp's avatar
    nwuser_testcomp
    Occasional Contributor

    Hi Tanya,

     

    Though much days not left for the contest and already people have submitted their versions, still wanted to give it a try. :)

    I am facing an error while importing the extension--it gives error invalid image size "export.bmp". Image size is 1 kb.

     

    Please help in this regard.

     

    TIA!!

    • StevenN's avatar
      StevenN
      Contributor

      If it's the toolbar image then it will be referring to the image size in pixels, it needs to be 16 x 16. 

    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      Hi Nwuser_testcomp,

       

       

      You are welcome to take part in the contest :)

      Does StevenN's suggestion help you?

      • nwuser_testcomp's avatar
        nwuser_testcomp
        Occasional Contributor

        Hi Tanya,

         

        Yes, the solution provided by Steven worked.

        StevenN,thanks!!

         

        Will post my solution probably by today.

         

        Cheers!!

  • ChrisPerth's avatar
    ChrisPerth
    Occasional Contributor

    +1

     

    I too am also getting the same error :smileysad:

     

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      The beautiful thing about Script Extensions is that you can simply open the TCX package by changing the extension to .zip.  Open it up, find the code, and check the code line.

      I'm GUESSING that the problem is related to something on the lines of Sys.OleObject('MSXML2.DOMDocument') where it has .4.0 added on the end.  Just change it to MSXML2.DOMDocument and see if that fixes it.

      • StevenN's avatar
        StevenN
        Contributor

        Sorry been missing from here for a while. I no longer have the original project (never bothered adding it to source control) but I found the updated version that I meant to upload. It has the fix for escaping the XML characters and it removed the MSXML version requirement, so should hopefully (maybe) work! 

         

        If not, then I only have access to the extension so will be limited in what can be changed.

         

        When I try to attach the extension I get the following error:

         

        "The attachment's importexportmanager.tcx content type (application/octet-stream) does not match its file extension and has been removed." 

         

        I'll need to upload it as a zip, so to run please first rename if from .zip to .tcx.