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


   

你可能感兴趣的:(Selenium实例----12306网站测试)