fdd4bb76
zengjin55
windows
|
1
2
3
4
5
6
7
8
9
|
package com.essa.framework;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
|
60b8852f
Administrator
test
|
10
11
|
import java.sql.Connection;
import java.sql.ResultSet;
|
fdd4bb76
zengjin55
windows
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.Alert;
|
09fdf50f
Administrator
bpms
|
27
|
import org.openqa.selenium.By;
|
fdd4bb76
zengjin55
windows
|
28
|
import org.openqa.selenium.JavascriptExecutor;
|
09fdf50f
Administrator
bpms
|
29
|
import org.openqa.selenium.Keys;
|
fdd4bb76
zengjin55
windows
|
30
31
32
33
34
|
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
|
09fdf50f
Administrator
bpms
|
35
36
|
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
|
fdd4bb76
zengjin55
windows
|
37
|
import org.openqa.selenium.support.ui.Select;
|
09fdf50f
Administrator
bpms
|
38
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
fdd4bb76
zengjin55
windows
|
39
40
41
42
|
import com.essa.framework.BasePage;
import com.essa.framework.LogType;
import com.essa.framework.Logger;
|
60b8852f
Administrator
test
|
43
|
import com.mysql.cj.jdbc.PreparedStatement;
|
fdd4bb76
zengjin55
windows
|
44
45
46
47
48
49
50
|
public class BasePage {
public static WebDriver driver;
public static String pageTitle;
public static String pageUrl;
public static String OutputFileName = getDateTimeByFormat(new Date(), "yyyyMMdd_HHmmss");
|
09fdf50f
Administrator
bpms
|
51
52
53
54
|
/**
* 构造方法
* @param driver
|
fdd4bb76
zengjin55
windows
|
55
56
57
58
59
|
*/
public BasePage(WebDriver driver) {
BasePage.driver = driver;
}
|
09fdf50f
Administrator
bpms
|
60
61
62
63
|
/**
* 在文本框内输入字符
* @param element
* @param text
|
fdd4bb76
zengjin55
windows
|
64
65
66
|
*/
protected void sendKeys(WebElement element, String text) {
try {
|
09fdf50f
Administrator
bpms
|
67
|
mywait(element);
|
fdd4bb76
zengjin55
windows
|
68
69
70
71
72
73
74
75
76
77
78
79
|
if (element.isEnabled()) {
element.clear();
Logger.Output(LogType.LogTypeName.INFO, "清除文本框中已有字符:" + partialStr(element.toString(), "xpath:"));
element.sendKeys(text);
Logger.Output(LogType.LogTypeName.INFO, "输入的字符是:" + text);
}
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, element.toString()+"元素不存在");
}
}
|
09fdf50f
Administrator
bpms
|
80
|
/**
|
fdd4bb76
zengjin55
windows
|
81
|
* 点击元素,这里指点击鼠标左键
|
09fdf50f
Administrator
bpms
|
82
|
* @param element
|
fdd4bb76
zengjin55
windows
|
83
84
85
86
|
*/
protected void click(WebElement element) {
try {
|
09fdf50f
Administrator
bpms
|
87
|
mywait(element);
|
fdd4bb76
zengjin55
windows
|
88
89
90
91
92
93
94
95
96
97
|
if (element.isEnabled()) {
Logger.Output(LogType.LogTypeName.INFO, "点击元素:" + partialStr(element.toString(), "xpath:"));
element.click();
}
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
}
|
09fdf50f
Administrator
bpms
|
98
|
/**
|
fdd4bb76
zengjin55
windows
|
99
|
* 在文本输入框执行清除操作
|
09fdf50f
Administrator
bpms
|
100
|
* @param element
|
fdd4bb76
zengjin55
windows
|
101
102
|
*/
protected void clear(WebElement element) {
|
fdd4bb76
zengjin55
windows
|
103
104
105
106
107
108
109
110
111
112
113
|
try {
if (element.isEnabled()) {
element.clear();
Logger.Output(LogType.LogTypeName.INFO, "清除输入框中字符:" + partialStr(element.toString(), "xpath:") );
}
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
}
|
09fdf50f
Administrator
bpms
|
114
|
/**
|
fdd4bb76
zengjin55
windows
|
115
|
* 判断一个页面元素是否显示在当前页面
|
09fdf50f
Administrator
bpms
|
116
|
* @param element
|
fdd4bb76
zengjin55
windows
|
117
118
119
120
121
122
123
124
125
126
127
128
129
|
*/
protected void verifyElementIsPresent(WebElement element) {
try {
if (element.isDisplayed()) {
Logger.Output(LogType.LogTypeName.INFO, "元素存在:" + partialStr(element.toString(), "xpath:").trim());
}
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
}
|
09fdf50f
Administrator
bpms
|
130
|
/**
|
fdd4bb76
zengjin55
windows
|
131
|
* 获取页面的标题
|
09fdf50f
Administrator
bpms
|
132
|
* @return
|
fdd4bb76
zengjin55
windows
|
133
134
135
136
137
138
139
140
|
*/
protected String getCurrentPageTitle() {
pageTitle = driver.getTitle();
Logger.Output(LogType.LogTypeName.INFO, "当前页面的标题为:" + pageTitle);
return pageTitle;
}
|
09fdf50f
Administrator
bpms
|
141
|
/**
|
fdd4bb76
zengjin55
windows
|
142
|
* 获取页面的url
|
09fdf50f
Administrator
bpms
|
143
|
* @return
|
fdd4bb76
zengjin55
windows
|
144
|
*/
|
09fdf50f
Administrator
bpms
|
145
|
public static String getCurrentPageUrl() {
|
fdd4bb76
zengjin55
windows
|
146
147
148
149
150
|
pageUrl = driver.getCurrentUrl();
Logger.Output(LogType.LogTypeName.INFO, "当前页面的URL为:" + pageUrl);
return pageUrl;
}
|
09fdf50f
Administrator
bpms
|
151
|
/**
|
fdd4bb76
zengjin55
windows
|
152
153
154
155
156
157
158
159
160
161
|
* 处理多窗口之间切换
*/
protected void switchWindow() {
String currentWindow = driver.getWindowHandle();// 获取当前窗口句柄
Set<String> handles = driver.getWindowHandles();// 获取所有窗口句柄
Logger.Output(LogType.LogTypeName.INFO, "当前窗口数量: " + handles.size());
Iterator<String> it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
|
a62053f7
Administrator
add scene
|
162
|
driver.close();
|
fdd4bb76
zengjin55
windows
|
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
continue;
}
try {
// driver.close();// 关闭旧窗口
WebDriver window = driver.switchTo().window(it.next());// 切换到新窗口
Logger.Output(LogType.LogTypeName.INFO, "新窗口的标题为:" + window.getTitle());
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, "无法切换到新打开窗口" + e.getMessage());
}
// driver.close();//关闭当前焦点所在的窗口
}
// driver.switchTo().window(currentWindow);//回到原来页面
}
|
09fdf50f
Administrator
bpms
|
178
|
/**
|
a62053f7
Administrator
add scene
|
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
* 处理多窗口之间切换
* 将会关闭当前的窗口
*/
protected void switchMoreWindow() {
String handle = driver.getWindowHandle();
for(String temhandle : driver.getWindowHandles()) {
if (!temhandle.equals(handle)) {
driver.close();
driver.switchTo().window(temhandle);
}
}
}
/**
|
fdd4bb76
zengjin55
windows
|
193
|
* 浏览器弹框操作,true确认弹框,false取消弹框
|
09fdf50f
Administrator
bpms
|
194
|
* @param isAccept
|
fdd4bb76
zengjin55
windows
|
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
*/
protected void alert(boolean isAccept) {
Alert alert = driver.switchTo().alert();
if (isAccept) {
Logger.Output(LogType.LogTypeName.INFO, "提示框内容为:" + alert.getText());
alert.accept();
Logger.Output(LogType.LogTypeName.INFO, "确认弹框");
} else {
Logger.Output(LogType.LogTypeName.INFO, "提示框内容为:" + alert.getText());
alert.dismiss();
Logger.Output(LogType.LogTypeName.INFO, "取消弹框");
}
}
|
09fdf50f
Administrator
bpms
|
209
|
/**
|
fdd4bb76
zengjin55
windows
|
210
|
* 下拉框选择选项
|
09fdf50f
Administrator
bpms
|
211
212
213
|
* 元素必须可以使用select,input和button使用会报错
* @param element
* @param optionText
|
fdd4bb76
zengjin55
windows
|
214
215
216
217
218
219
220
|
*/
protected void selectElement(WebElement element, String optionText) {
Select select = new Select(element);
select.selectByVisibleText(optionText);
Logger.Output(LogType.LogTypeName.INFO, "选择选项:" + optionText);
}
|
09fdf50f
Administrator
bpms
|
221
222
223
224
225
226
227
228
229
230
231
|
/**
* 下拉框选择选项,通过选项中的value来定位
* @param element
* @param value
*/
protected void selectElement(WebElement element, int value) {
Select select = new Select(element);
select.selectByIndex(value);
Logger.Output(LogType.LogTypeName.INFO, "选择选项:" + value);
}
/**
|
fdd4bb76
zengjin55
windows
|
232
|
* 判断元素在页面中是否存在
|
09fdf50f
Administrator
bpms
|
233
234
|
* @param element
* @return boolean
|
fdd4bb76
zengjin55
windows
|
235
236
237
238
|
*/
protected boolean isElementExist(WebElement element) {
try {
Boolean bool = element.isDisplayed();
|
09fdf50f
Administrator
bpms
|
239
240
|
Logger.Output(LogType.LogTypeName.INFO, "检查元素是否存在:" +partialStr(element.toString(), "xpath:")+":"+ bool);
|
fdd4bb76
zengjin55
windows
|
241
242
243
244
245
246
247
|
return bool;
} catch (NoSuchElementException e) {
takeScreenShot();
Logger.Output(LogType.LogTypeName.ERROR, "无法确认当前元素是否存在:" + e.getMessage());
return false;
}
}
|
09fdf50f
Administrator
bpms
|
248
249
|
/**
|
60b8852f
Administrator
test
|
250
|
* 元素在页面上是否可见,不建议用此方法,有时会报错,找不出原因
|
09fdf50f
Administrator
bpms
|
251
252
253
254
255
256
|
* @param element
* @return boolean
*/
protected boolean isVisibility(WebElement element) {
try {
if(ExpectedConditions.visibilityOf(element) != null) {
|
60b8852f
Administrator
test
|
257
|
Logger.Output(LogType.LogTypeName.INFO, "元素在页面上可见");
|
09fdf50f
Administrator
bpms
|
258
259
260
|
return true;
}
} catch (NoSuchElementException e) {
|
60b8852f
Administrator
test
|
261
|
Logger.Output(LogType.LogTypeName.ERROR, "无法页面上是否有此元素");
|
09fdf50f
Administrator
bpms
|
262
263
|
return false;
}
|
60b8852f
Administrator
test
|
264
|
Logger.Output(LogType.LogTypeName.INFO, "元素在页面不可见");
|
09fdf50f
Administrator
bpms
|
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
return false;
}
/**
* 元素在页面上是否可见
* @param element
* @return
*/
protected boolean isVisibility(By by) {
try {
Logger.Output(LogType.LogTypeName.INFO, "检查元素在页面上是否可见");
if(ExpectedConditions.visibilityOf(driver.findElement(by)) != null) {
Logger.Output(LogType.LogTypeName.INFO, "元素可见:"+by.toString());
return true;
}
} catch (NoSuchElementException e) {
}
Logger.Output(LogType.LogTypeName.INFO, "元素不可见:"+by.toString());
return false;
}
|
fdd4bb76
zengjin55
windows
|
286
|
|
09fdf50f
Administrator
bpms
|
287
|
/**
|
fdd4bb76
zengjin55
windows
|
288
|
* 获取元素的文本值
|
09fdf50f
Administrator
bpms
|
289
|
* @param element
|
fdd4bb76
zengjin55
windows
|
290
|
*/
|
60b8852f
Administrator
test
|
291
|
protected String getText(WebElement element) {
|
fdd4bb76
zengjin55
windows
|
292
293
294
|
try {
if (element.isEnabled()) {
|
fdd4bb76
zengjin55
windows
|
295
|
Logger.Output(LogType.LogTypeName.INFO, "获取当前元素的文本值:" + element.getText());
|
60b8852f
Administrator
test
|
296
|
return element.getText();
|
fdd4bb76
zengjin55
windows
|
297
298
299
300
|
}
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
|
60b8852f
Administrator
test
|
301
|
return null;
|
fdd4bb76
zengjin55
windows
|
302
303
|
}
|
09fdf50f
Administrator
bpms
|
304
|
/**
|
fdd4bb76
zengjin55
windows
|
305
|
* js的点击操作
|
09fdf50f
Administrator
bpms
|
306
|
* @param element
|
fdd4bb76
zengjin55
windows
|
307
308
309
|
*/
protected void jsExecutorClick(WebElement element) {
try {
|
09fdf50f
Administrator
bpms
|
310
|
mywait(element);
|
fdd4bb76
zengjin55
windows
|
311
312
|
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("arguments[0].click();", element);
|
60b8852f
Administrator
test
|
313
|
Logger.Output(LogType.LogTypeName.INFO, "调用JavaScript点击元素:" + partialStr(element.toString(), "xpath:"));
|
fdd4bb76
zengjin55
windows
|
314
315
316
317
318
|
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
}
|
09fdf50f
Administrator
bpms
|
319
320
|
/**
|
fdd4bb76
zengjin55
windows
|
321
|
* js的删除操作
|
09fdf50f
Administrator
bpms
|
322
323
|
* @param webElement
* @param attribute
|
fdd4bb76
zengjin55
windows
|
324
325
326
327
328
329
330
331
332
333
334
335
|
*/
protected void jsExecutorRemoveAttribute(WebElement webElement, String attribute) {
try {
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("arguments[0].removeAttribute('" + attribute + "');", webElement);
Logger.Output(LogType.LogTypeName.INFO, "调用JavaScript删除元素属性:" + attribute);
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
}
|
09fdf50f
Administrator
bpms
|
336
|
/**
|
fdd4bb76
zengjin55
windows
|
337
|
* 获取js返回的值
|
09fdf50f
Administrator
bpms
|
338
339
|
* @param webElement
* @return
|
fdd4bb76
zengjin55
windows
|
340
|
*/
|
09fdf50f
Administrator
bpms
|
341
|
protected String jsExecutorGetAttributeValue(WebElement webElement) {
|
fdd4bb76
zengjin55
windows
|
342
343
344
345
346
347
348
349
350
351
|
try {
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
Logger.Output(LogType.LogTypeName.INFO, "调用JavaScript返回元素属性值");
return (String) jsExecutor.executeScript("return arguments[0].id;", webElement);
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
return null;
}
}
|
09fdf50f
Administrator
bpms
|
352
|
/**
|
fdd4bb76
zengjin55
windows
|
353
|
* 读取excel中的数据
|
09fdf50f
Administrator
bpms
|
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
* @param filepath excel的路径地址
* @param filename excel的文件名
* @param SheetName excel的worksheet名
* @return
* @throws Exception
*/
public static Object[][] readExcel(String filepath, String filename, String SheetName){
try {
File file = new File(filepath + "\\" + filename);
FileInputStream inputStream = new FileInputStream(file);
Workbook Workbook = null;
// 获取文件扩展名
String fileExtensionName = filename.substring(filename.indexOf("."));
Logger.Output(LogType.LogTypeName.INFO, "获取所要读取的文件");
// 判断是.xlsx还是.xls的文件并进行实例化
if (fileExtensionName.equals(".xlsx")) {
Workbook = new XSSFWorkbook(inputStream);
Logger.Output(LogType.LogTypeName.INFO, "文件为:.xlsx格式");
} else if (fileExtensionName.equals(".xls")) {
Workbook = new HSSFWorkbook(inputStream);
Logger.Output(LogType.LogTypeName.INFO, "文件为:.xls格式");
}
// 通过sheetName生成Sheet对象
Sheet Sheet = Workbook.getSheet(SheetName);
int rowCount = Sheet.getLastRowNum() - Sheet.getFirstRowNum();
List<Object[]> records = new ArrayList<Object[]>();
for (int i = 0; i < rowCount + 1; i++) {
Row row = Sheet.getRow(i);
String fields[] = new String[row.getLastCellNum()];
for (int j = 0; j < row.getLastCellNum(); j++) {
if (row.getCell(j).getCellType() == Cell.CELL_TYPE_NUMERIC) {
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
}
// 判断数据的类型
switch (row.getCell(j).getCellType()) {
case Cell.CELL_TYPE_NUMERIC: // 数字
fields[j] = String.valueOf(row.getCell(j).getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: // 字符串
fields[j] = String.valueOf(row.getCell(j).getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: // Boolean
fields[j] = String.valueOf(row.getCell(j).getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: // 公式
fields[j] = String.valueOf(row.getCell(j).getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: // 空值
fields[j] = "";
break;
case Cell.CELL_TYPE_ERROR: // 故障
fields[j] = "非法字符";
break;
default:
fields[j] = "未知类型";
break;
}
|
fdd4bb76
zengjin55
windows
|
411
|
}
|
09fdf50f
Administrator
bpms
|
412
|
records.add(fields);
|
fdd4bb76
zengjin55
windows
|
413
|
}
|
09fdf50f
Administrator
bpms
|
414
415
416
417
418
419
420
421
|
Object[][] results = new Object[records.size()][];
for (int i = 0; i < records.size(); i++) {
results[i] = records.get(i);
}
Logger.Output(LogType.LogTypeName.INFO, "读取文件成功");
return results;
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
|
fdd4bb76
zengjin55
windows
|
422
|
}
|
09fdf50f
Administrator
bpms
|
423
|
return null;
|
fdd4bb76
zengjin55
windows
|
424
425
|
}
|
09fdf50f
Administrator
bpms
|
426
|
/**
|
fdd4bb76
zengjin55
windows
|
427
|
* 上传文件
|
09fdf50f
Administrator
bpms
|
428
429
|
* @param filePath
* @throws Exception
|
fdd4bb76
zengjin55
windows
|
430
|
*/
|
09fdf50f
Administrator
bpms
|
431
432
433
434
435
436
437
438
|
protected void uploadFile(String filePath){
try {
Logger.Output(LogType.LogTypeName.INFO, "开始上传文件");
StringSelection sel = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel, null);
// 新建一个Robot类的对象
Robot robot = new Robot();
Thread.sleep(1000);
|
fdd4bb76
zengjin55
windows
|
439
|
|
09fdf50f
Administrator
bpms
|
440
441
|
// 按下回车
robot.keyPress(KeyEvent.VK_ENTER);
|
fdd4bb76
zengjin55
windows
|
442
|
|
09fdf50f
Administrator
bpms
|
443
444
|
// 释放回车
robot.keyRelease(KeyEvent.VK_ENTER);
|
fdd4bb76
zengjin55
windows
|
445
|
|
09fdf50f
Administrator
bpms
|
446
447
448
|
// 按下 CTRL+V
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
|
fdd4bb76
zengjin55
windows
|
449
|
|
09fdf50f
Administrator
bpms
|
450
451
452
453
|
// 释放 CTRL+V
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
Thread.sleep(1000);
|
fdd4bb76
zengjin55
windows
|
454
|
|
09fdf50f
Administrator
bpms
|
455
456
457
458
459
460
461
462
|
// 点击回车 Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Logger.Output(LogType.LogTypeName.INFO, "上传文件成功");
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
|
fdd4bb76
zengjin55
windows
|
463
464
|
}
|
09fdf50f
Administrator
bpms
|
465
|
/**
|
fdd4bb76
zengjin55
windows
|
466
|
* 字符串切片
|
09fdf50f
Administrator
bpms
|
467
468
469
470
471
472
|
* @param element 需要被操作的元素
* @param begin 从这个字符开始切
* @param end 到这个字符结尾
* 例子:某个元素的文本值为:广州市天河区猎德,只要“天河区”
* 第二个参数:天, 第三个参数:区
* @return
|
fdd4bb76
zengjin55
windows
|
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
*/
protected String partialStr(WebElement element, String begin, String end) {
String result_string = element.getText();
Logger.Output(LogType.LogTypeName.INFO, "获取所需切片的字符串");
// 根据词切片,取第二片字符串
String st1 = result_string.split(begin)[1];
Logger.Output(LogType.LogTypeName.INFO, "切除" + begin + "之前的字符串");
// 再切一次结尾,得到我们想要的结果
String search_need = st1.split(end)[0];
Logger.Output(LogType.LogTypeName.INFO, "切除" + end + "之后的字符串");
Logger.Output(LogType.LogTypeName.INFO, "返回切片后的字符串");
return search_need;
}
|
09fdf50f
Administrator
bpms
|
487
488
489
490
491
492
|
/**
* 复写切片,仅在本页面作为截断日志文本后面带的一堆字符串
* @param string
* @param begin
* @return
*/
|
fdd4bb76
zengjin55
windows
|
493
494
495
496
497
|
protected String partialStr(String string,String begin) {
String st1 = string.split(begin)[1];
return st1;
}
|
09fdf50f
Administrator
bpms
|
498
499
|
/**要的元素是否存在,最多5秒
* @param element
|
fdd4bb76
zengjin55
windows
|
500
|
*/
|
09fdf50f
Administrator
bpms
|
501
502
|
protected void mywait(WebElement element) {
|
a62053f7
Administrator
add scene
|
503
|
WebDriverWait wait = new WebDriverWait(driver, 7);
|
09fdf50f
Administrator
bpms
|
504
505
|
// Logger.Output(LogType.LogTypeName.INFO, "等待元素在页面上加载可见,最多5秒");
wait.until(ExpectedConditions.visibilityOf(element));
|
fdd4bb76
zengjin55
windows
|
506
507
|
}
|
09fdf50f
Administrator
bpms
|
508
|
/**
|
fdd4bb76
zengjin55
windows
|
509
|
* 设立检查点,判断页面是否是我们要的
|
09fdf50f
Administrator
bpms
|
510
511
512
|
* @param checkPoint
* @param element
* @return
|
fdd4bb76
zengjin55
windows
|
513
514
515
|
*/
protected boolean isThisPage(String checkPoint,WebElement element) {
boolean bool1=checkPoint.equals(element.getText());
|
09fdf50f
Administrator
bpms
|
516
|
Logger.Output(LogType.LogTypeName.INFO, "判断检查点是否存在:"+bool1);
|
fdd4bb76
zengjin55
windows
|
517
518
519
|
return bool1;
}
|
09fdf50f
Administrator
bpms
|
520
|
/**
|
fdd4bb76
zengjin55
windows
|
521
522
523
524
525
526
527
528
529
530
531
532
533
|
* 截图当前页面
*/
protected void takeScreenShot() {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// 拷贝截图文件到我们项目./Screenshots
FileUtils.copyFile(src, new File(".\\Log\\Screenshots\\"+OutputFileName+"截图.png"));
Logger.Output(LogType.LogTypeName.INFO, "截图当前页面成功!");
}
catch (IOException e) {
System.out.println(e.getMessage());
|
09fdf50f
Administrator
bpms
|
534
|
Logger.Output(LogType.LogTypeName.ERROR, "截图当前页面失败!");
|
fdd4bb76
zengjin55
windows
|
535
536
537
538
|
}
}
|
09fdf50f
Administrator
bpms
|
539
|
/**
|
fdd4bb76
zengjin55
windows
|
540
|
* 上下移动滚动条,这里使用js操作
|
09fdf50f
Administrator
bpms
|
541
|
* @param percent 0:最下方 100:最上方
|
fdd4bb76
zengjin55
windows
|
542
543
544
545
|
*/
protected void moveHeightScroll(String percent) {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scrollBy(0, 0-document.body.scrollHeight *"+percent+"/100)");
|
09fdf50f
Administrator
bpms
|
546
547
548
549
|
try {
Thread.sleep(1000);
} catch (Exception e) {
}
|
fdd4bb76
zengjin55
windows
|
550
551
|
Logger.Output(LogType.LogTypeName.INFO, "上下拖动滚动条");
}
|
09fdf50f
Administrator
bpms
|
552
553
554
555
556
|
/**
* 左右移动滚动条
* @param percent 0:最左 100:最右
*/
|
fdd4bb76
zengjin55
windows
|
557
558
559
560
561
562
|
protected void moveWidthScroll(String percent) {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scrollBy(0, 0-document.body.scrollWidth *"+percent+"/100)");
Logger.Output(LogType.LogTypeName.INFO, "左右拖动滚动条");
}
|
09fdf50f
Administrator
bpms
|
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
/**
* 鼠标点击
* @param element
*/
protected void actionClick(WebElement element) {
try {
Actions action = new Actions(driver);
Logger.Output(LogType.LogTypeName.INFO, "鼠标事件点击元素");
action.click(element).perform();
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, "鼠标事件点击元素失败!");
}
}
/**
* 鼠标双击
* @param element
*/
protected void actionDoubleClick(WebElement element) {
try {
Actions action = new Actions(driver);
Logger.Output(LogType.LogTypeName.INFO, "鼠标双击元素");
action.doubleClick(element).perform();
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, "鼠标双击元素失败!");
}
}
/**
* 模拟鼠标事件拖动元素
* @param element 需要拖动的元素
* @param horizontal 水平方向:正数向右,负数向左
* @param vertical 垂直方向:正数向上,负数向下
*/
protected void jsExecutorDragAndDrop(WebElement element,int horizontal,int vertical) {
try {
Actions action = new Actions(driver);
action.dragAndDropBy(element, horizontal, vertical).perform();
Logger.Output(LogType.LogTypeName.INFO, "使用鼠标拖动,将元素水平拖动"+horizontal+" 垂直拖动"+vertical);
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
}
}
/**
* 移动鼠标到指定元素
* @param element
*/
public void moveMouse(WebElement element) {
try {
Actions action = new Actions(driver);
Logger.Output(LogType.LogTypeName.INFO, "移动鼠标到指定元素上");
action.moveToElement(element).perform();
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, "移动鼠标失败了~");
}
}
/**
* 键盘回车
* @param element
*/
protected void enter(WebElement element) {
try {
Logger.Output(LogType.LogTypeName.INFO, "对元素进行键盘回车");
element.sendKeys(Keys.ENTER);
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, "键盘回车失败!");
}
}
/**
* 强行等待,有时候页面加载需要时间,检查点检测不出使用
* @param msec
*/
protected void forceWait(int msec) {
try {
Logger.Output(LogType.LogTypeName.INFO, "强行等待:"+msec/1000+"秒");
Thread.sleep(msec);
} catch (Exception e) {
Logger.Output(LogType.LogTypeName.ERROR, "强行等待失败");
}
}
|
60b8852f
Administrator
test
|
647
|
/**
|
a62053f7
Administrator
add scene
|
648
|
* 动态等待,如果元素不存在,等待一秒直到元素出现,最长7秒
|
60b8852f
Administrator
test
|
649
650
651
|
* @param by
*/
protected void dynamicWait(By by) {
|
a62053f7
Administrator
add scene
|
652
653
654
655
656
657
658
659
660
661
662
|
for(int count=0;count<7&&!(isVisibility(by));count++) {
forceWait(1000);
}
}
/**
* 与上面的方法相反,如果元素存在,等待直到元素不存在,最长7秒
* @param by
*/
protected void dynamicLoad(By by) {
for(int count=0;count<7&&isVisibility(by);count++) {
|
60b8852f
Administrator
test
|
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
forceWait(1000);
}
}
/**
* 连接数据库查询数据
* @param sql 查询sql
* @param field 需要的字段,这个方法只支持一个字段查询
* @return 返回字段值
*/
protected String DBSqlSearch(String sql,String field) {
try {
Connection conn = DButil.getCon();
PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
return rs.getString(field);
}
DButil.close(rs, pstmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
|
09fdf50f
Administrator
bpms
|
686
687
688
689
690
691
692
|
/**
* 获取当前系统时间,得到格式化时间字符串
* @param date
* @param format
* @return
*/
|
fdd4bb76
zengjin55
windows
|
693
694
695
696
697
698
699
700
|
protected static String getDateTimeByFormat(Date date, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
}
|