Create a variable to be used across tests

10 posts, 2 answers
  1. David
    David avatar
    4 posts
    Member since:
    Mar 2013

    Posted 29 Mar 2013 Link to this post

    Hello Everyone, this is my first post so be gentle.

    I'm trying to automate a set of pages where a user that is already in the system can create requests to add other users.  Another user would then approve the request and create a username for that user.  There is some data that needs to be shared between those accounts (The tracking number request).  I was wondering if there was a way to store and edit this data during automated test execution where I can, perhaps, edit this variable from a scripted step or something of that nature; where in one test I could set the variable in one of my scripted steps and then in another test I could access that variable so that I can populate a field with that data.

    Thanks for any help that can be provided.
  2. Answer
    Byron
    Admin
    Byron avatar
    184 posts

    Posted 01 Apr 2013 Link to this post

    Hello David,

    Welcome to the Test Studio forums. Using the Extraction and Test as Step features, you can pass a variable between tests. After extracting the variable and before passing it to another test, you can use a coded step to modify the variable. This example converts an extracted variable to all caps:

    string myData = GetExtractedValue("varname").ToString();
    myData = myData.ToUpperInvariant();
    SetExtractedValue("varname", myData);

    Greetings,
    Byron
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  3. Answer
    Himanshu
    Himanshu avatar
    5 posts
    Member since:
    Oct 2012

    Posted 02 Apr 2013 Link to this post

    Hi David

    Just to add to Byron, using extracted value is the best option as it is very well integrated with Test Studio and eases re usability in other steps. Still, other things that can be done are:
    1) To declare a variable in the project namespace outside the class for the current test and that can be used throughout project.
    public class variable
        {
            public static int a;
        }
     use it as "variable.a" wherever you want it in any test in the same project but make sure that the test setting the value runs before the test using its value.

    2) Also, you can use simple readstream and writestream to write the value to a txt file and use it from there. or wite the value to excel and fetch it back using interops. This way the value could be retained even after the test has closed( That was a specific requirement in our case).

    Hope It Helps
  4. David
    David avatar
    4 posts
    Member since:
    Mar 2013

    Posted 02 Apr 2013 Link to this post

    Thank you very much for the quick response.

    Both answers are very helpful.
  5. Vlas
    Vlas avatar
    1 posts
    Member since:
    Mar 2014

    Posted 14 Aug 2014 Link to this post

    Hi,
    we have the next scenario, which might also require a use of variables:

    We want to be able to run a test list aginst multiple URLs without having to manualy change the Base URL in test list settings.

    I was able to do that with a single test scenario by binding a .csv file with needed URLs to a Navigate step. So, no problem with that.

    But what about the whole test list with a number of test scenarios - is it possible to automate a change of Base URLs in it? Maybe using variables?

    Thanks in advance for any input.
  6. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 14 Aug 2014 Link to this post

    Hello Vlas,

    If you don't mind running your test list from the command line, there you can specify an XML file containing specific settings for this run, which includes the BaseUrl setting.

    Regards,
    Cody
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
  7. jippy
    jippy avatar
    2 posts
    Member since:
    Dec 2010

    Posted 19 Aug 2015 in reply to Himanshu Link to this post

    Hello Himanshu, in context to your post which says

    >>public class variable
    {
    public static int a;
    }
    use it as "variable.a" wherever you want it in any test in the same project but make sure that the test setting the value runs before the test using its value..<<

     

    I have a Query please. Can we assign a value to this variable a which can be used by all other telerik tests just by referring as : "variable.a";

     

  8. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 21 Aug 2015 Link to this post

    Hello Jippy,

    Please check out the utility class article.

    It will contain global variables or functions in Test Studio Standalone version that are accessible from all the tests within the test project. 

    Let me know if this is what you want to achieve.

    Regards,
    Boyan Boev
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
  9. jippy
    jippy avatar
    2 posts
    Member since:
    Dec 2010

    Posted 23 Aug 2015 in reply to Boyan Boev Link to this post

    Hi Boyan,

    I already had a look at this Utility class.

    However I was facing challenges picking up the value of global variable from excel(data driven approach) to be assigned in utility class and the same can be used in other scripts just by referring to the class.variable name.

     

    I am just pasting some part of the class here :

    namespace Web_Version_Testing
    {
    public static class DBConnectionString
    {
    public static void InitializeConnectionString()
    {
    string s = Data["server"].ToString();
    string d = Data["db"].ToString();}
    public string dbconnectionstring = "data source=" + s + ";database=" + d + ";uid=SYSADM;pwd=SYSADM;Persist Security Info=true;";

    }}}

     

    I am picking the server and db name fron excel which is binded to this test and then creating the db connection string which I want to use/refer in other tests.

    Is this the right way to pursue?

     

    Jippy

  10. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 27 Aug 2015 Link to this post

    Hello Jippy,

    Unfortunately you cannot use Data method in the utility class since it doesn't extend BaseWebAiiTest class.

    However you can read the Excel file entirely in code in the utility class and create public static variables.

    Let me know if that approach works for you.

    Regards,
    Boyan Boev
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
Back to Top