Ask a Question

Code Contest: Export/Importing Project Variables

SOLVED
TanyaYatskovska
SmartBear Alumni (Retired)

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!

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



51 REPLIES 51
tristaanogre
Esteemed Contributor

Challenge accepted.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cunderw
Community Hero

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


Thanks,
Carson

Click the Accept as Solution button if my answer has helped
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:

 

eximvar.png

 

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

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.


Thanks,
Carson

Click the Accept as Solution button if my answer has helped
StevenN
Contributor

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. 

 

 

 

ImportExportManager.png

 

Good luck!

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.

filedialog

  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?

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



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


Thanks,
Carson

Click the Accept as Solution button if my answer has helped

Hi,

 

I agreed that better to have one button to open an export/import dialog.

 

At the same time I believe, that the option to choose a separate variables from the list - it's a redundant functionality. E.g. one of our current projects contains more than 100 different variables. Easy to be confused while selecting from them. Anyway it can be implemented as a task 🙂

 

v2.0 attached.

 

Change Log:

  1. Two different buttons for import / export as opposed to a dropdown
  2. Can now specify file name to save or import
  3. Added open / save dialogs for import / export buttons
  4. Can type path to file in edit box to change initial patch for open / save dialogs
  5. Last path / file is remembered so it's easier to import what was exported
  6. Can select which variables to export
  7. Fixed bug with export that did not correctly stringify the the output that is saved.

 


Thanks,
Carson

Click the Accept as Solution button if my answer has helped
cancel
Showing results for 
Search instead for 
Did you mean: