This is a migrated thread and some comments may be shown as answers.

Global variables - another way

3 Answers 311 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Asare
Top achievements
Rank 1
Asare asked on 01 Nov 2012, 04:01 PM
The suggested approach to creating global variable is rather cumbersome and also forces every reference to a global variable to require customize in code.

The approach below uses the SetExtractedValue and GetExtractedValue instead

All references to the variables can be through DataDriven bindings.

  1. Create an XML file

<GlobalValues>
    <GlobalValue>
        <TelerikHomeFirstname>"Michael"</TelerikHomeFirstname>
        <TelerikOwnerSurname>"Jackson"</TelerikOwnerSurname>
        <TelerikHomeEmailAddress>"Neverland Ranch"</TelerikHomeEmailAddress>
        <TelerikNickName>"King of POP"</TelerikNickName>
        <TelerikURL>"http://google.com/"</TelerikURL>
        <TelerikDBUsername>"telerik"</TelerikDBUsername>
        <TelerikDBPassword>"TelerikPassword"</TelerikDBPassword>
        <TelerikDBDatabase>"telerikdb"</TelerikDBDatabase>
        <TelerikCheckCountUsername>"tlk"</TelerikCheckCountUsername>
        <TelerikCheckCountPassword>"password"<TelerikCheckCountPassword>
    </GlobalValue>
</GlobalValues>   

Add a new Script Step. In the OnBeforeTestStarted method
add the following code

public override void OnBeforeTestStarted()
{
    string globalVarsFile = @"c:\<path>\myglobalvarsfile.xml";
    
    try
    {                    
        if(!File.Exists(globalVarsFile))
        {
            // an error has occurred
            Log.WriteLine("Error: FILE" + globalVarsFile + "DOES NOT EXIST");                    
            Assert.IsTrue(false);
        }
        else
        {
            Log.WriteLine("File found" +globalVarsFile);
        }
                            
        XDocument globalVarsXML = XDocument.Load(globalVarsFile);

        var queryResult =
            from gl in globalVarsXML.Element("GlobalValues").Element("GlobalValue").Elements()
            select gl;
        
        foreach(XElement x in queryResult)
        {
            SetExtractedValue(x.Name.ToString(), x.Value.ToString());                     
        }   
    }
    catch(FileNotFoundException fnfe)
    {
        Log.WriteLine("IO Error occurred: " + fnfe.Message);
        
    }
    catch(IOException ioex)
    {
        Log.WriteLine("IO Error occurred: " + ioex.Message);
    }
}




3 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 06 Nov 2012, 09:26 PM
Hi Asare,

That is an interesting approach. We also need to note that you must add a DLL reference to System.Xml.Linq from the .NET assemblies, plus add:

using System.Linq;
using System.Xml.Linq;

Then your code will work. Let's also store the XML file in the Data folder of the project and use a relative path to locate it, like this:
public override void OnBeforeTestStarted(BeforeTestStartedArgs args)
{
    string globalVarsFile = Path.Combine(args.Context.DeploymentDirectory, @"Data\myglobalvarsfile.xml");
 
    try
    {
        if (!File.Exists(globalVarsFile))
        {
            // an error has occurred
            Log.WriteLine("Error: FILE '" + globalVarsFile + "' DOES NOT EXIST");
            Assert.IsTrue(false);
        }
 
        XDocument globalVarsXML = XDocument.Load(globalVarsFile);
        var queryResult =
            from gl in globalVarsXML.Element("GlobalValues").Element("GlobalValue").Elements()
            select gl;
 
        foreach (XElement x in queryResult)
        {
            Log.WriteLine("Setting global variable " + x.Name + " to " + x.Value);
            SetExtractedValue(x.Name.ToString(), x.Value.ToString());
        }
    }
    catch (FileNotFoundException fnfe)
    {
        Log.WriteLine("IO Error occurred: " + fnfe.Message);
 
    }
    catch (IOException ioex)
    {
        Log.WriteLine("IO Error occurred: " + ioex.Message);
    }
}

Also note that OnBeforeTestStarted now takes a BeforeTestStartedArgs parameter, which is useful in this instance.

I'm granting you some Telerik Points for your useful code sample.

All the best,
Cody
the Telerik team
Are you enjoying Test Studio? We’d appreciate your vote in the ATI automation awards.
Vote now
0
Asare
Top achievements
Rank 1
answered on 07 Nov 2012, 11:26 AM
Hi Cody,

Thank you for the feedback, very useful indeed.

Which version of Test Studio introduced the BeforeTestStartedArgs parameter?

I am using Test Studio version 2012.1.719.0. The parameter does not appear to exist in this version.

Error: The type or namespace name 'BeforeTestStartedArgs' could not be found (are you missing a using directive or an assembly reference?)


Regards,

Asare
0
Cody
Telerik team
answered on 07 Nov 2012, 06:07 PM
Hi Asare,

It was just added in our latest internal build, version 2012.2.1022.

Regards,
Cody
the Telerik team
Are you enjoying Test Studio? We’d appreciate your vote in the ATI automation awards.
Vote now
Tags
General Discussions
Asked by
Asare
Top achievements
Rank 1
Answers by
Cody
Telerik team
Asare
Top achievements
Rank 1
Share this question
or