如何在不需要打开浏览器的情况下运行 Selenium 系统测试?

发布于 2024-12-29 08:17:06 字数 1276 浏览 4 评论 0原文

我有一个使用 Selenium 创建的测试方法,与此类似:

[TestFixture]
public class Test_Google
{
        IWebDriver driver;

        [SetUp]
        public void Setup()
        {
            driver = new InternetExplorerDriver();
        }

        [TearDown]
        public void Teardown()
        {
            driver.Quit();
        }

    [Test]
    public void TestSearchGoogleForTheAutomatedTester()
        {
            //Navigate to the site
        driver.Navigate().GoToUrl("http://www.google.co.uk");
        //Find the Element and create an object so we can use it
        IWebElement queryBox = driver.FindElement(By.Name("q"));
        //Work with the Element that's on the page
        queryBox.SendKeys("The Automated Tester");
            queryBox.SendKeys(Keys.ArrowDown);
        queryBox.Submit();
        //Check that the Title is what we are expecting
        Assert.True(driver.Title.IndexOf("The Automated Tester") > -1);
    }
}

当测试运行时,它会打开一个 IE 并执行其测试。

想象一下,有 200 个这样的测试方法分布在多个测试装置中,这意味着 IE 必须打开和关闭多次(与测试装置一样多,因为每个测试装置将打开 1 个浏览器)。

如何在不需要打开浏览器的情况下运行 Selenium 系统测试?

我的意思是,例如,我认为可能可以开发一个 Windows 服务来在 WinForms Web 浏览器控件中运行 Selenium 测试,在这种情况下,不必每次都打开浏览器,并且测试可以自动无缝地运行。但不确定如何实施?

或者还有其他更好的方法吗?

谢谢,

I have a test method created using Selenium, something similar to this:

[TestFixture]
public class Test_Google
{
        IWebDriver driver;

        [SetUp]
        public void Setup()
        {
            driver = new InternetExplorerDriver();
        }

        [TearDown]
        public void Teardown()
        {
            driver.Quit();
        }

    [Test]
    public void TestSearchGoogleForTheAutomatedTester()
        {
            //Navigate to the site
        driver.Navigate().GoToUrl("http://www.google.co.uk");
        //Find the Element and create an object so we can use it
        IWebElement queryBox = driver.FindElement(By.Name("q"));
        //Work with the Element that's on the page
        queryBox.SendKeys("The Automated Tester");
            queryBox.SendKeys(Keys.ArrowDown);
        queryBox.Submit();
        //Check that the Title is what we are expecting
        Assert.True(driver.Title.IndexOf("The Automated Tester") > -1);
    }
}

When the test runs, it opens an IE and carries out its test.

Imagine there are 200 test methods like this spread across multiple test fixtures, which means IE has to be opened and closed many times (as many as test fixtures since 1 browser will be opened per test fixture).

How to run Selenium system tests without requiring to open the browser?

I mean for example I was thinking it might be possible to develop a windows service to run the Selenium tests in the WinForms Web Browser Control, in which case the browser doesn't have to be opened each time and the tests can run automatically and seemlessly. Not sure how to implement this though?

Or is there any other better known way?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖凝香 2025-01-05 08:17:06

不。Selenium 是用 JavaScript 编写的;它被设计为在真实浏览器中运行,以测试与真实浏览器的兼容性。还有许多其他工具旨在运行模拟浏览器的测试;您可以查看 HtmlUnitCanoo WebTest

希望这会对您有所帮助。

No. Selenium is written in JavaScript; it's designed to run in a real browser to test compatibility with a real browser. There are a variety of other tools out there designed to run tests that simulate a browser; you can look into HtmlUnit or Canoo WebTest.

Hope this will help you.

旧时光的容颜 2025-01-05 08:17:06

您尝试过 XLT 吗?
它根本不需要打开浏览器
http://www.xceptance.com/products/xlt/what-is -xlt.html

Have you tried XLT?
It doesnt require an open browser at all
http://www.xceptance.com/products/xlt/what-is-xlt.html

無心 2025-01-05 08:17:06

如果您愿意,您可以在一个浏览器实例中运行所有测试。您只需将您的 webdriver 实例传递给每个测试即可。就像在静态类中拥有一个单独的 WebDriver 一样,所有测试用例都可以从其中访问 WebDriver。工作正常,如果您有一个会话要保留,则很有用

You can run all your tests in one browser instance if you like. You just have to pass your webdriver instance to each test. Like having a singelton WebDriver in a static class from where all your testcases can access the WebDriver. Works fine and is usefull if you got a session to keep

饭团 2025-01-05 08:17:06

驱动程序=新的HtmlUnitDriver(true);
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

driver = new HtmlUnitDriver(true);
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文