【问题标题】:How to test a web page using selenium through a desktop application?如何通过桌面应用程序使用 selenium 测试网页?
【发布时间】:2013-10-25 22:18:33
【问题描述】:

我需要通过桌面应用程序测试网页,我正在尝试使用 selenium IDE,我成功创建了测试用例,但我没有 能够在java上执行它们。

我一直在寻找有用的东西,但我根本找不到任何帮助。

谢谢

【问题讨论】:

  • 桌面应用你说的是IE、Firefox或chrome之类的浏览器???
  • IDE 允许您将其导出为 Java 代码。

标签: java swing testing selenium desktop


【解决方案1】:

为此而创建的框架(使用 Java)可以下载 here 或者您可以从 github here 查看项目。

这个项目的设计非常简单,但非常有效。这种类型的框架是我对我每天在生产型环境中使用的框架的解释的“免费版本”。

在名为SampleFunctionalTest.java 的项目中包含一个示例测试。假设您按照自述文件阅读 T,您应该可以毫无问题地开始。

这是该框架中测试的样子。

@Config(url = "http://ddavison.github.io/tests/getting-started-with-selenium.htm", browser = Browser.FIREFOX) // You are able to specify a "base url" for your test, from which you will test. You may leave `browser` blank.
public class SampleFunctionalTest extends AutomationTest {

    /**
     * You are able to fire this test right up and see it in action.  Right click the test() method, and click "Run As... jUnit test".
     * 
     * The purpose of this is to show you how you can continue testing, just by taking the semi colon out, and continuing with your test.
     */
    @Test
    public void test() {

            // click / validateAttribute
        click(props.get("click"))
        .validateAttribute(props.get("click"), "class", "success") // validate that the class indeed added.

        // setText / validateText
        .setText(By.id("setTextField"), "woot!")
        .validateText(By.id("setTextField"), "woot!") // validates that it indeed set.

        // check / uncheck
        .check(By.id("checkbox"))
        .validateChecked(By.id("checkbox")) // validate that it checked

        .check(props.get("radio.2")) // remember that props come from <class name>.properties, and are always CSS selectors. (why use anything else, honestly.)
        .validateUnchecked(props.get("radio.1")) // since radio 1 was selected by default, check the second one, then validate that the first radio is no longer checked.

        // select from dropdowns.
        .selectOptionByText(By.xpath("//select[@id='select']"), "Second") // just as a proof of concept that you can select on anything. But don't use xpath!!
        .validateText(By.id("select"), "2") // validateText() will actually return the value="" attr of a dropdown, so that's why 2 works but "Second" will not.

        .selectOptionByValue(By.cssSelector("select#select"), "3")
        .validateText(props.get("select"), "3")

        // frames
        .switchToFrame("frame") // the id="frame"
        .validatePresent(By.cssSelector("div#frame_content"))

        // windows
        .switchToWindow("Getting Started with Selenium") // switch back to the test window.
        .click(By.linkText("Open a new tab / window"))
        .waitForWindow("Google") // waits for the url.  Can also be the you are expecting. :) (regex enabled too)
        .setText(By.name("q"), "google!")
        .closeWindow(); // we've closed google, and back on the getting started with selenium page.

    }
}

【讨论】:

    【解决方案2】:

    您应该创建一个 WebDriver 的实例并在该对象的实例上调用方法。

    这里显示了一个简单的示例:http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example

    【讨论】:

      【解决方案3】:

      希望你已经在 webdriver 中创建了脚本。

      现在在 selenium ide 记录的脚本中,您有三个方法称为 设置、testSomeName 和拆卸。

      从最基本的开始:要运行这个脚本,你需要做的就是在同一个类中创建一个主方法,并且你需要按照上面指定的顺序调用这些方法。

      之后你只需要运行那个程序。

      这是一个更清楚的例子:

      public class p_adjcb {
      
          public void setUp() throws Exception {
          }
      
          public void testP_adjcb() throws Exception {
          }
      
          public void tearDown() throws Exception {
          }
      
          public static void main(String[] args) {
              p_adjcb obj = new p_adjcb();
              try {
                  obj.setUp();
                  obj.testP_adjcb();
                  obj.tearDown();
              } catch (Exception ex) {
              }
          }
      }
      

      如果您遇到任何编译器错误,请确保您已下载 selenium-standalone-server.jar 文件并将其添加到您的类路径中。 这是一个非常基本的开始。稍后您可能需要使用像 junit 这样的 som 框架。

      希望对你有帮助。

      【讨论】:

      • 这很糟糕.. 如果您甚至不使用 junit,为什么还要在其中放置 junit 测试.. 您正在使用 Java 处理此任务,而不是您应该使用的 jUnit。
      • @sircapsalot:非常感谢。刚刚删除了注释。
      • 啊是的,好多了:)
      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2023-02-18
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      相关资源
      最近更新 更多