[3]Selenium学习系列---- 一个简单的Selenium Java 工程
来源:互联网 发布:mac截屏方法 编辑:程序博客网 时间:2024/06/10 02:40
在前面的一个章节,笔者介绍了[1]Selenium学习系列---- Selenium IDE工具的安装,顺便告诉大家一个小秘密,其实Selenium IDE还可以录制网页的操作过程,并导出成C#,Java,Python,Ruby等各种各样形式的代码。笔者就以Selenium IDE录制并导出Java代码为例子,给大家演示一个Selenium Hello World项目。
1. 打开Selenium IDE,并输入一个目标网站地址并开启录制功能。本文以Tomcat的官方网站为例子,http://tomcat.apache.org/
2. 上面红线框内的准备就绪后,点击Selenium IDE右边录制按钮,开始录制。
3. 这个时候在http://tomcat.apache.org/网站上的任意操作都会被Selenium IDE记录下来。
4. 选择导出代码,并取名为ApacheWebSiteTest.java
5. 打开Eclipse,新建一个Maven的工程,下面是Maven的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.sample.selenium</groupId><artifactId>selenium-learn</artifactId><version>0.0.1-SNAPSHOT</version><name>selenium-learn</name><description>selenium-learn1</description><repositories><repository><id>central</id><name>Maven Repository Switchboard</name><layout>default</layout><url>http://repo1.maven.org/maven2</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.0.1</version></dependency></dependencies></project>
6. 把上面的ApacheWebSiteTest.java复制到这个maven项目中。
package com.example.tests;import java.util.regex.Pattern;import java.util.concurrent.TimeUnit;import org.junit.*;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.Select;public class ApacheWebSiteTest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "http://tomcat.apache.org/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testApacheWebSite() throws Exception { driver.get(baseUrl + "/"); driver.findElement(By.linkText("Home")).click(); driver.findElement(By.linkText("PoweredBy")).click(); driver.findElement(By.linkText("Tomcat Connectors")).click(); driver.findElement(By.linkText("Tomcat 8.5")).click(); driver.findElement(By.xpath("(//a[contains(text(),'Tomcat Native')])[2]")).click(); driver.findElement(By.linkText("archive download site")).click(); driver.findElement(By.linkText("Tomcat Native")).click(); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } }}7. 注意上面的代码跑起来是会抛出下面的异常的
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releasesat com.google.common.base.Preconditions.checkState(Preconditions.java:199)at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:37)at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:95)at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:247)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:242)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:238)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:127)at com.example.tests.ApacheWebSiteTest.setUp(ApacheWebSiteTest.java:20)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)java.lang.NullPointerExceptionat com.example.tests.ApacheWebSiteTest.tearDown(ApacheWebSiteTest.java:39)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
8. 解决方法是,针对Firefox浏览器,需要下载geckodriver.exe web驱动(webDriver,网上可以搜索geckodriver.exe 的下载)并在setup()方法中设置下面的类似代码:
@Before public void setUp() throws Exception { System.setProperty("webdriver.gecko.driver","D:/drivers/geckodriver.exe"); driver = new FirefoxDriver(); baseUrl = "http://tomcat.apache.org/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); }
恭喜你,第一个Selenium Hello World的例子就这样写成了,是不是很简单啊!!!!
对了,对于上面7中遇到的问题,还有另外一种更智能和方便的解决方案(前提是读者的网络足够强大能访问这些网站,具体原因,你懂的!)
在Pom.xml文件的依赖中加入
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>1.4.10</version></dependency>
然后在每次新建WebDriver对象之前,调用下面的方法。比如在setup()方法中。
MarionetteDriverManager.getInstance().setup();
如果读者对于后面的一种方式感兴趣的话,可以自己测试验证。
- [3]Selenium学习系列---- 一个简单的Selenium Java 工程
- 一个简单的selenium demo
- 适合selenium rc初学者的一个简单java测试案例
- 一个简单的139邮箱登录脚本--->java-selenium
- [1]Selenium学习系列---- Selenium IDE工具的安装
- 使用Selenium的一个简单程序
- java web + testNG + ant + Selenium整合对IE浏览器的自动化测试二(一个简单的Selenium测试)
- 基于selenium的简单Java爬虫
- JAVA操作selenium的简单示例
- selenium的简单实用
- Selenium 2 系列学习一: JAVA环境搭建(IDEA)
- selenium学习3 selenium for IE8+WIN7
- Mac安装Ruby+Cucumber+Selenium 然后运行一个简单的ruby脚本用selenium-webdriver
- java selenium的使用
- Selenium WebDriver java 简单实例
- Selenium学习笔记3
- selenium 学习3
- Selenium 学习札记3
- “加密系统”的巨坑
- 一个关于转义字符的很有意思的事情”\\n“
- Python菜鸟--File(文件) 方法
- 【1410 - Consistent Verdicts】+ 思维
- 8086寻址方式
- [3]Selenium学习系列---- 一个简单的Selenium Java 工程
- VMware11+centos6.6+5.7.16 MySQL Community Server
- ASP.NET教程(一) 概述
- 循环神经网络(RNN, Recurrent Neural Networks)介绍
- Python菜鸟--异常处理
- Java 泛型
- 交换两个变量的值 不使用第三个变量
- java的深复制和浅复制
- 时间转换1