伟大的!很高兴看到 Selenium WebDriver 的另一个初学者!我想推荐一个可以帮助您入门的项目。
你可以在这里下载,或者从 github 上查看,这里
不需要使用 Maven,但强烈建议使用某种依赖管理软件,如 Maven、Ant 或其他。为什么?因为假设您有一个 jar 文件,并且您有多个贡献者。这些贡献者将如何获得依赖?他们必须自己下载jar吗?你怎么知道他们下载了正确的版本?!
我刚刚给你的selenium入门项目,是一个我已经工作了一段时间的个人项目,它具有我在使用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.
}
}
简单的!不?如果您确实需要有关提供的此框架的帮助,我可以为您提供帮助。