是否有可能使用Selenium与C#窗口包含WebBrowser对象的窗体?
我想知道是否有可能使用包含WebBrowser对象的C#Windows窗体 使用Selenium。是否有可能使用Selenium与C#窗口包含WebBrowser对象的窗体?
我使用硒 ,我可以用Selenium脚本记录创建测试用例;我是 只是试图查明我是否可以导出C#代码,并且它们都在C#环境中运行。我很欣赏任何想法或 的解释。
更新 我已经到了让Selenium打开包含WebBrowser组件的WinForm的地步。但是从那里我的测试不会执行。看起来它不懂硒指令。我没有看到任何错误消息被抛出。嗯
事情是winformWithWebBrowserTest
.exe打开winForm与webbrowser打开。 但没有任何反应。下面的代码是我提出,触发了该.exe
测试代码(硒命令)的一个
namespace ClassLibrary1
{
class Class2
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium
("localhost", 4444, "*custom C:\\Users\\m-tak\\Documents\\Visual Studio 2010\\Projects\\winformWithWebBrowserTest\\winformWithWebBrowserTest\\bin\\Release\\winformWithWebBrowserTest.exe", "http://www.livemocha.com");
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
selenium.Stop();
}
[Test]
public void TheUntitledTest()
{
//nothing here gets executed :(
Console.WriteLine("foobar front");
selenium.Open("/");
Console.WriteLine("foobar");
selenium.WaitForPageToLoad("30000");
selenium.Open("https://stackoverflow.com/users/logout");
selenium.Open("https://stackoverflow.com/users/login");
}
}
}
.EXE
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//this gets executed always......
HtmlElement head = webBrowser1.Document.GetElementsByTagName("body")[0];
HtmlElement scriptOne = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptOne.DomElement;
element.text = "function sayHello() { " +
"alert('hello');" +
" }" ;
head.AppendChild(scriptOne);
webBrowser1.Document.InvokeScript("sayHello");
}
//getter setter.....
public WebBrowser getWebBrowser()
{
return this.webBrowser1;
}
public void setWebBrowser(WebBrowser wb)
{
this.webBrowser1 = wb;
}
//just address bar
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
webBrowser1.Navigate(textBox1.Text);
}
}
//just address bar
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (textBox1.Text != e.Url.ToString())
{
textBox1.Text = e.Url.ToString();
}
}
}
}
UPDATE
我的测试很简单,所以现在我不使用NUnit。我创建了C#控制台应用程序来运行c#.exe文件。但是,在我的控制台也将输出"--1--"
和“只--2--"
..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Selenium;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-- 1---");
ISelenium selenium;
selenium = new DefaultSelenium
("localhost", 4444, "*custom C:\\Users\\m-takayashiki\\Documents\\Visual Studio 2010\\Projects\\winformWithWebBrowserTest\\winformWithWebBrowserTest\\bin\\Release\\winformWithWebBrowserTest.exe", "http://www.livemocha.com");
Console.WriteLine("-- 2---");
selenium.Start();
Console.WriteLine("-- 3---");
selenium.Open("/");
selenium.WaitForPageToLoad("30000");
selenium.Open("https://stackoverflow.com/users/logout");
selenium.Open("https://stackoverflow.com/users/login");
Console.WriteLine("test test test");
//tear down
selenium.Stop();
}
}
}
UPDATE
我检查RC日志:
15:40:35.379 DEBUG [13] org.openqa.jetty.http.HttpContext - Handler org.openqa.selenium.server.SeleniumDriverResourceHandler in HttpContext[/selenium-server,/selenium-server]
15:40:35.380 DEBUG [13] org.openqa.selenium.server.SeleniumDriverResourceHandler - req: POST /selenium-server/driver/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:4444
Content-Length: 247
Expect: 100-continue
Connection: keep-alive
15:40:45.408 DEBUG [13] org.openqa.selenium.server.FrameGroupCommandQueueSet - waiting for window 'null' local frame 'null' for 1790 more secs
15:40:45.408 DEBUG [13] org.openqa.selenium.server.FrameGroupCommandQueueSet - waiting for condition for 1000 more ms
15:40:45.945 DEBUG [12] org.openqa.selenium.server.FrameGroupCommandQueueSet - got condition? : false
在理论上这是可行的,只要硒能注入JS插入到浏览器组件中,你需要使用* custom命令并传入你希望Selenium启动的可执行文件,然后它会尝试做你想做的事。
我认为你可以,但你可能不得不使用Selenium RC。它有一个.Net版本的Selenium API。
是的,我知道硒测试可以使用C#编写,但我不知道硒是否可以在某种程度上与winForm对话,并且winForm可以理解命令以自动执行某些操作,如按钮点击等。 – 2011-03-07 02:28:46
您只需要做在C#代码中调用任何你需要的东西。如果你想使用一个winform按钮,那么你所需要做的就是使用buttonname.PerformClick()。如果有一个特定的函数需要运行,那么调用该函数应该很简单。 – 2011-03-07 09:36:43
所以你的意思是,硒只是执行,然后其余的C#代码是...只是C#代码?我的意思是不使用Selennese,比如“selenium.open(”/ somepath“)”cuz selenese似乎不适合我。 – 2011-03-07 10:22:27
我还没有用硒做过这件事,但是当我想让InfoPath自动化时,我用WatiN做了同样的事情。我采取的方法如下:
-
消灭所有有,在我的情况下,它的名字是“的InfoPath”
foreach (Process proc in Process.GetProcessesByName("infopath")) { proc.Kill(); }
-
启动测试应用程序调用Process.GetProcessesByName的过程获取所有进程ID然后获得第一个窗口句柄,因为应该只有一个进程正在运行。
Process[] updatedInfopathProcessList = Process.GetProcessesByName("infopath"); if (updatedInfopathProcessList[0].Id == 0) { throw new ApplicationException("No Infopath processes exist"); } infopathProcessId = updatedInfopathProcessList[0].Id; infopathHwnd = updatedInfopathProcessList[0].MainWindowHandle;
-
既然我有窗口句柄,我就可以使用IHTMLDocument2来自动化WatiN。
InternalHTMLDOMDocument = IEDom.IEDOMFromhWnd(this.hWnd);
真正的橡胶打在IEDom类道路...代码如下...
namespace ItiN
{
public class IEDom
{
internal static IHTMLDocument2 IEDOMFromhWnd(IntPtr hWnd)
{
Guid IID_IHTMLDocument2 = new Guid("626FC520-A41E-11CF-A731-00A0C9082637");
Int32 lRes = 0;
Int32 lMsg;
Int32 hr;
//if (IsIETridentDlgFrame(hWnd))
//{
if (!IsIEServerWindow(hWnd))
{
// Get 1st child IE server window
hWnd = NativeMethods.GetChildWindowHwnd(hWnd, "Internet Explorer_Server");
}
if (IsIEServerWindow(hWnd))
{
// Register the message
lMsg = NativeMethods.RegisterWindowMessage("WM_HTML_GETOBJECT");
// Get the object
NativeMethods.SendMessageTimeout(hWnd, lMsg, 0, 0, NativeMethods.SMTO_ABORTIFHUNG, 1000, ref lRes);
if (lRes != 0)
{
// Get the object from lRes
IHTMLDocument2 ieDOMFromhWnd = null;
hr = NativeMethods.ObjectFromLresult(lRes, ref IID_IHTMLDocument2, 0, ref ieDOMFromhWnd);
if (hr != 0)
{
throw new COMException("ObjectFromLresult has thrown an exception", hr);
}
return ieDOMFromhWnd;
}
}
// }
return null;
}
internal static bool IsIETridentDlgFrame(IntPtr hWnd)
{
return UtilityClass.CompareClassNames(hWnd, "Internet Explorer_TridentDlgFrame");
}
private static bool IsIEServerWindow(IntPtr hWnd)
{
return UtilityClass.CompareClassNames(hWnd, "Internet Explorer_Server");
}
}
}
抱歉,这并不是对硒,但它是我如何解决WatiN的问题,所以我希望它可以帮助。代码在这里http://itin.codeplex.com/,我也认为这已经被添加到WatiN中。
感谢您的示例代码,我会尝试将这种方法应用于Selenium。 – 2011-03-08 00:29:00
@ masato-san 2015年的问候。你有没有设法完成这个? – 2015-11-24 08:53:00
我们稍微攻击了selenium webdriver代码以附加到Windows应用程序内部的嵌入式IE实例。 http://bradbax.blogspot.ca/2013/07/driving-embedded-wpf-browser-with.html
http://www.theautomatedtester.co.uk/非常有趣的阅读硒。所以看起来像只有selenium2可以处理.NET实现 – 2011-03-01 00:45:38
我是.NET维护者,所以只显示该代码。 IT可以.NET,Java,Ruby和Python – AutomatedTester 2011-03-02 16:40:46
你能发布一些示例代码吗? – 2011-03-07 00:57:41