入住 CI 中国社区 登录
CodeIgniter 中国开发者社区 返回首页

rosemarry的个人空间 https://codeigniter.org.cn/forums/?39678 [收藏] [复制] [分享] [RSS]

日志

转载“Selenium实例---12306网站测试”,主要是看看给日期控件赋值 ...

已有 917 次阅读2015-10-10 16:31 |个人分类:Selenium| 网站测试


[java] view plaincopyprint?
<pre class="java" name="code">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);     
        </pre><pre class="java" name="code"><span style="white-space: pre;">        </span>//单击查询按钮 
        wd.findElement(By.id("submitQuery")).click(); 
         
         
        //关闭浏览器 
        wd.close(); 
    } 
}</pre> 
<pre></pre> 
<pre></pre> 
     
        <div style="padding-top: 20px;">          
            <p style="font-size: 12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。

 
        </div> 
--------------------------------------------------------------------------转载出处------------------------------------------------------------------------

http://blog.csdn.net/xc5683/article/details/9629827


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 入住 CI 中国社区