在实践中使用 Selenium 2.0 WebDriver

发布于 12-02 16:41 字数 870 浏览 0 评论 0 原文

我想在 JUnit 中编写 Selenium 测试用例并在多个浏览器中测试我的项目,并且我想利用所有 Selenium 驱动程序实现相同接口的事实。

每个测试用例应该如下所示:

package fm;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.*;

public class HomepageTest {

    @Test
    public void testTitle(WebDriver driver) {
        driver.get("http://localhost/");
        assertEquals("Foo", driver.getTitle());
    }

    @Test
    public void testSearchForm(WebDriver driver) {
        //...
    }

}

传递的 WebDriver 实现应该在某个地方集中控制。我可能需要重写一些 JUnit 行为,我希望这是可能的。

我想这样做是为了避免两件事:

  • 代码重复:如果每个测试用例都会在@Before中初始化所有被测试的浏览器,那么测试套件将有很多难以维护的重复代码。
  • 测试套件的速度:如果我对顺序进行集中控制并通过 WebDriver 实现,我可以轻松地打开例如 Firefox,运行其中的所有测试用例,关闭它并打开下一个浏览器。如果每个测试用例都能够自行打开和关闭浏览器,那么每次测试运行都会增加大量时间。

有人知道我该怎么做吗?谢谢。

I want to write Selenium test cases in JUnit and test my projects in multiple browsers and I would like to take advantage of the fact that all Selenium drivers implement the same interface.

Each test case should look like this:

package fm;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.*;

public class HomepageTest {

    @Test
    public void testTitle(WebDriver driver) {
        driver.get("http://localhost/");
        assertEquals("Foo", driver.getTitle());
    }

    @Test
    public void testSearchForm(WebDriver driver) {
        //...
    }

}

The passed WebDriver implementations should be controlled somewhere centrally. I'll probably need to override some of the JUnit behaviour and I hope it's possible.

I want to do it this way in order to avoid two things:

  • Code repetition: If each test case would initialize all tested browsers in @Before, the test suite would have a lot of repeated code that is hard to maintain.
  • Speed of the test suite: If I had centralized control over the order and passed WebDriver implementations, I could easily manage to open for example Firefox, run all test cases in it, close it and open the next browser. If each test case would manage to open and close browsers on its own, it would add a lot of time to each test run.

Anybody have an idea how should I do it? Thanks.

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

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

发布评论

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

评论(3

浅暮の光 2024-12-09 16:41:56

在 Selenium 项目中,我们使用 "="">http://code.google.com/p/selenium/source/browse/trunk/java/client/test/org/openqa/selenium/AbstractDriverTestCase.java 然后我们的构建调用浏览器,我们在其中运行测试。

查看我们的代码库以获得一些灵感!

In the Selenium project we inject what we need using http://code.google.com/p/selenium/source/browse/trunk/java/client/test/org/openqa/selenium/AbstractDriverTestCase.java and then our build calls the browser and we get tests running in it.

Have a look at our code base to get some inspiration!

晌融 2024-12-09 16:41:56

请检查 ISFW 它支持 selenium webdriver/remote webdriver 以及传统的 (selenium1) rc 方式。
您需要使用常规 selenium api 编写代码
例如,

selenium.open(url);
selenium.type("loc", "text to type");
selenium.submit("loc");

这是工作演示。根据您的要求设置浏览器字符串。
该固件支持 selenium 传统方式以及 selenium 2 webdriver。您需要在应用程序属性中设置适当的浏览器字符串。以下是 Firefox 的不同浏览器配置:

  • *firefox - 需要在配置的主机/端口上运行的 selenium 服务器
    如果未找到,则固件将在 locahost/port
  • firefoxDriver 上检查/启动一个 - 将直接使用 firefox Web 驱动程序运行,无需
    selenium 服务器
  • firefoxRemoteDriver - 需要运行的 selenium 服务器
    配置的主机/端口,如果找不到,则固件将检查/启动一个
    locahost/port,它将在主机上使用 firefox web 驱动程序运行测试
    机器

IE 的方式相同 - *iexplore、*iehta、iexplorerDriver、iexplorerRemoteDriver
等等。

Please check with ISFW it supports selenium webdriver/remote webdriver as well as conventional (selenium1) rc way.
You need to write code using regular selenium api
for example

selenium.open(url);
selenium.type("loc", "text to type");
selenium.submit("loc");

Here is the working demo. Set browser String as per your requirement.
The FW support selenium conventional way as well as selenium 2 webdriver. You need to set appropriate browser string in application properties. Following are different browser configurations for Firefox:

  • *firefox - required selenium server running on configured host/port
    if not found then fw will check/start one on locahost/port
  • firefoxDriver – will run directly with firefox web driver without
    selenium server
  • firefoxRemoteDriver - required selenium server running on
    configured host/port if not found then fw will check/start one on
    locahost/port, it will run test using firefox web driver on host
    machine

Same way for IE - *iexplore, *iehta, iexplorerDriver, iexplorerRemoteDriver
and so on.

夏尔 2024-12-09 16:41:56

我做了你现在/曾经的事?尝试使用控制网络驱动程序的静态类,并且我所有需要相同网络驱动程序的测试都从那里获取它。当您运行需要使用同一会话的多个测试时,它确实很有帮助。所有测试都在一个浏览器中运行,因此并非每个测试都会打开一个新的浏览器实例。

也许你还应该看看 testNG。我的经验是 testNG 更适合使用 selenium 进行测试,因为它不太专注于独立测试。它提供了许多有用的功能。

I did what you are/were? trying to do with a static class that controlls the webdriver and all my test which need the same webdriver get it from there. It really helps when you are running multiple tests that need to use the same session. And all your tests run in one browser, so not every tests opens a new browser instance.

Maybe you should also have a look at testNG. I made the experience that testNG is better for tests with selenium since it is not so focused on independent tests. It offers a lot of useful functionality.

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