views:

18

answers:

1

I have a test harness that runs in an embedded IronPython engine in our C# app. Some of the tests are UI automation that simulate button clicks, etc.

# assume code to find an existing button, 'b' is an instance of System.Windows.Forms.Button
b.OnClick(EventArgs())

The issue I have is that the above code works on IPy 2.0.2 and not on 2.6. Reading the link below, I can see why it no longer works.

http://dlr.codeplex.com/Thread/View.aspx?ThreadId=57324

So I created a python sublclass as follows:

class PyButton(Button):pass

This works fine if you are going to create a new instance of PyButton and attempt the OnClick(). However, in my case, I need to perform an OnClick against an existing Button. Essentially I want to do the following, to allow me to simulate the click on the button:

  • Get a reference to existing winforms Button
  • Cast/convert Button to custom PyButton subclass (to allow for OnClick)
  • Call OnClick

I tried using clr.Convert:

import clr
from System.Windows.Forms import *
class PyButton(Button):pass
# assume b = existing button
pb = clr.Convert(b, clr.GetClrType(PyButton))

... but get this error:

expected Button_2$2, got Button
  input was pb = clr.Convert(b, clr.GetClrType(PyButton))
A: 

You can't - just like you can't call it from C# or VB.Net or other .NET languages without having a subclass. Instead you probably want to call Button.PerformClick()

Dino Viehland
Alas - Button.PerformClick() works fine in the case of Button, but it looks to me like Button is the only control with that method. In the more general case, I need to do clicks on other objects as well, such as menu items. I ended up using reflection to get at OnClick (as well as some of the other user input events). Thanks!
Tom E