代码之家  ›  专栏  ›  技术社区  ›  Jeflopo

关于使用Eclipse和/或不使用Maven设置Selenium项目

  •  2
  • Jeflopo  · 技术社区  · 11 年前

    我是一个完全的初学者,只是想用ChromeDriver或HTMLUnitDriver创建一个Selenium Web驱动程序项目。如果我是对的,请告诉我。我读过这篇文章,为了设置Selenium Webdriver项目,我可以通过在netbeans中创建一个Maven项目来完成,并配置pom.xml文件来下载依赖项。但我真的不知道从哪里开始,也不知道现在如何正确配置Maven。

    这让我觉得。。。Maven是强制性的吗?要创建一个Selenium项目,我必须处理Maven?可以只下载库( http://www.seleniumhq.org/download/ )并将它们包含在“全局库”部分中,然后将库添加到项目“库”文件夹中,并使用import子句来使用一个或另一个包?

    2 回复  |  直到 11 年前
        1
  •  2
  •   ddavison    11 年前

    太棒了很高兴看到另一个Selenium WebDriver初学者!我想推荐一个可以帮助你开始的项目。

    你可以下载 here ,或从github查看, here

    不需要使用Maven,但它 强烈建议使用某种依赖性管理软件,如Maven、Ant或其他软件。 为什么? 因为假设您有一个jar文件,并且您有多个贡献者。这些贡献者将如何获得依赖性?他们必须自己下载罐子吗?你怎么知道他们下载了正确的版本?!

    我刚刚给你的硒项目入门,是我已经做了一段时间的个人项目,它有我想要的东西 当我开始使用带有Java的Selenium 2 WebDriver时。它还使用了一个概念,我在现实生活中用现实生活中的回归系统实现了这个概念。

    由于你是新手,你可能已经注意到了使用 driver.findElement(blah).click() 还有诸如此类丑陋的东西。这个框架特别通过提供方法(你可以流利地调用这些方法)将你从所有的困惑中抽象出来。

    这个项目实际上 使用maven,您可以看到一个活跃的例子,说明maven如何与Java和jUnit一起工作,以提供一个不错的框架。

    以下是框架中包含的一个示例-

    /**
     * This is a sample test that can get you started.
     * <br><br>
     * This test shows how you can use the concept of "method chaining" to create successful, and independent tests, as well as the validations method that can get you started.
     * @author ddavison
     *
     */
    
    @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
  •  1
  •   Philipp Sander    11 年前

    Maven是强制性的吗?

    不不是的

    可以只下载库并将其包括在内吗?

    是的,这是可能的!

    Maven也会这么做!(简化)它创建了一个包含所有jar的库,并将其添加到您的项目中。