是否有可能使用Selenium,其中包含一个WebBrowser对象C#Windows窗体?有可能、窗体、其中包含、对象

由网友(笺)分享简介:我不知道是否有可能使用Selenium用C#Windows窗体包含WebBrowser对象。 我使用的是硒而我能够创建测试用例与硒脚本记录;我只是试图找出我是否能导出C#code和拥有这一切在C#环境中运行。我AP preciate任何想法或解释。 更新我给点有硒打开包含WebBrowser组件的WinFo...

我不知道是否有可能使用Selenium用C#Windows窗体 包含WebBrowser对象。

我使用的是硒 而我能够创建测试用例与硒脚本记录;我 只是试图找出我是否能导出C#code和 拥有这一切在C#环境中运行。我AP preciate任何想法或 解释。

更新 我给点有硒打开包含WebBrowser组件的WinForm的。然而从那里我的测试将不会执行。看起来它不理解硒命令。我没有看到被抛出,虽然任何错误消息。嗯

的事情是 winformWithWebBrowserTest .exe文件将打开的WinForm与网页浏览器打开。 但没有任何反应。下面code是一个触发了该.exe

测试code(硒命令)

 命名空间ClassLibrary1的
{
    一流的Class2
    {
        私人ISelenium硒;
        私人StringBuilder的verificationErrors;

        [建立]
        公共无效SetupTest()
        {

            硒=新DefaultSelenium
                (本地主机,4444,*自定义的C:用户 M-德 文件的Visual Studio 2010ProjectswinformWithWebBrowserTestwinformWithWebBrowserTestbinReleasewinformWithWebBrowserTest.exe", http://www.livemocha.com);

            selenium.Start();
            verificationErrors =新的StringBuilder();
        }

        [拆除]
        公共无效TeardownTest()
        {
            selenium.Stop();
        }

        [测试]
        公共无效TheUntitledTest()
        {
            //这里没有被执行:(
            Console.WriteLine(FOOBAR前);
            selenium.Open(/);
            Console.WriteLine(FOOBAR);
            selenium.WaitForPageToLoad(30000);
            selenium.Open(/用户/注销);
            selenium.Open(/用户/登录);


        }

    }
}
 

的.exe

 命名空间WindowsFormsApplication1
{
    公共部分类Form1中:形态
    {
        公共Form1中()
        {
            的InitializeComponent();
        }

        私人无效webBrowser1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
        {
           //这个被始终执行......
            的HtmlElement头= webBrowser1.Document.GetElementsByTagName(身体)[0];
            的HtmlElement scriptOne = webBrowser1.Document.CreateElement(脚本);
            IHTMLScriptElement元=(IHTMLScriptElement)scriptOne.DomElement;
            element.text =功能的sayHello(){+
                警报('你好'); +
                };
            head.AppendChild(scriptOne);
            webBrowser1.Document.InvokeScript(sayHello的);


        }
        //吸气二传手.....
        公共web浏览器getWebBrowser()
        {
            返回this.webBrowser1;

        }
        公共无效setWebBrowser(web浏览器WB)
        {
            this.webBrowser1 = WB;
        }

        //只是地址栏
        私人无效的button1_Click(对象发件人,EventArgs的)
        {
            如果(!string.IsNullOrEmpty(textBox1.Text))
            {
                WebBrowser1.Navigate时(textBox1.Text);
            }
        }

        //只是地址栏
        私人无效webBrowser1_Navigated(对象发件人,WebBrowserNavigatedEventArgs E)
        {
            如果(textBox1.Text!= e.Url.ToString())
            {
                textBox1.Text = e.Url.ToString();
            }
        }

    }
}
 
使用selenium IDE录制第一个脚本

更新

我做了我的测试简单,所以现在我不使用NUnit的。我创建的C#控制台应用程序只运行C#的.exe文件。但是,在我的控制台也将输出 - 1 - - 2 - 只。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Web;
使用Selenium;

命名空间ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {

            Console.WriteLine( -  1 ---);
            ISelenium硒;


            硒=新DefaultSelenium
                (本地主机,4444,*自定义的C:用户 M-takayashiki  文件的Visual Studio 2010ProjectswinformWithWebBrowserTestwinformWithWebBrowserTestbinReleasewinformWithWebBrowserTest.exe", http://www.livemocha.com);

            Console.WriteLine( -  2 ---);

            selenium.Start();

            Console.WriteLine( -  3 ---);


            selenium.Open(/);
            selenium.WaitForPageToLoad(30000);
            selenium.Open(/用户/注销);
            selenium.Open(/用户/登录);


            Console.WriteLine(测试测试测试);

            //拆除
            selenium.Stop();

        }
    }
}
 

更新

我检查RC日志:

  15:40:35.379 DEBUG [13] org.openqa.jetty.http.HttpContext  - 处理器org.openqa.selenium.server.SeleniumDriverResourceHandler在HttpContext的[/硒服务器,/硒服务器]
15:40:35.380 DEBUG [13] org.openqa.selenium.server.SeleniumDriverResourceHandler  -  REQ:POST /硒服务器/驱动器/ HTTP / 1.1
内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
主持人:本地主机:4444
内容长度:247
期望:100-继续
连接:保持活动

15:40:45.408 DEBUG [13] org.openqa.selenium.server.FrameGroupCommandQueueSet  - 等待窗口'空'本地帧'空'了1790多秒
15:40:45.408 DEBUG [13] org.openqa.selenium.server.FrameGroupCommandQueueSet  - 等待条件1000多毫秒
15:40:45.945 DEBUG [12] org.openqa.selenium.server.FrameGroupCommandQueueSet  - 得到了条件? : 假
 

解决方案

我还没有与硒做到这一点,但我做了同样的事情华廷时,我想自动化的InfoPath。我采取的方法如下:

消灭所有有,在我的情况下,将其命名为流程的InfoPath

 的foreach(过程PROC在Process.GetProcessesByName(InfoPath中))
{
  proc.Kill();
}
 

启动你的测试程序调用Process.GetProcessesByName让所有的进程的ID,然后拿到第一的窗口句柄,因为应该只有一个进程在运行。

 过程[] updatedInfopathProcessList = Process.GetProcessesByName(InfoPath中);
如果(updatedInfopathProcessList [0] .ID == 0)
{
    抛出新ApplicationException的(没有InfoPath中的进程存在);
}
infopathProcessId = updatedInfopathProcessList [0] .ID;
infopathHwnd = updatedInfopathProcessList [0] .MainWindowHandle;
 

现在,我有窗口句柄,我得到的IHTMLDocument2自动对与华廷。

  InternalHTMLDOMDocument = IEDom.IEDOMFromhWnd(this.hWnd);
 

真正的橡胶打在IEDom类的道路... code是低于...

 命名空间ITIN
    {
        公共类IEDom
        {

            内部静态的IHTMLDocument2 IEDOMFromhWnd(IntPtr的的HWND)
            {
                GUID IID_IHTMLDocument2 =新的GUID(626FC520-A41E-11CF-A731-00A0C9082637);

                的Int32 LRES = 0;
                INT32 lMsg;
                INT32小时;

                //如果(IsIETridentDlgFrame(HWND))
                // {
                    如果(!IsIEServerWindow(HWND))
                    {
                        //获取第一个孩子的IE服务器窗口
                        的hWnd = NativeMethods.GetChildWindowHwnd(HWND,互联网Explorer_Server);
                    }

                    如果(IsIEServerWindow(HWND))
                    {
                        //注册信息
                        lMsg = NativeMethods.RegisterWindowMessage(WM_HTML_GETOBJECT);
                        //获取对象
                        NativeMethods.SendMessageTimeout(HWND,lMsg,0,0,NativeMethods.SMTO_ABORTIFHUNG,1000,参考LRES);
                        如果(LRES!= 0)
                        {
                            //从LRES对象
                            的IHTMLDocument2 ieDOMFromhWnd = NULL;
                            HR = NativeMethods.ObjectFromLresult(LRES,参考IID_IHTMLDocument2,0,参考ieDOMFromhWnd);
                            如果(小时!= 0)
                            {
                                抛出新收到COMException(ObjectFromLresult发生异常,人力资源);
                            }
                            返回ieDOMFromhWnd;
                        }
                    }
               //}
                返回null;
            }

            内部静态布尔IsIETridentDlgFrame(IntPtr的的HWND)
            {
                返回UtilityClass.CompareClassNames(HWND,互联网Explorer_TridentDlgFrame);
            }

            私有静态布尔IsIEServerWindow(IntPtr的的HWND)
            {
                返回UtilityClass.CompareClassNames(HWND,互联网Explorer_Server);
            }
        }
    }
 

抱歉,这并不是硒,但它是怎么解决的华廷的问题,所以我希望它可以提供帮助。在code是这里 HTTP://itin.$c$cplex.com/ ,同时我也认为,这已被添加到华廷为好。

I'm wondering if it's possible to use Selenium with a C# Windows Form that contains a WebBrowser object.

I'm using selenium and I'm able to create test cases with the Selenium script record; I'm just trying to pinpoint whether or not I can export the C# code and have it all run within a C# environment. I appreciate any thoughts or explanations.

update I got to the point to have Selenium open the WinForm which contains the WebBrowser Component. However from there my test won't execute. Looks like it doesn't understand selenium command. I don't see any error messages being thrown though. hmmm

The thing is winformWithWebBrowserTest.exe which opens winForm with webbrowser is opened. But nothing happens. Below code is the one that fires up the .exe

test code (Selenium command)

namespace ClassLibrary1
{
    class Class2
    {
        private ISelenium selenium;
        private StringBuilder verificationErrors;

        [SetUp]
        public void SetupTest()
        {

            selenium = new DefaultSelenium
                ("localhost", 4444, "*custom C:Usersm-takDocumentsVisual Studio 2010ProjectswinformWithWebBrowserTestwinformWithWebBrowserTestbinReleasewinformWithWebBrowserTest.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("/users/logout");
            selenium.Open("/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

I made my test simple so now I don't use NUnit. I've created C# console app just to run the c# .exe file. But in my console it would output "--1--" and "--2--" only..

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:Usersm-takayashikiDocumentsVisual Studio 2010ProjectswinformWithWebBrowserTestwinformWithWebBrowserTestbinReleasewinformWithWebBrowserTest.exe", "http://www.livemocha.com");

            Console.WriteLine("-- 2---");

            selenium.Start();

            Console.WriteLine("-- 3---");


            selenium.Open("/");
            selenium.WaitForPageToLoad("30000");
            selenium.Open("/users/logout");
            selenium.Open("/users/login");


            Console.WriteLine("test test test");

            //tear down
            selenium.Stop();

        }
    }
}

UPDATE

I checked RC log:

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

解决方案

I haven't done this with selenium, but I did the same thing with WatiN when I wanted to automate InfoPath. The approach that I took was as follows:

Kill off all the processes that have that name in my case it was "infopath"

foreach (Process proc in Process.GetProcessesByName("infopath"))
{    
  proc.Kill();
}

Launch your test application call Process.GetProcessesByName to get all the process id's then get the window handle of the first as there should only be one process running.

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;

Now that I had the window handle, I get the IHTMLDocument2 to automate against with WatiN.

InternalHTMLDOMDocument = IEDom.IEDOMFromhWnd(this.hWnd);

The real rubber hits the road in the IEDom class ... code is below ...

    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");
            }
        }
    }

Sorry this is not for selenium, but it is how I solved the problem for WatiN so I hope it can help. The code is here http://itin.codeplex.com/, and I also think that this has been added to WatiN as well.

阅读全文

相关推荐

最新文章