selenium测试(Java)-- 一组元素操作(十一)

利用下面的例子来编写测试脚本

页面代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
 5 <title>CheckBox</title>
 6 <link
 7     href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
 8     rel="stylesheet" />
 9 <script
10     src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
11 </head>
12 <body>
13     <h3>checkbox</h3>
14     <div class="well">
15         <form class="form-horizontal">
16             <div class="control-group">
17                 <label class="control-lable" for="c1">checkbox1</label>
18                 <div class="controls">
19                     <input type="checkbox" id="c1" />
20                 </div>
21             </div>
22             <div class="control-group">
23                 <label class="control-lable" for="c2">checkbox2</label>
24                 <div class="controls">
25                     <input type="checkbox" id="c2" />
26                 </div>
27             </div>
28             <div class="control-group">
29                 <label class="control-lable" for="c3">checkbox3</label>
30                 <div class="controls">
31                     <input type="checkbox" id="c3" />
32                 </div>
33             </div>
34         </form>
35     </div>
36 </body>
37 </html>

 

脚本:

 

 1 package com.test.findelement;
 2 
 3 import java.util.Iterator;
 4 import java.util.List;
 5 
 6 import org.openqa.selenium.By;
 7 import org.openqa.selenium.WebDriver;
 8 import org.openqa.selenium.WebElement;
 9 import org.openqa.selenium.firefox.FirefoxDriver;
10 
11 public class CheckBox {
12 
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         
16         WebDriver driver = new FirefoxDriver();
17         driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/findelement/checkbox.html");
18         driver.manage().window().maximize();
19         
20         List<WebElement> WebElements = driver.findElements(By.cssSelector("[type=checkbox]"));
21         
22         Iterator iterator = WebElements.iterator();
23         
24         //全部勾选
25         while(iterator.hasNext()){
26             WebElement wb = (WebElement)iterator.next();
27             wb.click();
28         }
29         //去掉第一个
30         waitTime(3000);
31         driver.findElements(By.cssSelector("[type=checkbox]")).get(0).click();
32         
33         //去掉最后一个
34         waitTime(3000);
35         driver.findElements(By.cssSelector("[type=checkbox]")).get(WebElements.size()-1).click();
36         
37         
38         waitTime(5000);
39         driver.quit();
40     }
41     
42     static public void waitTime(int time) {
43 
44         try {
45             Thread.sleep(time);
46         } catch (InterruptedException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50     }
51 
52 }

 

posted @ 2016-07-14 21:11  月色深潭  阅读(1690)  评论(0编辑  收藏  举报