UI自動化測試
Qunar機票搜索場景
訪問Qunar機票首頁http://flight.qunar.com,選擇“單程”,輸入出發、到達城市,選擇today+7日后的日期,點“搜索”,跳轉到機票單程搜索列表頁。
在列表頁停留1分鍾,至到頁面上出現“搜索結束”。
如果出現航班列表,對於出現“每段航班均需繳納稅費”的行隨機點選“訂票”按鈕,在展開的列表中會出現“第一程”、 “第二程”;對於沒有出現“每段航班均需繳納稅費”的行隨機點選“訂票”按鈕,在展開的列表底部中會出現“報價范圍”
如果不出現航班列表,則頁面會出現“該航線當前無可售航班”
請使用maven創建java工程,引入Selenium框架,編寫WebUI代碼,實現上述人工操作和驗證。要求能隨機驗證100個城市對的3個月內的任意搜索條件。
package com.test;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Demo3 {
public WebDriver driver;
private int waitTime = 20;
private Date fromDate;
private SimpleDateFormat sdf;
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@AfterClass
public void tearDown() {
driver.close();
driver.quit();
}
private WebElement getElement(final By by) {
boolean flag = new WebDriverWait(driver, waitTime)
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(by).isDisplayed();
}
});
WebElement element = null;
if (flag) {
element = driver.findElement(by);
}
return element;
}
private WebElement getElementNotWait(final By by) {
WebElement element = null;
try {
element = driver.findElement(by);
} catch (Exception e) {
element = null;
}
return element;
}
private String getFromDate() {
fromDate = new Date();
sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(fromDate);
}
private String getToDate() {
Calendar c = Calendar.getInstance();
c.setTime(fromDate);
c.add(Calendar.DAY_OF_MONTH, +7);
return sdf.format(c.getTime());
}
private ArrayList<WebElement> getAllResultElement() {
int labelIndex = 1;
int rowIndex = 1;
ArrayList<WebElement> list = new ArrayList<WebElement>();
while (true) {
if (this.getElementNotWait(By.xpath("//div[@class='outContainer']/div[" + labelIndex + "]")) != null) {
if (this.getElementNotWait(By.xpath("//div[@class='outContainer']/div[" + labelIndex + "]/div[starts-with(@id,'itemRowXI')][" + rowIndex + "]")) != null) {
list.add(this.getElementNotWait(By.xpath("//div[@class='outContainer']/div["+ labelIndex + "]/div[starts-with(@id,'itemRowXI')][" + rowIndex + "]")));
rowIndex++;
} else{
labelIndex++;
rowIndex = 1;
}
} else
break;
}
return list;
}
private int getRandom(int count) {
return (int) Math.round(Math.random() * (count - 1));
}
private void sleep(int s){
try {
Thread.sleep(s*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void process() {
driver.navigate().to("http://flight.qunar.com/");
this.getElement(By.id("searchTypeSng")).click();
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']")).clear();
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']")).sendKeys("武漢");
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='toCity']")).clear();
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='toCity']")).sendKeys("北京");
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromDate']")).clear();
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromDate']")).sendKeys(this.getFromDate());
this.getElementNotWait(By.xpath("//div[@id='dom_arrivalDateDiv_disable']//div[@class='sicon']")).click();
JavascriptExecutor j =(JavascriptExecutor)driver;
j.executeScript("$('input[name=toDate]').val('"+this.getToDate()+"')");
this.getElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//button[text()='搜 索']")).click();
this.sleep(10);
this.getElement(By.xpath("//div[@class='outContainer']"));
ArrayList<WebElement> all = this.getAllResultElement();
int random = this.getRandom(all.size());
WebElement element = all.get(random);
String id = element.getAttribute("id");
String lindId = id.replace("itemRow", "btnBook");
this.getElement(By.xpath("//a[@id='"+lindId+"']")).click();
this.sleep(10);
}
}
