关于去哪儿网的UI自动化测试脚本

本文介绍如何使用Maven创建Java工程,并引入Selenium框架,实现自动化测试Qunar机票搜索场景,包括随机验证100个城市对的3个月内的任意搜索条件。

UI自动化测试
Qunar机票搜索场景
访问Qunar机票首页http://flight.qunar.com,选择“单程”,输入出发、到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索列表页。
在列表页停留1分钟,至到页面上出现“搜索结束”。
如果出现航班列表,对于出现“每段航班均需缴纳税费”的行随机点选“订票”按钮,在展开的列表中会出现“第一程”、 “第二程”;对于没有出现“每段航班均需缴纳税费”的行随机点选“订票”按钮,在展开的列表底部中会出现“报价范围”
如果不出现航班列表,则页面会出现“该航线当前无可售航班”
请使用maven创建java工程,引入Selenium框架,编写WebUI代码,实现上述人工操作和验证。要求能随机验证100个城市对的3个月内的任意搜索条件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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 );
     }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值