是否可以将Selenium与包含WebBrowser对象的C#窗体一起使用?

mas*_*san 6 .net selenium webbrowser-control winforms

我想知道是否可以将Selenium与包含WebBrowser对象的C#Windows窗体一起使用.

我正在使用selenium,我可以使用Selenium脚本记录创建测试用例; 我只想确定是否可以导出C#代码并让它们都在C#环境中运行.我感谢任何想法或解释.

更新 我得到了Selenium打开包含WebBrowser组件的WinForm的观点.但是从那里我的测试将不会执行.看起来它不懂selenium命令.我没有看到任何错误消息被抛出.嗯

事情是winformWithWebBrowserTest打开带有webbrowser的winForm的.exe.但没有任何反应.下面的代码是启动.exe的代码

测试代码(Selenium命令)

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("/users/logout");
            selenium.Open("/users/login");


        }

    }
}
Run Code Online (Sandbox Code Playgroud)

.可执行程序

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

    }
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

我让我的测试变得简单,所以现在我不使用NUnit.我创建了C#console app只是为了运行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("/users/logout");
            selenium.Open("/users/login");


            Console.WriteLine("test test test");

            //tear down
            selenium.Stop();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Aut*_*ter 1

理论上,只要 Selenium 可以将 JS 注入到浏览器组件中,它就可以工作。您需要使用 *custom 命令并传入您希望 Selenium 启动的可执行文件,然后它将尝试执行您想要的操作。