Selenium 是一个用于Web应用程序测试的工具,支持浏览器有 ie、firefox、Safari、Google等。
学习selenium自动化具备知识点:
1,语言知识掌握方面:java、C#、Ruby、Python等一种即可;
2,了解 xPath ,非必须,可以通过firefox ide工具 获取路径。可以安装firebug 、 firepath插件可以搞定路径问题。
xpath:语法:http://www.cnblogs.com/jianjialin/archive/2009/02/01/1382056.htm
selenium2.44最新jar包、src源码等下载链接:http://download.csdn.net/detail/u012874998/8435449#comment
javadoc api链接: http://selenium.googlecode.com/git/docs/api/java/index.html
selenium ide:下载链接:http://download.csdn.net/detail/u012874998/8212601
firebug下载路径:http://download.csdn.net/detail/u012874998/8442313
搭建开发环境:eclipse软件、java jdk、安装firefox浏览器及相关插件
1,将 selenium 下载包解压。 libs中的jar包及selenium-java-2.44.0.jar 加到新建的java项目构建路径即可。
测试代码_java语言、firefox浏览器:
public class HelloSelenium {
public static void main(String[] args) throws InterruptedException, ParseException{
//window 环境
//System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
// System.setProperty(“webdriver.ie.driver”, “C:\Program Files (x86)\Internet Explorer\iexplore.exe”);
// System.setProperty(“webdriver.chrome.driver”, “C:\Program Files (x86)\Google\Chrome\Application\chrome”);
//iMac 环境
// System.setProperty(“webdriver.firefox.bin”, “/Applications/Firefox.app/Contents/MacOS/firefox”);
// System.setProperty(“webdriver.chrome.driver”, “/Applications/Google Chrome.app/Contents/MacOS/Google Chrome”);//存在一定的问题
// System.setProperty(“webdriver.safari.bin”, “/Applications/Safari.app/Contents/Safari”);
WebDriver driver=new FirefoxDriver();
Navigation navigation=driver.navigate();
test8(navigation,driver);
System.out.println("close...");
Thread.sleep(2000);
driver.close();
}
/**
* @throws InterruptedException
* method: to 、forward、refresh
*/
public static void test1(Navigation navigation,WebDriver driver) throws InterruptedException{
navigation.to("http://www.baidu.com");
navigation.to("http://www.360.cn");
Thread.sleep(2000);
navigation.back();
Thread.sleep(2000);
navigation.forward();
System.out.println("Refresh。。。");
Thread.sleep(3000);
navigation.refresh();
}
/**
* method: sendKeys、clear、submit、id、name、linkText:链接文本查找、partialLinkText:模糊查找、(className、tagName:对象隐藏时不怎么好用)、xpath、
*/
public static void test2(Navigation navigation,WebDriver driver){
navigation.to("http://www.baidu.com");
// WebElement baiduBox=driver.findElement(By.id(“kw”));
// WebElement baiduBox=driver.findElement(By.name(“wd”));
// baiduBox.sendKeys(“selenium”);
// WebElement baiduBox=driver.findElement(By.linkText(“登录”));
// WebElement baiduBox=driver.findElement(By.partialLinkText(“登”));
// WebElement baiduBox=driver.findElement(By.className(“mnav”));//新闻
// WebElement baiduBox=driver.findElement(By.tagName(“a”));
WebElement baiduBox=driver.findElement(By.xpath(“.//*[@id=’kw’]”));
baiduBox.sendKeys(“selenium”);
}
/**
* 遍历 select 选项内容
*/
public static void test3(Navigation navigation,WebDriver driver){
navigation.to(“http://www.baidu.com“);
navigation.to(“http://tieba.baidu.com/“);
driver.findElement(By.xpath(“.//*[@id=’tb_header_search_form’]/a”)).click();
WebElement select= driver.findElement(By.name(“sm”));
String targetText=”按时间顺序”; //按时间顺序/按相关性排序
List options=select.findElements(By.tagName(“option”)); //获取列表集合
for(int i=0;i