首页 > 代码库 > 自动化测试题目某旅游网站题目
自动化测试题目某旅游网站题目
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.Spring;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumHelper {
public static void main(String[] arg) {
// 组和100组 数据
System.setProperty(
"webdriver.chrome.driver",
"C:\\Users\\Administrator.PC-20121111FEJF\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
// C:\Users\Administrator.PC-20121111FEJF\AppData\Local\Google\Chrome\Application
List cityist = new ArrayList<String>();
cityist.add("北京");
cityist.add("上海");
cityist.add("昆明");
cityist.add("西安");
cityist.add("广州");
cityist.add("南京");
cityist.add("重庆");
cityist.add("大连");
cityist.add("青岛");
cityist.add("郑州");
cityist.add("成都");
int maxIteration = 1;
// 总验证次数
for (int index = 0; index < cityist.size(); index++) {
String sCityname = (String) cityist.get(index);
for (int indexECity = 1; indexECity < cityist.size(); indexECity++) {
String eCityname = (String) cityist.get(indexECity);
if (maxIteration > 3) {
// 最大次数,退出,防止无限制的迭代
break;
}
Date current = new Date();
Calendar c = getSpecifiedDay(7);
try {
validateWebPage(sCityname, eCityname, current, c.getTime());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
maxIteration++;
}
}
}
/**
* @param step
* @return
*/
private static Calendar getSpecifiedDay(int step) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, step);
return c;
}
/**
* @param driver
*/
private static void setSngStatus(WebDriver driver) {
WebElement sngWebElement = driver.findElement(By.id("searchTypeSng"));
sngWebElement.click();
}
// 执行规则验证
private static void validateWebPage(String sCityName, String eCityName,
Date sDate, Date eDate) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://flight.qunar.com");
setSngStatus(driver);// 保证选择单选
WebElement sinput = driver.findElement(By.name("fromCity"));
sinput.sendKeys(sCityName);
WebElement einput = driver.findElement(By.name("toCity"));
einput.sendKeys(eCityName);
WebElement sDateinput = driver.findElement(By.name("fromDate"));
sDateinput.sendKeys(getStringFromDate(sDate));
// 显示数据,将不可用的状态的控件改为可用
((JavascriptExecutor) driver)
.executeScript("$(\"#dom_arrivalDateDiv_disable\").click()");
WebElement eDateinput = driver.findElement(By.name("toDate"));
eDateinput.clear();
eDateinput.sendKeys(getStringFromDate(eDate));
WebElement submitCtl = driver.findElement(By.className("btn_txt"));
submitCtl.submit();// 发送请求
Thread.sleep(1000);// 页面等待1分钟
List<WebElement> feeEveryParts = driver.findElements(By
.cssSelector("span.highlight"));
if (feeEveryParts == null) {
WebElement emptyMark = driver
.findElement(By.className("e_fly_lst"));
Assert.assertEquals(emptyMark == null, "没有找到提示信息");
return;
}
WebElement feeEveryPart = driver.findElement(By
.cssSelector("span.highlight"));
WebElement lineAClick = driver
.findElement(By.className(".b_avt_lst a"));
lineAClick.click();
// 分析结果
if (feeEveryPart != null) {
// 此时,有AB2端
List<WebElement> elements = driver.findElements(By
.cssSelector(".e_qvt_route h3"));
Boolean first = elements.get(0).getText().contains("第一");
Assert.assertEquals(first, "提示信息错误");
Boolean sencond = elements.get(1).getText().contains("第二");
Assert.assertEquals(sencond, "提示信息错误");
} else {
// 没有航费
// 有查查询列表
WebElement priceRange = driver.findElement(By
.cssSelector("qvt_col_more"));
if (priceRange != null) {
String contente = priceRange.getText();
Assert.assertEquals(contente.contains("报价范围"), "发生错误");
}
}
WebElement result = (WebElement) ((JavascriptExecutor) driver)
.executeScript("var query= $(\".e_qvt_hd\"); return query;");
Assert.assertEquals(result == null, "发生错误");
driver.quit();
}
/**
* @param eDate
* @return
*/
@SuppressWarnings("deprecation")
private static String getStringFromDate(Date eDate) {
return DateFormat.getDateInstance(DateFormat.DEFAULT).format(eDate);
}
// 获取随机数
private static int getSpecifiedRandom(int inputNo) {
if (inputNo <= 0)
inputNo = 10;
Random random = new Random();
return random.nextInt(inputNo);
}
}
不行了,实在困了。先写到这吧,欢迎大家批评啊。算是抛砖引玉,代码的实现不好啊,架构啊,方法名什么的都没考虑啊。以后在优化吧。