セレン元のJava utilのを使用する超簡単なソフトウェアテストとしての神

以下は、ツールの独自のパッケージですseleniumJava

プロジェクトで使用するコピーへの無料

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
      
      
輸入 java.awt.AWTException。
輸入 java.awt.Robot。
輸入 java.awt.event.KeyEventの。
インポート のjava.io.File;
インポート にjava.io.IOException;
輸入 java.text.SimpleDateFormatの。
輸入 java.util.Date;
輸入 org.openqa.selenium.JavascriptExecutor。
輸入 org.openqa.selenium.TakesScreenshot。
輸入 org.apache.commons.io.FileUtils。
輸入 org.openqa.selenium.By;
輸入 org.openqa.selenium.OutputType。
輸入 org.openqa.selenium.WebDriver;
輸入 org.openqa.selenium.WebElement。
輸入 org.openqa.selenium.chrome.ChromeDriver;
輸入 org.openqa.selenium.firefox.FirefoxDriver;
輸入 org.openqa.selenium.htmlunit.HtmlUnitDriver;
輸入 org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class {
private static WebDriver webDriver;
private static WebElement webElement;
private static Robot robot;
private static By byelement;
public void Script ( String script , WebElement element ) {
JavascriptExecutor js=(JavascriptExecutor)webDriver;
if ( element== null ) {
js.executeScript(script);
} else {
js.executeScript(script, element);
}
}
/*javascript 返回数据*/
public String ScriptString ( String script ) {
JavascriptExecutor js=(JavascriptExecutor)webDriver;
String s=( String) js.executeScript(script);
return s;
}
/**
* 初始化
* @param select
* 1:Firefox
* 2:IE
* 3:Chrome
* 4:后台执行
* @param browserpath
* 选择本地浏览路径
*/
public void Init ( int switchbrowser , String browserpath ) {
switch ( switchbrowser ) {
case 1:
System.setProperty ( "webdriver.firefox.bin" , browserpath );
webDriver = new FirefoxDriver();
webDriver.manage().window().maximize();
break;
case 2:
System.setProperty( "webdriver.ie.driver", browserpath);
webDriver = new InternetExplorerDriver();
webDriver.manage().window().maximize();
break;
case 3:
System.setProperty( "webdriver.chrome.driver", browserpath);
webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
break;
case 4:
webDriver = new HtmlUnitDriver();
break;
}
}
//复制
protected void CopyKeys() {
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_C);
} catch (AWTException e) {
e.printStackTrace();
}
}
//粘贴
protected void PasteKeys() {
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
* 拖拽
* @param by 要多拽的元素
* @param target 目的地
*/
protected void DragAndDrop ( By by , By target ) {
( new Actions(webDriver)).dragAndDrop(webDriver.findElement(by), webDriver.findElement(target)).perform();
}
//传入值
protected void SendKeys(By by, String value){
webDriver.findElement(by).sendKeys(value);
}
/**
* 截图
* @param outpath 导出截图后的路径 文件名是以当前时间
*/
public void GetScreen ( String outpath ) {
File screenShotFile = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShotFile, new File ( outpath + "/" + getSysTime() + ".jpg" ) );
} catch (IOException e) {
e.printStackTrace();
}
}
/**获取元素
* @param by
* @param second 查找需要时间
*/
public void Wait( final By by , int second ) {
WebDriverWait wait = new WebDriverWait(webDriver,second);
wait.until( new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver d) {
return d.findElement(by);
}
});
}
//打开浏览器
public void Start ( String url ) {
webDriver.get( url );
}
//结束
public void Stop () {
webDriver.quit();
}
//获取当前时间
public static String getSysTime(){
SimpleDateFormat filename = new SimpleDateFormat( "yyyyMMdd_HHmmss");
return filename.format( new Date());
}
// 睡眠
public void Sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static WebDriver getWebDriver() {
return webDriver;
}
パブリック静的ボイド setWebDriver(webdriverをwebdriverを){
SeleniumUtil.webDriver = webdriverを。
}
パブリック静的 WebElement getWebElement(){
返す webElementを。
}
パブリック静的ボイド setWebElement(WebElement webElement){
SeleniumUtil.webElement = webElement。
}
パブリック静的 getByelementにより(){
返す byelement;
}
パブリック静的ボイド(byelementにより)setByelement {
SeleniumUtil.byelement = byelement。
}
}

オリジナル:大きな柱  の神のように使用する超簡単なソフトウェアテストのセレン元のJava utilに


おすすめ

転載: www.cnblogs.com/chinatrump/p/11588877.html