关于去哪儿网的UI自动化测试脚本
来源:互联网 发布:linux classpath 编辑:程序博客网 时间:2024/05/17 03:04
UI自动化测试
Qunar机票搜索场景
访问Qunar机票首页http://flight.qunar.com,选择“单程”,输入出发、到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索列表页。
在列表页停留1分钟,至到页面上出现“搜索结束”。
如果出现航班列表,对于出现“每段航班均需缴纳税费”的行随机点选“订票”按钮,在展开的列表中会出现“第一程”、 “第二程”;对于没有出现“每段航班均需缴纳税费”的行随机点选“订票”按钮,在展开的列表底部中会出现“报价范围”
如果不出现航班列表,则页面会出现“该航线当前无可售航班”
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;<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); } }
0 0
- 关于去哪儿网的UI自动化测试脚本
- 关于去哪儿网的UI自动化测试脚本
- 关于去哪儿网的UI自动化测试脚本
- webdriver UI自动化测试执行JS脚本
- Selenium Webdriver 编写UI自动化测试脚本
- 关于UI自动化技术及测试的一些看法
- 《去哪儿网》使用的糟糕体验
- 失控的UI自动化测试
- 去哪儿网前端
- 基于webdriver的web UI自动化测试框架(系统架构+测试脚本应用架构)
- 基于webdriver的web UI自动化测试框架(系统架构+测试脚本应用架构)
- 自动化测试-----IDS的测试脚本
- 去哪儿测试工程师笔试面试总结
- 2、Selenium + Python 实现 UI 自动化测试-第一个自动化测试脚本
- 自动化测试-小公司,UI设计想法,欢迎大家留言,一起讨论,有最好的办法去实现
- 去哪儿网面试总结
- 去哪儿网面试问题
- 去哪儿网笔试题
- [iOS]iOS的字号转换问题
- HTML5转义字符
- SQLite中Update语句
- 架构知识点体系
- 如何使用Application Loder 上传app
- 关于去哪儿网的UI自动化测试脚本
- 解除 wordpress 上传文件大小限制的最新方法
- ORACLE数据库迁移中文乱码问题
- Android spinner点击相同选项处理无法响应事件问题,暴力反射
- 我在csdn的第一个博客
- 手机对话中的语音处理(一)
- PCL—综述—三维图像处理
- AutoCAD界面移植到VB.net应用程序窗体中
- Android代码模拟按下Home键