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

关于去哪儿网的UI自动化测试脚本的更多相关文章

  1. 关于去哪儿网的UI自动化测试脚本(Python实现)

    UI自动化测试Qunar机票搜索场景访问Qunar机票首页http://flight.qunar.com,选择“单程”,输入出发.到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索 ...

  2. 腾讯优测优分享 | 游戏的UI自动化测试可以这样开展

    腾讯优测是专业的自动化测试平台,提供自动化测试-全面兼容性测试,云真机-远程真机租用,漏洞分析等多维度的测试服务,让测试更简单! 对于目前的两大游戏引擎cocos-2dx.unity3D,其UI自动化 ...

  3. 使用WatiN进行UI自动化测试

    Watin是一个UI自动化测试工具,支持ie/firefox,官方网站:http://watin.org/. 主要有以下特点: 支持主要的html元素,见:http://watin.org/docum ...

  4. Selenium UI自动化测试 Selenium Automatic Testing

    https://www.cnblogs.com/sunada2005/archive/2013/12/22/3486314.html UI Automatic Testing 1. 什么样的项目适合自 ...

  5. Mac下的UI自动化测试 (一)

    在我看来,实现UI自动化测试的过程一向都是令人快乐的事情,而维护它们就是跟噩梦一样了,尤其是对每次CI产生的build进行BVT测试,由于开发不会告诉你任何UI的变化,那么你拿到的测试结果就势必会一片 ...

  6. 从一次故障聊聊前端 UI 自动化测试

    背景 事件的起因在于老板最近的两次"故障",一次去年的,一次最近.共同原因都是脚手架在发布平台发布打包时出错,导致线上应用白屏不可用. 最神奇的是,事后多次 Code Review ...

  7. 使用AirtestProject+pytest做支付宝小程序UI自动化测试

    一,前言 1,背景 因公司业务需要做支付宝小程序的UI自动化测试,于是在网上查找小程序的自动化资料,发现微信小程序是有自己的测试框架的,但几乎找不到支付宝小程序UI自动化测试相关的资料.白piao失败 ...

  8. APP UI自动化测试思路总结

    python+appium自动化测试系列就要告一段落了,本篇博客咱们做个小结. 首先想要说明一下,APP自动化测试可能很多公司不用,但也是大部分自动化测试工程师.高级测试工程师岗位招聘信息上要求的,所 ...

  9. UI自动化测试(三)对页面中定位到的元素对象做相应操作

    前两天分别讲述了UI自动化测试基础以及对页面元素该如何进行定位,这一篇自然就是对定位到的页面元素对象进行相应操作啦. 阅读目录 1.常用操作元素对象的方法 2.鼠标事件操作 3.键盘事件操作 4.We ...

随机推荐

  1. BiLSTM-CRF模型中CRF层的解读

    转自: https://createmomo.github.io/ BiLSTM-CRF模型中CRF层的解读: 文章链接: 标题:CRF Layer on the Top of BiLSTM - 1  ...

  2. Axure-如何设置圆形组件

    --打开Axure软件,在[选择部件库]窗口中找到如图中所示的矩形. --将矩形拖到设计布局中 --将鼠标悬停在矩形上就能看到右上角有一个小圆点 --点击小圆点就能选择不同的形状,如:圆形

  3. java 中xml转换为json对象

    1.前提须要jar包: json-lib-2.4-jdk15.jar 和 xom-1.2.5.jar ,maven 仓库: net.sf.json-lib json-lib 2.4 jdk15 xom ...

  4. 在javascript中substr和substring的区别是什么

      1.substring 用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 substring 方法返回的子串包括 start ...

  5. 判断一个整数是否是2的n次方

    参考:http://bbs.csdn.net/topics/370058619 如题,如何判断一个整数是否是2的N次方,我能想到的方法有两个 1.一直除2,看最后是否等于1.(最笨的方法) 2.转换成 ...

  6. Sublime 格式化代码 设置快捷键以及插件使用

    实在sublime中已经自建了格式化按钮: Edit  ->  Line  ->  Reindent 只是sublime并没有给他赋予快捷键,所以只需加上快捷键即可 Preference ...

  7. DexHunter脱壳神器分析

    0x00 这篇文章我们分析Android脱壳神器DexHunter的源码. DexHunter作者也写了一篇介绍它的文章从Android执行时出发.打造我们的脱壳神器.DexHunter源码位于htt ...

  8. Android实战技巧:如何在ScrollView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  9. vsphere 5.1 改进和SSO理解

    虚拟交换器 以5.1版的vSphere而言,VMware在VDS上提供一些新功能.例如,现在可以用快照的方式,来备份还原VDS组态及网络端口群组(port group)的组态,以因应vCenter S ...

  10. Linux清理Buffer/Cache内存空间让系统变流畅

    释放缓存区内存的方法    a)清理pagecache(页面缓存) # echo 1 > /proc/sys/vm/drop_caches     或者 # sysctl -w vm.drop_ ...