自动化测试【Maven+Eclipse+Selenium+Java环境搭建和测试】

一、下载必要的文件

1、eclipse

Eclipse官网

2、jdk

jdk官网

3、selenium IDE、Selenium Server、Selenium Client Drivers(java)等等

Selenium下载地址备注:需要代理服务器才能下载 我使用的是太太猫

4、maven安装、配置等二、安装 1、Eclipse解压缩就可以用了 2、jdk安装、配置变量等 3、Selenium相关的安装 4、maven

最新版本的Eclipse已经自带maven

三、运行 1、Eclipse建个maven工程,建成后,,直接修改pom.xml <project xmlns="" xmlns:xsi=""xsi:schemaLocation=" http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Selenium2Test</groupId><artifactId>Selenium2Test</artifactId><version>1.0</version><dependencies><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.25.0</version></dependency><dependency><groupId>com.opera</groupId><artifactId>operadriver</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>com.opera</groupId><artifactId>operadriver</artifactId><version>0.16</version><exclusions><exclusion><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-remote-driver</artifactId></exclusion></exclusions></dependency></dependencies></dependencyManagement></project> pom.xml修改保存后,Eclipse会自动把需要的jar包下载完成 给项目工程配置所需要的库 工程右击properties->java build path->add library 2、测试Firefox(ExampleForFirefox.java)import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;public class ExampleForFireFox {public static void main(String[] args) {// 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置//System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");// 创建一个 FireFox 的浏览器实例WebDriver driver = new FirefoxDriver();// 让浏览器访问 Baidudriver.get("");// 用下面代码也可以实现// driver.navigate().to("");// 获取 网页的 titleSystem.out.println("1 Page title is: " + driver.getTitle());// 通过 id 找到 input 的 DOMWebElement element = driver.findElement(By.id("kw"));// 输入关键字element.sendKeys("zTree");// 提交 input 所在的 formelement.submit();// 通过判断 title 内容等待搜索页面加载完毕,间隔10秒(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {public Boolean apply(WebDriver d) {return d.getTitle().toLowerCase().endsWith("ztree");}});// 显示搜索结果页面的 titleSystem.out.println("2 Page title is: " + driver.getTitle());//关闭浏览器driver.quit();}

运行正常的情况下就可以看到自动打开Firefox窗口,访问baidu.com,然后输入关键字并查询

在搭建、测试中出现了几个问题:

问题1: Description Resource Path Location Type ArtifactTransferException: Failure to transfer com.google.code.findbugs:jsr305:jar:1.3.9 from was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact com.google.code.findbugs:jsr305:jar:1.3.9 from/to central (): repo.maven.apache.org pom.xml /lesson line 2 Maven Dependency Problem解决方案: 可能是连不上这个仓库,在pom.xml文件加一下下面的配置试试看。 xml代码: <repositories><repository><snapshots><enabled>false</enabled></snapshots><id>central</id><name>Maven Repository Switchboard</name><url></url></repository></repositories>问题2: ava文件(.java)右键run as没有java application 解决方法: public static void main(String[] args) 这几个必不可少 public static void main String[] arg 或者 String arg[] 可能少掉其中一个元素问题3: Caused by: com.thoughtworks.selenium.SeleniumException: Failed to start new browser session: java.lang.RuntimeException: Browser not supported: (Did you forget to add a *?) 分析:出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty 设置环境变量 "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。解决方法: 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置 System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); 3、测试IE Selenium 主要也就是针对 FireFox 和 IE 来制作的,所以把 FireFox 的代码修改为 IE 的,那是相当的容易,只需要简单地两步: 1)把 ExampleForFireFox.java 另存为 ExampleForIE.java 2)把 WebDriver driver = new FirefoxDriver(); 修改为 WebDriver driver = new InternetExplorerDriver(); 3)一般大家的 IE都是默认路径,所以也就不用设置 property 了

不能接受失败,也意味太想去成功了,从心理学上解释,

自动化测试【Maven+Eclipse+Selenium+Java环境搭建和测试】

相关文章:

你感兴趣的文章:

标签云: