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

Get number from Excel

10 Answers 143 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
algot
Top achievements
Rank 1
algot asked on 03 Jan 2012, 11:11 AM
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?

10 Answers, 1 is accepted

Sort by
0
Stoich
Telerik team
answered on 04 Jan 2012, 12:22 PM
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
0
algot
Top achievements
Rank 1
answered on 04 Jan 2012, 12:28 PM
But my method GenerateUniqueId shoud get int parameter. How will it work with string?
0
Accepted
Stoich
Telerik team
answered on 04 Jan 2012, 01:40 PM
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
0
algot
Top achievements
Rank 1
answered on 04 Jan 2012, 01:52 PM
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?
0
Accepted
Stoich
Telerik team
answered on 04 Jan 2012, 09:25 PM
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
0
algot
Top achievements
Rank 1
answered on 05 Jan 2012, 11:04 AM
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;
0
Stoich
Telerik team
answered on 05 Jan 2012, 01:28 PM
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
0
algot
Top achievements
Rank 1
answered on 05 Jan 2012, 01:44 PM
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?
0
Stoich
Telerik team
answered on 10 Jan 2012, 03:45 PM
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
0
algot
Top achievements
Rank 1
answered on 10 Jan 2012, 03:48 PM
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.
Tags
General Discussions
Asked by
algot
Top achievements
Rank 1
Answers by
Stoich
Telerik team
algot
Top achievements
Rank 1
Share this question
or