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. SciPy 安装不上?

    参考:链接:https://www.zhihu.com/question/30188492/answer/150928275来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...

  2. jquery 文字滚动大全 scroll 支持文字或图片 单行滚动 多行滚动 带按钮控制滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. javascript链式语法

    因为 jQuery 库的缘故,链式语法在前端界变得非常流行.实际上这是一种非常容易实现的模式.基本上,你只需要让每个函数返回 'this',这样其他函数就可以立即被调用.看看下面的例子. var bi ...

  4. 【Linux】好玩的Linux命令(二)

    关于Linux talk:http://man.linuxde.net/talk 下面文章转自:http://www.oschina.net/translate/11-lesser-known-use ...

  5. 微信小程序 - radio/checkbox自定义组件

    更新 2019-01-26:首次发布 2019-01-27:增加默认取值选中radio/checkbox,checkbox需在onload取值 2019-01-28:增加radio取值不存在红色提示和 ...

  6. linux2.6.30.4内核移植(2)——Nand Flash驱动移植

    内核源码:linux2.6.30.4 交叉编译工具:3.4.5 移植linux内核至:TQ2440 工作基础:http://www.cnblogs.com/nufangrensheng/p/36696 ...

  7. Configutation读取properties文件信息

    commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件.XML文件.JNDI资源.来自JDBC Da ...

  8. Spring面试题一

    目录一.Spring工作原理 二.为什么要用Spring三.请你谈谈SSH整合四.介绍一下Spring的事务管理五.什么是依赖注入,依赖注入的作用是什么? 六.什么是AOP,AOP的作用是什么? 七. ...

  9. js 数组 转为树形结构

    题目: source = [{ id: 1, pid: 0, name: 'body' }, { id: 2, pid: 1, name: 'title' }, { id: 3, pid: 2, na ...

  10. React-Native视频组件react-native-video使用(安卓版)

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7366019.html 一:安装 在IDE中打开你的项目,然后在IDE的terminal,输入: npm i - ...