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