Get number from Excel

11 posts, 2 answers
  1. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 03 Jan 2012 Link to this post

    I have custom method and want to get int parameter for it from Excel.

    If I use following statement:
    Pages.RegistrationPage.Field_FirstName.Text = GenerateUniqueId((int)Data["FirstName"]);


    following error occurs
    System.InvalidCastException: Specified cast is not valid.

    How I could get parameter for method from Excel?
  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 04 Jan 2012 Link to this post

    Hello Alexander,
    even though you're reading a number from the Excel, it's actually stored as a String and should be extracted as a String.

    So just change your cast to String:
    Pages.RegistrationPage.Field_FirstName.Text = GenerateUniqueId((String)Data["FirstName"]);

    Kind regards,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 04 Jan 2012 Link to this post

    But my method GenerateUniqueId shoud get int parameter. How will it work with string?
  4. Answer
    Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 04 Jan 2012 Link to this post

    Hi Alexander,
    you can use a simple conversion:

    int Fn = Convert.ToInt32(Data["FirstName"] );
    Pages.RegistrationPage.Field_FirstName.Text = GenerateUniqueId(Fn);

    Regards,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 04 Jan 2012 Link to this post

    Your adviсe helped me. Thank you.

    Could you please help me with another problem with this code.
    I have following:
    I use static variables in the beginning of code-behind file because I use it later:
    static string fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(Data["FirstName"] ));
    static string companyValue = UniqueIdGenerator.GenerateUniqueId(100);
    static string phoneValue = UniqueIdGenerator.GenerateUniqueId(50);
     
    public void FillCompany()    {
      Pages.SitecoreCMS.Field_Company.Text = companyValue;
    }
    public void VerifyingCompany()    {
          Assert.IsTrue(ArtOfTest.Common.CompareUtils.StringCompare(Pages.Contact.FrameContentIFrame.SitecoreTentativeaccountnameText.Text,companyValue, ArtOfTest.Common.StringCompareType.Contains));
    }
    But if I use data from Excel I get following error
    An object reference is required for the non-static field, method, or property 'ArtOfTest.WebAii.Design.BaseWebAiiTest.Data.get'
    How I should rewrite my code to use data from Excel?
  6. Answer
    Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 04 Jan 2012 Link to this post

    Hi Alexander,
    you need a setter function for that. 

    Instead of doing this:
    public class CodeBehindclass {
    static object obj = Data["test"];
    .... (other logic like coded steps here)...
    }

    do this:
    public class CodeBehindclass {
    static object obj;
     
      [CodedStep(@"New Coded Step")]
            public void test1_CodedStep()
            {
            setObjValue();
            Log.WriteLine(obj.ToString());
    }
     
    public void setObjValue() { //Setter function
    obj = Data["test"];
    }
     
    }

    Since obj is static, you shouldn't need to invoke setObjValue() more than once per iteration. You can try calling the setter function from inside of OnBeforeTestStarted()
    http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/execute-custom-scripts-before-after-test.aspx

    But I'm not sure that's invoked for each individual iteration when doing data-driven testing.
    Kind regards,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 05 Jan 2012 Link to this post

    I tried to write some code but it can't be compiled
    static object fullNameValue;
     
    public void test1_CodedStep() {
      setObjValue();
      Log.WriteLine(fullNameValue.ToString());
    }
     
    public void setObjValue() {
      //Setter function
      fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(Data["FullNameLength"]));
    }
    [CodedStep(@"Enter text in 'Field_FullName'")]
       public void FillFullName()    {
     
         // Enter text in 'Field_FullName'
         Pages.SitecoreCMS.Field_FullName.Text = fullNameValue;
       }
    "Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)"
    For string Pages.SitecoreCMS.Field_FullName.Text = fullNameValue;
  8. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 05 Jan 2012 Link to this post

    Hello Alexander,
    we store the value from the datasource into an instance of an Object. In the following line:
    Pages.SitecoreCMS.Field_FullName.Text = fullNameValue;
    the compiler is expecting a String variable but you're giving it an Object variable instead. Since the Object actually is a String (String is an extension of Object as are all other classes) we need to basically assure the compiler that it's OK and it's a String in the disguise. To do that we can add a simple cast:
    Pages.SitecoreCMS.Field_FullName.Text = (String) fullNameValue;

    If you google such compilation errors you will definitely find good tips on how to resolve them as well a description on why the error occurred and how it can be avoided in the future (better than any explanation I could provide). 

    Greetings,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 05 Jan 2012 Link to this post

    Finally my code is
    public class PricingForm : BaseWebAiiTest
    {
      private Pages _pages;
      public Pages Pages {
        get      {
          if (_pages == null) {
            _pages = new Pages(Manager.Current);
          }
          return _pages;
        }
      }
     
      static object fullNameValue;
     
      public void test1_CodedStep()    {
        setObjValue();
        Log.WriteLine(fullNameValue.ToString());
      }
     
      public void setObjValue()    { //Setter function
        fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(Data["FullNameLength"]));
      }
     
      [CodedStep(@"Enter text in 'Field_FullName'")]
      public void FillFullName()    {
        // Enter text in 'Field_FullName'
        Pages.SitecoreCMS.Field_FullName.Text = (string)fullNameValue;
      }
     
      [CodedStep(@"Enter text in 'Field_GridFindCriteria'")]
      public void EnteringTextInSearch()    {
        // Enter text in 'Field_GridFindCriteria'
        Pages.AccountsMyActiveAccounts.FrameContentIFrame.Field_GridFindCriteria.Text = fullNameValue;
      }
    }
    But if I run it fullNameValue in FillFullName method has null value. Why it happens?
  10. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 10 Jan 2012 Link to this post

    Hello Alexander,
    try invoking the function setObjValue() right before you access the variable. Like this:
    public void FillFullName()    {
        setObjValue();
        // Enter text in 'Field_FullName'
        Pages.SitecoreCMS.Field_FullName.Text = (string)fullNameValue;
      }

    Greetings,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 10 Jan 2012 Link to this post

    Thanks for your answer.
    I found workaround for my code:
    string _fullNameValue;
    string fullNameValue
    {
      get
      {
        if (_fullNameValue == null)
          _fullNameValue = UniqueIdGenerator.GenerateUniqueId(Convert.ToInt32(this.Data["FullNameLength"]));
     
        return _fullNameValue;
      }
    }
    and so on for all variables.
Back to Top