How to get Test variable through custom code in HP UFT - ApiTest - c#

I want to set value to the Test Variable using Custom Code.
Can you tell me how to do it as i am not able to access the variable from code.
I need to access the User Variable URL in my custom code and set the value.
Please help me if you know how to do it using Custom Code.
Thanks,
Madhan

Based on your question:
1. Click on File menu and then Settings, it will open Properties pane.
2. Click on "+" to add user variable and give a name.
3. In your custom code, type below code:
string s = "https://www.google.com/";
this.Context.TestProfile.SetVariableValue("NameOfYourVariable",s);
To retrieve the value of given variable:
string ss = this.Context.TestProfile.GetVariableValue("NameOfYourVariable");
CodeActivity5.Report("Variable is : ", ss); //(This line will print your variable value)

answer provided here is valid when the need is to set string values. The "SetVariableValue" method takes two "String" type parameters. This limits the ability to set Int32 type variables. Obviously, C# throws an error when trying to set an integer value.
Now, an integer value is particularly useful while setting values for loop iterations. I am not sure whether this is a limitation of the tool or whether my lack of knowledge. So, to work around this, i used the output property of custom code activity. To do this, create a custom code activity and create an output property of the desired type, say Int. Now, assign a value to this output property using the line:
this.ActivityName.Output.property name = property value
This is available in UFT help and can be useful while trying to pass values other than string between different activities in a flow.

Related

GLOP - Google OR Tools - choose simplex method

I am using the default linear solver in GLOP (which I believe is dual-simplex) and getting ABNORMAL result.
I'd like to use normal simplex instead (as this post describes - https://github.com/google/or-tools/issues/1868) but I don't know how to change this setting. How do I change the solver? (Using C# by the way)
Laurent gives the correct steps above. Here is the practical code to create these parameters and pass to solver (if anyone is interested):
var Params = new Google.OrTools.LinearSolver.MPSolverParameters();
Params.SetIntegerParam(MPSolverParameters.IntegerParam.LP_ALGORITHM, (int) MPSolverParameters.LpAlgorithmValues.PRIMAL);
Solver.ResultStatus solved = solver.Solve(Params);
You need to change the parameters of the solver.
The class is here: http://google.github.io/or-tools/dotnet/classGoogle_1_1OrTools_1_1LinearSolver_1_1MPSolverParameters.html
You need to change the integer parameter http://google.github.io/or-tools/dotnet/classGoogle_1_1OrTools_1_1LinearSolver_1_1MPSolverParameters.html#a3b8427a9f4368fd831af8f8c61fb823c
The parameter to change is http://google.github.io/or-tools/dotnet/classGoogle_1_1OrTools_1_1LinearSolver_1_1MPSolverParameters.html#a7319655592ea63d50ef2a6645e309784
the value to assign is http://google.github.io/or-tools/dotnet/classGoogle_1_1OrTools_1_1LinearSolver_1_1MPSolverParameters.html#a79b59c0c868544afdaa05d89c8f8541f
As per the current documentation (see the last row of the table here), it actually seems that the default mode is primal simplex, so that is what one would have if they did not set anything. You'd have to explicitly set parameters to make it work with dual simplex.

C# - How to get a long type variable defined by user in a TextBox and use it in a mathematical operation

I need a Text field of my program to be treated as a LONG type variable and be processed in a mathematical operation. The value of this variable needs to be specified every time by the user and I want the program to treat this value not as an integer but as a long indeed.
I have treated other fields as integer and they work fine with this kind of code:
HourField.IntValue
now notice that .IntValue that obviously says to the program to take the content of the HourField whatever is in it and treat it as an integer.
But unfortunately there is no equivalent for the long type in fact if I try to write .LongValue, C# just doesn't recognise this function....there are other similar functions like .FloatValue or .DoubleValue etc. but there is no such thing as .LongValue.
However I even tried to use this kind of syntax:
Convert.ToInt64(FileSizeBytesField);
or something like that and in theory the compiler doesn't give me any error for the compilation etc. but if I try to actually make the calculation by pressing the button the program crashes and Visual Studio tells me that the type of casting is invalid.
Please please pease help me with this. It's the last thing I need to actually finish my program!!!
P.s. I am posting some screenshots of what I got and of my source code. Thanks
program's source code
Debugging error in Visual Studio after program crash
I guess the FileSizeBytesField you are trying to take a value from is an instance of NSTextField or another subclass of NSControl. In that case, you can take the value of control using properties like IntValue or StringValue. So, to convert the value to long type try this:
Convert.ToInt64(FileSizeBytesField.StringValue)
Or, using more common approach already suggested by Hooman Bahreini:
long fileSizeBytes;
if (long.TryParse(FileSizeBytesField.StringValue, out fileSizeBytes))
{
// use fileSizeBytes
}
You can use Parse, to convert the string value to long
long l = long.Parse("453216");
If you want to ensure that your input is a valid number, you can use tryParse
if (long.TryParse("45263572", out l) == true)
{
// use long value
}
else
{
// input is not a valid long value... handle the situation here
}

What's the difference between Entity.Attributes and Entity.FormattedValues?

I'm learning how to write custom workflows and am trying to figure out where and in which format all the values I need are stored. I have noticed that I can access Entity instance data in both the Attributes and FormattedValues properties. How do I know when to use which one?
I have noticed MSDN's remark "Entity formatted values are only available on a retrieve operation, not on an update operation.".
For testing I've made two foreach-blocks iterating through both collections. Attributes gives me 65 lines and FormattedValues gives me 39. I can see that, yes, the output from FormattedValues is indeed formatted.
For example, where Attributes gives the output "Microsoft.Xrm.Sdk.OptionSetValue", FormattedValues gives me a string with the actual value.
Which values/attributes are generally excluded from the FormattedValues collection and why?
I'm not 100% sure about this, but the formatted values are the values you will be able to see on the form. In that list you will be able to find money types with the $ symbol, or the labels of the option sets. A text field shouldn't be shown since is already human-readable.
https://community.dynamics.com/crm/b/crmmitchmilam/archive/2013/04/18/crm-sdk-nugget-entity-formattedvalues-property.aspx
Refer to this article to know a little bit more about it. I rareley using that attribute list since the data is in string format. I found it really useful to retrieve the OprionSet lables.
After a quick check it'd appear that the difference between an attribute and a formatted value is that the former to the actual value stored in the DB (or at least the value that was stored there at the occasion of fetching it) while the latter serves what's shown to the user.
I haven't used formatted values but until proven otherwise, I'd say that an entity's attribute will provide you with the typed value that the regarded field is based on (be that int, DateTime and such), whereas its formatted value is the rendered, stringified representation of that value (being dependent on e.g. what form you're referring to, what language etc.)
By that logic, I'd expect the set of formatted values to be a subset to the set of attributes. Also, it should be constituted of exclusively String typed values, while the counterpart is a member of the type conversion table.
An example of the difference I can think of is an option set called picky with the currently selected option named "hazaa" and the ID of 1234. The below sample is written by heart so feel free to correct. It exemplifies the point, though: plainValue will be an integer equal to 1234, while formattedValue will be "hazaa".
int plainValue = (int)entity["picky"];
String formattedValue = (String)entity.FormattedValues["picky"];
I'd say that the attributive approach is more reliable as it'll render the actual values, while the alternative can lead to unexpected outcome. However, there's a certain convenience to it, I must add.
Personally, I'd recommend to look into the method GetAttributeValue<T>(String) or, as every cocky CRM developer would - have your own class with extension methods and use the method Get<T>(T,String) in there. That provides you with the sense of control and offers the best predictability and readability, IMAO.

Allowing null in int text box with non nullable int

So there are very similar questions about this, but none seem to solve what I'm trying to do, and maybe it's not possible. I have a form with a number of text boxes bound to int properties in my model. We allow the user to clear the field, and it should remain cleared. CUrrently if they do this they get a validation error that the it can't convert "". I understand why this is, however, due to the design I can't use int?, which I know would solve my problem. Is this possible to do, without having to define a string property to bind to? I'm trying not to duplicate all my int properties as string properties, but maybe that's the road I need to take.
What you would like to do when the string is empty?
I mean do you want to have some int value in prop if textbox is empty? If yes, simply create a converter that converts null value to that value and assign to Binding
You need to be able to represent an empty int.
This is a dirty hack but if you chose a number say -999 that will never be valid you could use a custom converter to convert and convert back between "" and -999.
It will probably achieve what you want but I would not recommend it.

Question about reference types

This is taken from Jon Skeet's excellent personal C# site (http://www.yoda.arachsys.com/csharp/):
StringBuilder first = new StringBuilder();
StringBuilder second = first;
first.Append ("hello");
first = null;
Console.WriteLine (second);
1) Changing the value of first will not change the value of second -
2) although while their values are still references to the same object, any changes made to the object through the first variable will be visible through the second variable.
This is taken from the same sentence. What is meant by changing the value? I assume the value of a variable (eg int x = 4, or 5, or 45, etc).
Does this mean if first points to another compatible object, it won't have an effect on two?
Everything on that page makes sense, I think it's just an issue with my interpretation of the English.
Thanks
first is a reference to an object of type StringBuilder. That is, first stores a value that can be used to refer to an object on the heap that is type of Stringuilder. second is another reference to an object of type StringBuilder and its value is initially set refer to the same object that first is referring to.
If you change the value of first what you are doing is changing what the referent is. That is, you are using first to refer to a different object. This does not impact second; its value is unaffected by changes to the value of first. (Remember: the value of first and second are references that initially have the same referent. But just like with
int x = 1;
int y = x;
x = 2;
does not change the value of y, changing the value of first does not change the value of second.
On the other hand, when first and second refer to the same object, any changes to that object will be visible through both first and second.
Think of it like this. Let's say I create a text file first.html whose contents are
Stack Overflow
and I issue the command copy first.html second.html. Then both pages can be used to refer to the same webpage; by following the link we arrive at the same referent. If changes are made to the Stack Overflow home page, then accessing the homepage through either first.html or second.html will allow me to see those changes. But if I then change the contents of first.html to be
<a href="http://www.thedailywtf.com>The Daily WTF</a>
then I can no longer use first.html to refer to the Stack Overflow homepage. Moreover, this change does not impact the value of second.html. It is still
<a href="http://stackoverflow.com>Stack Overflow</a>
Think of the contents of these files as the values of a reference type, and the ultimate desination as the referent object.
The difference between the value of the object itself and the contents of the object are not clear.
For example, it is possible to change the contents of second by calling methods on first, as in the call to Append in the example. However, setting the value of first to null does not set second to null.
You can see this easily by writing this code and stepping through it in a debugger.

Categories