Selenium实例----12306网站测试

package com.beyondtest;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.HasInputDevices;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestOrder {

	WebDriver wd;
	
	@Test
	public void test() throws InterruptedException{
		//设置firefox浏览器
		wd = new FirefoxDriver();
		
		//打开12306主页
		wd.get("http://www.12306.cn/");
		
		//点击购票/预约按钮
		Thread.sleep(1000);
		wd.findElement(By.cssSelector("img[alt=\"购票\"]")).click();
		
		//输入用户名、密码,等待手工输入验证码
		Thread.sleep(1000);
		wd.switchTo().frame("iframepage");
		wd.switchTo().frame("main");
		wd.findElement(By.id("UserName")).sendKeys("username");
		wd.findElement(By.id("password")).sendKeys("password");
		Thread.sleep(10000);
		
		wd.findElement(By.id("subLink")).click();
		
		//进入订票界面,点击车票预定按钮
		Thread.sleep(2000);
		wd.findElement(By.partialLinkText("车票预订")).click();
		
		//输入出发地和目的地
		//出发地无法直接输入,需要借助键盘的上下键和回车键
		Thread.sleep(1000);
		wd.findElement(By.id("fromStationText")).clear();
		wd.findElement(By.id("fromStationText")).sendKeys("北京");
		((HasInputDevices) wd).getKeyboard().sendKeys(Keys.ARROW_DOWN);
		((HasInputDevices) wd).getKeyboard().sendKeys(Keys.ARROW_DOWN);
		//注意这里不能用Keys.ENTER
		((HasInputDevices) wd).getKeyboard().sendKeys(Keys.RETURN);
		
		Thread.sleep(1000);
		wd.findElement(By.id("toStationText")).clear();
		wd.findElement(By.id("toStationText")).sendKeys("沈阳");
		((HasInputDevices) wd).getKeyboard().sendKeys(Keys.RETURN);
		
		//输入出发日期
		//现在这个地方不可以直接输入了,可以通过js来输入
		String str = "document.getElementById(\"startdatepicker\").readonly=false";
		String strDate = "document.getElementById(\"startdatepicker\").value=\"2013-08-02\"";
		((JavascriptExecutor)wd).executeScript(str);
		((JavascriptExecutor)wd).executeScript(strDate);	
		
		//单击查询按钮
		wd.findElement(By.id("submitQuery")).click();
		
		
		//关闭浏览器
		wd.close();
	}
}


  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值