1、Selenium1(Remote control)
Selenium1包含Selenium服务器(RC)和客户端库文件两部分
Selenium服务器用来接收测试程序传来的selenium命令,解释并执行,然后向测试程序反馈测试的结果
RC服务器捆绑了Selenium Core,Selenium Core是javascript程序,用于调用浏览器内置的JavaScript解释器,以解释和执行Selenese命令。它在测试程序打开浏览器时自动将其注入浏览器。
客户端库文件提供了对编程的支持,这样就可以自己设计程序来运行selenium命令,对于每一种支持的编程语言,都有不同的客户端库文件。
selenium下载:
http://www.seleniumhq.org/download/
位于”Selenium Client Driver”栏,选择使用的编程语文版本下载即可,这些包中时包含了selenium1和Selenium2的文件使用,如下图:
使用selenium1时需要下载Selenium Standalone Server:
下载完成后通过java -jar selenium-server-standalone-2.53.0.jar启动selenium服务器,如下图:
2、Selenium2
Selenium2与selenium1相比,API更容易理解和使用,可读性与维护性也大提高。selenium2完全是一套类库,不依赖于任何测试框架,不需要启动其他进程或服务。
另外,二者采用的技术方案也不同。selenium1是在浏览器中运行javascript来进行测试,而selenium2则是通过原生浏览器支持或者浏览器扩展直接控制浏览器
严格的说,它们完全属于两个不同产品而不是简单的升级关系,更像是互补关系,它们之间各有优劣,selenium2可以弥补selenium1存在的缺点(例如能够绕过JS限制、API更易使用),而Selenium1也
可以解决selenium2存在的问题(例如支持更多的浏览器).
下面是selenium1与selenium2的示例代码:
package com.my.webdriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import com.thoughtworks.selenium.DefaultSelenium;
public class Test {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
public Test() {
// TODO Auto-generated constructor stub
}
public void testselenium1() {
// 创建创建实例selenium,指定服务器的主机名或ip地址,浏览器为firefox(支持的浏览器有*safari,*chrome,*iexplorer,*opera等)
// 起始URL为百度
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://www.baidu.com");
selenium.start(); // 启动浏览器和控制界面
selenium.open("https://www.baidu.com"); // 打开指定URL
selenium.windowFocus(); // 激动窗口
selenium.windowMaximize(); // 最大化窗口
selenium.refresh(); // 刷新页面
selenium.type("id=kw", "软件测试"); // 输入指定值
selenium.click("id=su"); // 单击
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selenium.close(); // 关闭测试所使用的浏览器
selenium.stop(); // 关闭selenium控制界面
}
public void testselenium2() {
// System.setProperty("webdriver.ie.driver","F:/java/selnium/IEDriverServer.exe");
// WebDriver driver = new InternetExplorerDriver();
// 指定firefox的路径
// System.setProperty("webdriver.firefox.bin","C:/Program Files
// (x86)/Mozilla Firefox/firefox.exe");
WebDriver driver = new FirefoxDriver(); // 创建firefox对象
Navigation navigation = driver.navigate(); // selenium2的API很好理解,基本上可以按字面意思来使用
navigation.to("https://www.baidu.com");
// driver.get("https://www.baidu.com");
navigation.refresh();
driver.manage().window().maximize();
driver.navigate().back();
driver.navigate().forward();
WebElement subox = driver.findElement(By.id("kw"));
subox.sendKeys("软件测试");
WebElement subtn = driver.findElement(By.id("su"));
subtn.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.close();
}
public void testselenium2_1() {
driver = new FirefoxDriver();
baseUrl = "https://worktile.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.manage().window().maximize();
Actions action = new Actions(driver);
// 模拟鼠标停顿显示页面隐藏元素
action.moveToElement(driver.findElement(By.cssSelector("button.btn.btn-login"))).perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.linkText("登录免费版")).click();
;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private void fail(String verificationErrorString) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t1 = new Test();
t1.testselenium1();
t1.testselenium2();
t1.testselenium2_1();
}
}