[自动化测试] 去哪儿机票搜索

Qunar机票搜索场景,原文在http://www.cnblogs.com/tobecrazy/p/4752684.html。

现在Qunar的页面有所变更,原文的代码已不能执行。本文修改示例代码后,实现步骤如下:
在Qunar机票首页选择“单程”,输入出发、到达城市,选择7天后的日期,点搜索,跳到机票单程搜索列表页,点击需要转机的航班显示转机信息。每次点击或者输入时,都先调用WebDriverWait判断是否已加载完毕。

实现代码如下:

JAVA代码



import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class Qunar {
	// 引用原文代码
	public static String getDateAfterToday(int dateAfterToday) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, +dateAfterToday);
		System.out.println(cal.getTime().toString());
		Date date = cal.getTime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(df.format(date));
		return df.format(date);
	}

	// 修改后代码
	@Test
	public void searchTicket() {
		System.setProperty("webdriver.chrome.driver", "C:\\work\\selenium\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("http://flight.qunar.com/");
		driver.manage().window().maximize();
		// 出发城市
		WebElement from_inpox = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(
				driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromCity']"))));
		from_inpox.clear();
		from_inpox.sendKeys("FOC");
		WebElement sour = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
				By.xpath("//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'福州')]")));
		sour.click();

		// 选择单程
		WebElement sigleWayCheckBox = driver.findElement(
				By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@class='inp_chk js-searchtype-oneway']"));
		if (!sigleWayCheckBox.isSelected()) {
			sigleWayCheckBox.click();
		}

		// 到达城市
		WebElement to_inpox = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(
				driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='toCity']"))));
		to_inpox.clear();
		to_inpox.sendKeys("NKG");
		WebElement dest = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
				By.xpath("//div[@class='qcbox-fixed js-suggestcontainer']//td[contains(text(),'南京')]")));
		dest.click();

		// 出发日期
		WebElement from_date = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(
				driver.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//input[@name='fromDate']"))));
		from_date.clear();
		from_date.sendKeys(getDateAfterToday(7));

//		WebElement search = driver
//				.findElement(By.xpath("//div[@id='js_flighttype_tab_domestic']//button[@class='btn_search']"));
//
//		search.submit();
		from_date.submit();

		(new WebDriverWait(driver, 10))
				.until(ExpectedConditions
						.presenceOfElementLocated(By.xpath("//div[@class='content']//div[contains(text(),'停留')]")))
				.click();
	}
}

###pom.xml

       .......
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.8.1</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.9.8</version>
			<exclusions>
				<exclusion>
					<artifactId>junit</artifactId>
					<groupId>junit</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		......
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值