gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:29:06 24 4
gpt4 key购买 nike

我是一个完全的初学者,只是想创建一个 Selenium Webdriver 项目,我认为使用 ChromeDriver 或 HTMLUnitDriver。请告诉我如果我是正确的。我读过要设置一个 Selenium Webdriver 项目,我可以通过在 netbeans 中创建一个 Maven 项目来实现,并配置 pom.xml 文件以下载依赖项。但我真的不知道从哪里开始,或者此时如何正确配置 Maven。

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

最佳答案

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

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

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

我刚刚给你的 getting-started-with-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.

}
}

简单!不?如果您确实需要有关此框架的帮助,我可以帮助您。

关于java - 关于使用 Eclipse 和/或不使用 Maven 设置 Selenium 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com