Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2067028
  • 博文数量: 103
  • 博客积分: 206
  • 博客等级: 入伍新兵
  • 技术积分: 1819
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-12 10:24
个人简介

效字当先,以质为本。

文章分类
文章存档

2019年(2)

2018年(4)

2017年(7)

2016年(3)

2015年(14)

2014年(33)

2013年(31)

2012年(9)

分类: Java

2015-08-04 22:19:24

题目:请尝试写出以下自动化脚本:
Qunar机票搜索场景
访问Qunar机票首页,选择“单程”,输入出发、到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索列表页。
在列表页停留1分钟,至到页面上出现“搜索结束”。
如果出现航班列表,对于出现“每段航班均需缴纳税费”的行随机点选“订票”按钮.
代码:

点击(此处)折叠或打开

  1. package week5;

  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.List;

  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.firefox.FirefoxDriver;
  10. import org.openqa.selenium.support.ui.ExpectedCondition;
  11. import org.openqa.selenium.support.ui.WebDriverWait;

  12. public class Day52 {
  13.     public WebDriver driver;

  14.     public void startfirefox(){
  15.         driver= new FirefoxDriver();
  16.         driver.manage().window().maximize();
  17.         driver.get("/");
  18.     }
  19.     public void endfirefox(){
  20.         driver.close();        
  21.     }
  22.     public void inputfromandto(String fromcity,String tocity){
  23.         //选择单程
  24.         driver.findElement(By.id("searchTypeSng")).click();
  25.         //输入出发站
  26.         WebElement weblementElement = driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']"));
  27.         weblementElement.clear();
  28.         weblementElement.sendKeys(fromcity);
  29.         try {
  30.             Thread.sleep(3000);
  31.         } catch (InterruptedException e1) {
  32.             e1.printStackTrace();
  33.         }    
  34.         //输入终点站
  35.         WebElement webElement = driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='toCity']"));
  36.         webElement.clear();
  37.         webElement.sendKeys(tocity);
  38.         try {
  39.             Thread.sleep(3000);
  40.         } catch (InterruptedException e1) {
  41.             e1.printStackTrace();
  42.         }
  43.         //输入当前日期的七天后日期
  44.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  45.         Calendar rightNow = Calendar.getInstance();
  46.         rightNow.add(Calendar.DAY_OF_YEAR, 7);
  47.         Date dt1 = rightNow.getTime();
  48.         String reStr = formatter.format(dt1);
  49.         WebElement webelemt = driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromDate']"));
  50.         webelemt.clear();
  51.         webelemt.sendKeys(reStr);
  52. //        webelemt.click();
  53.         //点击搜索
  54.         WebElement element = driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//button[text()='搜 索']"));
  55.         element.click();        
  56.         //分析搜索结果
  57.         boolean wait = false;
  58.         try {
  59.             wait = new WebDriverWait(driver, 50)
  60.                     .until(new ExpectedCondition<Boolean>() {
  61.                         WebElement webelement;

  62.                         public Boolean apply(WebDriver driver) {
  63.                             webelement = driver.findElement(By.xpath(".//*[@id='progTip']/span"));
  64.                             System.out.println(webelement.getText());
  65.                             return webelement.getText().contains("搜索结束");
  66.                         }
  67.                     });
  68.         } catch (Exception e) {
  69.             System.out.println("没有出搜索结束页面");
  70.         }    
  71.         System.out.println(wait);
  72.         if (wait){
  73.             try {
  74.                 driver.findElement(By.xpath("//*[contains(@id,'itemBar')]//div//span[@class='highlight']/parent::p")).getText().contains("每段航班均需缴纳税费");
  75.             } catch (Exception e) {
  76.                 System.out.println("不存在每段航班均需缴纳税费");
  77.             }
  78.             //点击订票
  79.             List<WebElement> element1 = driver.findElements(By.xpath("//*[contains(@id,'itemBar')]//div//span[@class='highlight']/following::div[@class='a_booking']//span/b"));
  80.             int order = (int) Math.round(Math.random() * (element1.size() - 1));
  81.             System.out.println(order);
  82.             element1.get(order).click();
  83.         }else{
  84.             System.out.println("搜索出问题,没达到搜索页面");
  85.         }    
  86.     }
  87.     public static void main(String[] args) {
  88.         Day52 day52 =new Day52();
  89.         day52.startfirefox();
  90.         day52.inputfromandto("银川","南阳");
  91.         day52.endfirefox();

  92.     }

  93. }

阅读(2435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~