Here I am again,
this is the complete code I translated but it keeps crashins when I call
this:
SetControlPropertyValue("txtFrac", "Text", "5") and that is caused by this:
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
because I can't get t1.GetField(controlName, flags) to return something it
always returns nothing
Any help would be much appreciated, I've included the .net code and the
original C# code
thanks in advance
Greetz Peter
it says object reference not set
Imports System.Threading
Imports System.windows.Forms
Imports System.Reflection
Module Module1
Private testForm As Form = Nothing
Private flags As BindingFlags = (BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance)
Private delay As Integer = 1500
Sub Main()
Dim exePath As String
Try
Console.WriteLine("Starting UItester")
Console.WriteLine("Launching WinApp under test")
exePath = "C:\Documents and Settings\pproost\Mijn
documenten\Visual Studio Projects\access\bin\access.exe"
testForm = LaunchApp(exePath, "access.Form2")
Console.WriteLine("Moving form")
SetFormPropertyValue("Location", New System.Drawing.Point(200,
200))
SetFormPropertyValue("Location", New System.Drawing.Point(500,
500))
Console.WriteLine("Resizing form")
SetFormPropertyValue("Size", New System.Drawing.Size(600, 600))
SetFormPropertyValue("Size", New System.Drawing.Size(300, 320))
Console.WriteLine("Setting txtFrac to 5")
SetControlPropertyValue("txtFrac", "Text", "5")
Console.WriteLine("Clicking btnFrac")
InvokeMethod("btnFrac_Click", New Object() {Nothing, New
EventArgs})
Console.WriteLine("Checking for result:")
Dim pass As Boolean = True
If (CStr(GetControlPropertyValue("txtResult", "Text")) <> "120")
Then pass = False
If (pass) Then
Console.WriteLine("UI test scenario result: PASS")
Else
Console.WriteLine("\nUI test scenario result: *FAIL*")
End If
Console.WriteLine("Clicking menu File->Exit in 5 seconds . . .
")
Thread.Sleep(3500)
InvokeMethod("MenuItem4_Click", New Object() {Nothing, New
EventArgs})
Console.ReadLine()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub SetControlPropertyValue(ByVal controlName As String, ByVal
propertyName As String, ByVal newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetControlPropertyValueHandler(AddressOf
SetControlPropertyValue), New Object() {controlName, propertyName,
newValue})
'Return
End If
Dim t1 As Type = testForm.GetType
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
pi.SetValue(ctrl, newValue, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Delegate Sub SetControlPropertyValueHandler(ByVal controlName As String,
ByVal propertyname As String, ByVal newValue As Object)
Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() {propertyName, newValue})
'Return
End If
Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
pi.SetValue(testForm, newValue, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Delegate Sub SetFormPropertyValueHandler(ByVal propertyName As String,
ByVal newValue As Object)
Private Function GetControlPropertyValue(ByVal controlName As String,
ByVal propertyName As String)
Try
If (testForm.InvokeRequired) Then
Thread.Sleep(delay)
Return testForm.Invoke(New
GetControlPropertyValueHandler(AddressOf GetControlPropertyValue), New
Object() {controlName, propertyName})
End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As New Object
ctrl = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
Return pi.GetValue(ctrl, New Object)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
Delegate Function GetControlPropertyValueHandler(ByVal controlName As
String, ByVal propertyName As String) As Object
Private Sub InvokeMethod(ByVal methodName As String, ByVal parms As
Object()) '{parms})
Try
If (testForm.InvokeRequired) Then
Thread.Sleep(delay)
testForm.Invoke(New InvokeMethodHandler(AddressOf
InvokeMethod), New Object() {methodName, parms})
End If
Dim t As Type = testForm.GetType()
Dim mi As MethodInfo = t.GetMethod(methodName, flags)
mi.Invoke(testForm, parms)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Delegate Sub InvokeMethodHandler(ByVal methodName As String, ByVal parms
As Object())
Private Function LaunchApp(ByVal exePath As String, ByVal formName As
String) As Form
Try
Thread.Sleep(delay)
Dim a As [Assembly] = [Assembly].LoadFrom(exePath)
Dim formType As Type = a.GetType(formName)
'Dim resultForm As Form =
DirectCast(a.CreateInstance(formType.FullName),a.G etType(formName))
Dim resultForm As Form =
CType(a.CreateInstance(formType.FullName), Form)
Dim t As Thread = New Thread(New ThreadStart(AddressOf New
AppState(resultForm).RunApp))
t.ApartmentState = ApartmentState.STA
t.IsBackground = True
t.Start()
Return resultForm
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
Private Class AppState
Public Sub New(ByVal f As Form)
FormToRun = f
End Sub
Public ReadOnly FormToRun As Form
Public Sub RunApp()
Application.Run(FormToRun)
End Sub
End Class
End Module
using System;
using System.Windows.Forms; // Add Project Reference to
System.Windows.Forms.dll
using System.Reflection;
using System.Threading;
namespace RunScenario // Must change this
{
class Program
{
static Form testForm = null;
static BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance;
static int delay = 1500;
[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("\nStarting UI test scenario");
Console.WriteLine("\nLaunching WinApp under test");
string exePath = @"..\\..\\..\WinApp\bin\Debug\WinApp.exe";
testForm = LaunchApp(exePath, "WinApp.Form1");
Console.WriteLine("Moving form");
SetFormPropertyValue("Location", new System.Drawing.Point(200,200));
SetFormPropertyValue("Location", new System.Drawing.Point(500,500));
Console.WriteLine("Resizing form");
SetFormPropertyValue("Size", new System.Drawing.Size(600,600) );
SetFormPropertyValue("Size", new System.Drawing.Size(300,320) );
Console.WriteLine("\nSetting textBox1 to 'yellow'");
SetControlPropertyValue("textBox1", "Text", "yellow");
Console.WriteLine("Setting textBox1 to 'red'");
SetControlPropertyValue("textBox1", "Text", "red");
Console.WriteLine("Selecting comboBox1 to 'green'");
SetControlPropertyValue("comboBox1", "SelectedItem", "green");
Console.WriteLine("Selecting comboBox1 to 'blue'");
SetControlPropertyValue("comboBox1", "SelectedItem", "blue");
Console.WriteLine("Clicking button1");
InvokeMethod("button1_Click", new object[]{null, new EventArgs()} );
Console.WriteLine("\nChecking for state:\n 'red', 'blue', 'Result is
purple'");
bool pass = true;
if ((string)GetControlPropertyValue("textBox1", "Text") != "red") pass =
false;
if ((string)GetControlPropertyValue("comboBox1", "SelectedItem") !=
"blue") pass = false;
if ( !((ListBox.ObjectCollection)GetControlPropertyValu e("listBox1",
"Items")).Contains("Result is purple")) pass = false;
if (pass) Console.WriteLine("\nUI test scenario result: PASS");
else Console.WriteLine("\nUI test scenario result: *FAIL*");
Console.WriteLine("\nClicking menu File->Exit in 5 seconds . . . ");
Thread.Sleep(3500);
InvokeMethod("menuItem4_Click", new object[] {null, new EventArgs()} );
Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.ReadLine();
}
} // Main()
static void SetControlPropertyValue(
string controlName, string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new
SetControlPropertyValueHandler(SetControlPropertyV alue), new
object[]{controlName, propertyName, newValue});
return;
}
Type t1 = testForm.GetType();
FieldInfo fi = t1.GetField(controlName, flags);
object ctrl = fi.GetValue(testForm);
Type t2 = ctrl.GetType();
PropertyInfo pi = t2.GetProperty(propertyName, flags);
pi.SetValue(ctrl, newValue, new object[0]);
}
delegate void SetControlPropertyValueHandler(string controlName, string
propertyName, object newValue);
static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}
delegate void SetFormPropertyValueHandler(string propertyName, object
newValue);
static object GetControlPropertyValue(
string controlName, string propertyName)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
return testForm.Invoke(new
GetControlPropertyValueHandler(GetControlPropertyV alue),new
object[]{controlName, propertyName});
}
Type t1 = testForm.GetType();
FieldInfo fi = t1.GetField(controlName, flags);
object ctrl = fi.GetValue(testForm);
Type t2 = ctrl.GetType();
PropertyInfo pi = t2.GetProperty(propertyName, flags);
return pi.GetValue(ctrl, new object[0]);
}
delegate object GetControlPropertyValueHandler(string controlName, string
propertyName);
static void InvokeMethod(string methodName, params object[] parms)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new InvokeMethodHandler(InvokeMethod), new
object[]{methodName, parms});
return;
}
Type t = testForm.GetType();
MethodInfo mi = t.GetMethod(methodName, flags);
mi.Invoke(testForm, parms);
}
delegate void InvokeMethodHandler(string methodName, params object []
parms);
static Form LaunchApp(string exePath, string formName)
{
Thread.Sleep(delay);
Assembly a = Assembly.LoadFrom(exePath);
Type formType = a.GetType(formName);
Form resultForm = (Form)a.CreateInstance(formType.FullName);
Thread t = new Thread(new ThreadStart(new AppState(resultForm).RunApp));
t.ApartmentState = ApartmentState.STA;
t.IsBackground = true;
t.Start();
return resultForm;
}
private class AppState
{
public AppState(Form f) { FormToRun = f; }
public readonly Form FormToRun;
public void RunApp()
{
Application.Run(FormToRun);
}
}
} // class
} // ns
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Peter Proost" <pp*****@nospam.hotmail.com> schreef in bericht
news:uG**************@TK2MSFTNGP12.phx.gbl...
Hi Ken that did it, thanks. I still have got an other problem that I
thought would go away if my first problem was fixed but I'll first try to solve it
myself and if I can't solve it I'll post it. Thanks again
Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Ken Tucker [MVP]" <vb***@bellsouth.net> schreef in bericht
news:e2**************@TK2MSFTNGP10.phx.gbl... Hi,
Since you are not actually using x for anything I would use
nothing instead
pi.SetValue(testForm, newValue, nothing)
Ken
------------------
"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl... Hi I got this piece of c# code which runs ok but now I translated it
to vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:
System.Reflection.TargetParameterCountException: Parameter count mismatch. at System.Reflection.RuntimeMethodInfo.InternalInvoke (Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean
verifyAccess) at System.Reflection.RuntimeMethodInfo.InternalInvoke (Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Reflection.RuntimePropertyInfo.SetValue(Obj ect obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)
But I can't figure out why it throws an error so some help would be
much appreciated :-)
Thanks in advance
Greetz Peter
static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new
SetFormPropertyValueHandler(SetFormPropertyValue), new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}
Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New
SetFormPropertyValueHandler(AddressOf SetFormPropertyValue), New Object() _{propertyName, newValue})
Return
End If
Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object
pi.SetValue(testForm, newValue, x)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub