Selenium ide及webDriver使用

官方网站:http://seleniumhq.org/
Selenium-IDE于firefox上脚本测试。并可以通过该插件生成对应Java webDriver适用的Junit测试代码。

下面是根据Firefox上录制产生的Junit代码,部分找HTML的ELEMENT时候代码需要自己手动更改。
View Code
 1 package com.example.tests;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 import org.junit.*;
 5 import static org.junit.Assert.*;
 6 import org.openqa.selenium.*;
 7 import org.openqa.selenium.firefox.FirefoxDriver;
 8 import org.openqa.selenium.support.ui.Select;
 9 
10 public class TestDemo {
11     private WebDriver driver;
12     private String baseUrl;
13     private StringBuffer verificationErrors = new StringBuffer();
14     @Before
15     public void setUp() throws Exception {
16 //        driver = new HtmlUnitDriver();
17         driver = new FirefoxDriver();
18         baseUrl = "http://localhost:8080/";
19         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
20     }
21 
22     @Test
23     public void test() throws Exception {
24         driver.get(baseUrl + "/pcloud-web/serve/login.do");
25         driver.findElement(By.id("userId")).clear();
26         driver.findElement(By.id("userId")).sendKeys("hello@163.com");
27         driver.findElement(By.id("fullName")).clear();
28         driver.findElement(By.id("fullName")).sendKeys("hello@163.com");
29         driver.findElement(By.id("mobile")).clear();
30         driver.findElement(By.id("mobile")).sendKeys("13932132121");
31         driver.findElement(By.id("submit")).click();
32         driver.findElement(By.id("phone")).clear();
33         driver.findElement(By.id("phone")).sendKeys("13932132121");
34         driver.findElement(By.id("phone-check")).click();
35         driver.findElement(By.id("phone-check")).clear();
36         driver.findElement(By.id("phone-check")).sendKeys("13932132121");
37         new Select(driver.findElement(By.id("face-select"))).selectByVisibleText("10元");
38         Thread.sleep(1000);
39         //开始直接购买
40         driver.findElement(By.id("direct-charge")).click();
41         Thread.sleep(5000);
42         for (String handle : driver.getWindowHandles()) {
43             driver.switchTo().window(handle);
44         }
45         driver.findElement(By.xpath("(//a[contains(text(),'我的订单')])[2]")).click();
46         Thread.sleep(1000);
47         try {
48             assertTrue(driver.findElement(By.cssSelector("td[class='status']")).getText().equals("成功"));
49         } catch (Error e) {
50             verificationErrors.append(e.toString());
51         }
52     }
53 
54     @After
55     public void tearDown() throws Exception {
56         driver.quit();
57         String verificationErrorString = verificationErrors.toString();
58         if (!"".equals(verificationErrorString)) {
59             fail(verificationErrorString);
60         }
61     }
62 
63     private boolean isElementPresent(By by) {
64         try {
65             driver.findElement(by);
66             return true;
67         } catch (NoSuchElementException e) {
68             return false;
69         }
70     }
71 }

 

 
posted on 2012-12-19 11:52  ThinkFar  阅读(247)  评论(0编辑  收藏  举报