我已經2天了.NET C#編碼器試圖獲得控制ProjectA - WinForm從ProjectB - C#控制檯應用程序。是否有可能從另一個C#控制檯應用程序控制WinForm?
基本上我使用WatiN在ProjectA中的WebBrowser控件內自動測試。
當我運行ProjectB執行winformWithWebBrowserTest.exe時,帶有webbrowser的winform出現了。但後來無法訪問form1。我如何從ProjectB訪問webbrowser控件?
錯誤:
System.Runtime.IteropServices.InvalidComObjectException
COM
object that has been separated from its underlying RCW cannot be used
項目A的WinForm:(winformWithWebBrowserTest.exe)
namespace WindowsFormsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
}//end class
}//end namespace
項目B控制檯應用程序:(WatinConsoleExample.cs)
namespace ConsoleApplication1
{
class WatinConsoleExample
{
[STAThread]
static void Main(string[] args)
{
//run ProjectA exe
System.Diagnostics.Process Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "C:\\Users\\m-takayashiki\\Documents\\Visual Studio 2010\\Projects\\winformWithWebBrowserTest\\winformWithWebBrowserTest\\bin\\Release\\winformWithWebBrowserTest.exe";
Proc.Start();
WindowsFormsApplication1.Form1 form1 = new Form1();
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
//exception occurs below ..........
var ie = new IE(form1.webBrowser1.ActiveXInstance);
ie.GoTo("http://www.google.com");
ie.TextField(Find.ByClass("lst")).TypeText("this is awesome!!");
ie.Button(Find.ByName("btnG")).Click();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
}
}
是否有一個特殊的原因,您不只是直接在瀏覽器中運行網站,並使用衆多瀏覽器自動化工具之一來驅動它,如watin或selenium? – 2011-03-08 17:10:59
我的項目是使用Selenium在.NET中啓用對webbrowser的自動化,但似乎WatiN是要走的路。 – 2011-03-09 00:41:30