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;<br> } 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); } }