常用操作均在下面的有测试用例有体现。
1 package CRM;
2
3 import static org.testng.Assert.assertEquals;
4
5 import java.awt.Checkbox;
6 import java.sql.Driver;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.concurrent.Delayed;
11
12 import org.openqa.selenium.By;
13 import org.openqa.selenium.JavascriptExecutor;
14 import org.openqa.selenium.WebDriver;
15 import org.openqa.selenium.WebElement;
16 import org.openqa.selenium.firefox.FirefoxDriver;
17 import org.openqa.selenium.interactions.Actions;
18 import org.openqa.selenium.support.ui.Select;
19 import org.openqa.selenium.support.ui.Sleeper;
20 import org.testng.annotations.Test;
21
22
23
24 public class NewTest {
25 public static boolean a;
26 public static WebDriver dr;
27 //登录的静态方法
28 public static boolean login(String name,String pwd) throws InterruptedException {
29 dr=new FirefoxDriver();
30 dr.get("http://crm.cloud.5kcrm.com/");
31 dr.findElement(By.name("name")).clear();
32 dr.findElement(By.name("password")).clear();
33 dr.findElement(By.name("name")).sendKeys(name);
34 dr.findElement(By.name("password")).sendKeys(pwd);
35 dr.findElement(By.name("submit")).click();
36 Thread.sleep(5000);
37
38 try {
39 dr.findElement(By.id("menu_home"));
40 a=true;
41 } catch (Exception e) {
42 a=false;
43 }
44 return a;
45 }
46
47
48 //登录密码错误的登录,调用登录方法
49 @Test
50 public void 登录密码错误() throws InterruptedException {
51 boolean a=login("boss1", "123456");
52 dr.quit();
53 assertEquals(a, false);
54 }
55
56
57 //登录正确账号密码的登录,调用登录方法
58 @Test
59 public void 正确登录() throws InterruptedException {
60 boolean a=login("boss", "123456");
61 dr.quit();
62 assertEquals(a, true);
63 }
64
65
66 //学习 不同的定位方法
67 @Test
68 public void 组织构架() throws InterruptedException{
69 boolean a=login("boss", "123456");
70 Actions action=new Actions(dr);
71 WebElement webe=dr.findElement(By.xpath("//a[contains(text(),'办')]"));
72 webe.click();
73 dr.findElement(By.linkText("组织架构")).click();
74 Thread.sleep(4000);
75 WebElement w1=dr.findElement(By.xpath(".//*[@id='browser']/li[1]/span/a"));
76 Actions a1=new Actions(dr);
77 a1.moveToElement(w1).perform();
78 dr.findElement(By.xpath(".//*[@id='popOverBox']/input[2]")).click();
79 dr.findElement(By.xpath("html/body/div[9]/div[2]/form/div[1]/div/input")).sendKeys("测试部门");
80 dr.findElement(By.cssSelector(".controls>textarea[name='description']")).sendKeys("测试测试");
81 dr.findElement(By.xpath("html/body/div[9]/div[3]/div/button[1]")).click();
82 }
83
84
85 //选择类、切换和切回frame框架、
86 @Test
87 public void 测试训练() throws InterruptedException{
88 boolean a=login("boss", "123456");
89 dr.manage().window().maximize();
90 dr.get("http://crm.cloud.5kcrm.com/index.php?m=task&a=add");
91 dr.findElement(By.id("subject")).sendKeys("测试任务");
92 WebElement d1=dr.findElement(By.name("message_alert"));
93
94 if (d1.getAttribute("checked").equals("true")){
95 d1.click();
96 }
97 dr.findElement(By.name("owner_name")).click();
98 Thread.sleep(1300);
99 dr.findElement(By.xpath(".//*[@id='dialog-message7']/form/ul/li[2]/a")).click();
100 dr.findElement(By.xpath(".//*[@id='ta2']/label")).click();
101 dr.findElement(By.xpath("html/body/div[8]/div[3]/div/button[1]")).click();
102 dr.findElement(By.id("due_date")).sendKeys("2017-07-29 15:57");
103 WebElement status=dr.findElement(By.name("status"));
104 Select slstatus = new Select(status);
105 slstatus.selectByVisibleText("进行中");
106 dr.switchTo().frame("ueditor_0");
107 dr.findElement(By.xpath("//body")).sendKeys("测试内容填写");
108 dr.switchTo().defaultContent();
109 dr.findElement(By.xpath("html/body/div[6]/div/div/div[3]"
110 + "/div/form/table/tfoot/tr/td[2]/input[1]")).click();
111 Thread.sleep(3000);
112 boolean a1=false;
113 try {
114 dr.findElement(By.linkText("测试任务"));
115 a1=true;
116
117 } catch (Exception e) {
118
119 }
120
121 //测试list中查找需要的元素
122 WebElement tg1=null;
123 List<WebElement> tagnames= dr.findElements(By.tagName("input"));
124 for (WebElement tg:tagnames){
125 if(tg.getAttribute("name").equals("password")){
126 tg1=tg;
127 break;
128 }
129
130 }
131 System.out.println(tg1.getTagName().toString());
132
133 assertEquals(a1,true);
134 dr.quit();
135 }
136
137
138 //切换window页面
139 @Test
140 public void 切换window() throws Exception{
141 boolean a=login("boss", "123456");
142 Thread.sleep(4000);
143 WebElement a1=dr.findElement(By.linkText("客户管理"));
144 Actions a2=new Actions(dr);
145 a2.clickAndHold(a1).perform();
146 Thread.sleep(1000);
147 dr.findElement(By.xpath("html/body/div[2]/div/div/div[2]/ul[1]/li[1]/ul/li[9]/a")).click();
148 //1、获得当前的句柄
149 String hd=dr.getWindowHandle();
150 System.out.println(hd.toString());
151 //2、获得所有的句柄
152 Set<String> hds=dr.getWindowHandles();
153 System.out.println(hds.toString());
154 String h1=null;
155 //3、切换句柄
156 for(String h:hds){
157 if(!h.equals(hd)){
158 h1=h;
159 break;
160 }
161 }
162
163 dr.switchTo().window(h1);
164 dr.get("http://baidu.com");
165 dr.switchTo().window(hd);
166 dr.get("http://so.com");
167 }
168 }