├── .gitignore
├── .idea
├── compiler.xml
├── encodings.xml
├── misc.xml
├── vcs.xml
└── workspace.xml
├── .svn
├── entries
├── format
├── pristine
│ ├── 18
│ │ └── 18faef162d97c2e0b772c9b0d11172df30acd690.svn-base
│ ├── 20
│ │ └── 203e1c6476440e563d9bb5e0d1c59cc9454478b5.svn-base
│ ├── 28
│ │ └── 28a11a34935d3de501afdb8314438c69737b1b31.svn-base
│ ├── 29
│ │ └── 29e8aacdb0282ce0d0fd3368b535db3b2c1a1027.svn-base
│ ├── 38
│ │ └── 380b8d095b3980e176da752b7eca173058a762b7.svn-base
│ ├── 60
│ │ └── 60882905053439eec83b3bc874cf140e16d22b7e.svn-base
│ ├── 65
│ │ └── 65b3036b7309fc047130744241d34602ae911613.svn-base
│ ├── 66
│ │ └── 66c70257fbf22ca1e4519e1b146ff82f47e20af6.svn-base
│ ├── 67
│ │ └── 67b062a832ff8188fe40893a4f8cccb5a0821c13.svn-base
│ ├── 83
│ │ └── 838ac2106539e0b32d37ff350e9a7d5478833207.svn-base
│ ├── 84
│ │ └── 84145a3a5fbf680c16a271129f98a9a7041d08bf.svn-base
│ ├── 94
│ │ └── 940996d965a99b8a6b600ca24cb8ec2ef74b10dd.svn-base
│ ├── 01
│ │ └── 01a2a7517781eb884a5e7900f8dcfb0a61789bee.svn-base
│ ├── 1c
│ │ └── 1ccd915970357909ec81455110530858850babb3.svn-base
│ ├── 1f
│ │ └── 1f560bc82955efe93b15a29a61b4872dbc3aed80.svn-base
│ ├── 3f
│ │ └── 3f8f0ab2f5634316def326d1efc27934515e6480.svn-base
│ ├── 4d
│ │ └── 4d443d4a562d7d4f23dafb110ee1257e668ab0b4.svn-base
│ ├── 5c
│ │ └── 5c912473203b1302769766f01995c4bef0a24d80.svn-base
│ ├── 5d
│ │ └── 5d807e123b97bcfd6e16771905a76d76bade132e.svn-base
│ ├── 6b
│ │ └── 6bbbf124c9c9a6caf456f0df2ec5fa55639416ae.svn-base
│ ├── 7f
│ │ └── 7f9d84f01368bd9f33d406c64ae76c70fdad002e.svn-base
│ ├── 8f
│ │ ├── 8f7506d20d579dbeda5d3cd62db649b84d69c961.svn-base
│ │ └── 8fdd2101f6e9b2fc4c41b18f63ca94c1c93e4ce4.svn-base
│ ├── 9d
│ │ └── 9de366c4246ffc7578e203aa9fb99f5b38781661.svn-base
│ ├── a5
│ │ └── a51bfc999ffbc07359cd443a934da89b163dae45.svn-base
│ ├── a8
│ │ └── a8af6ae39b2179aeed950a40d7df795d1a263bd0.svn-base
│ ├── ac
│ │ └── acac4bcb9d33444fa3e9c91a7d4e98aa685c2033.svn-base
│ ├── b4
│ │ └── b47ece37f00bf63f04b228baa1cca94819ad0408.svn-base
│ ├── bb
│ │ └── bb20cf436382a1316d10edbc08cbfeafd087cbaf.svn-base
│ ├── c5
│ │ └── c5bb68f65547523040fd19739bce5e1ad1ade8b6.svn-base
│ ├── ca
│ │ └── ca4a388e93650475ba06ad6249329a2fbdf7cfa7.svn-base
│ ├── d5
│ │ └── d51628ab14729fce40821a679393daabb65b0d94.svn-base
│ ├── d6
│ │ └── d6cb6f7ef9de23237578c0582ef9cb814586629e.svn-base
│ ├── d9
│ │ └── d9223b38eaf5869c9831601ac1562db0b500af85.svn-base
│ ├── da
│ │ └── da6229555f7228870e249b188775d676d98932d3.svn-base
│ ├── ec
│ │ └── ec4b3e407c1ecf21c4f15eed9f8184763a12070e.svn-base
│ ├── ed
│ │ └── ed7fc595f121fbd89379f646df5a99a67d6df009.svn-base
│ ├── f4
│ │ └── f464f4075902a9440cbf8b237f16b8e4478101ec.svn-base
│ ├── f5
│ │ └── f52c0830806dc60385832348873806aa34e6ae0c.svn-base
│ └── ff
│ │ └── ff78cbf17ebb5c81e511c4657e133a3cf92e10a6.svn-base
├── wc.db
└── wc.db-journal
├── README.md
├── env.properties
├── pom.xml
├── resource
├── UpLoadRoomPic.au3
├── UpLoadRoomPic.exe
├── chromedriver.exe
└── 自动化测试用例.xlsx
├── src
└── test
│ └── java
│ ├── cases
│ ├── BaiduTest.java
│ └── SogouTest.java
│ ├── common
│ ├── BaseTest.java
│ ├── InitDriver.java
│ ├── OperateElement.java
│ └── TestngListener.java
│ ├── pageobject
│ ├── BaiduPO.java
│ └── SogouPO.java
│ └── utils
│ ├── AutoITUtil.java
│ ├── DBUtil.java
│ ├── ExcelData.java
│ ├── ExcelXlsData.java
│ ├── FileOperate.java
│ ├── IdCardGeneration.java
│ ├── Log4jTest.java
│ ├── ReadCsvFile.java
│ ├── ReadProperties.java
│ ├── Retry.java
│ ├── RetryListener.java
│ ├── SikuliUtil.java
│ ├── SuperRandom.java
│ ├── TimeUtil.java
│ └── XmlUtils.java
└── testng.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 | true
64 | DEFINITION_ORDER
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 | C:\Users\lingshu\AppData\Roaming\Subversion
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 | 1560405367668
223 |
224 |
225 | 1560405367668
226 |
227 |
228 |
229 | 1560405701037
230 |
231 |
232 |
233 | 1560405701037
234 |
235 |
236 | 1560406678533
237 |
238 |
239 |
240 | 1560406678533
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 | 1.8
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
--------------------------------------------------------------------------------
/.svn/entries:
--------------------------------------------------------------------------------
1 | 12
2 |
--------------------------------------------------------------------------------
/.svn/format:
--------------------------------------------------------------------------------
1 | 12
2 |
--------------------------------------------------------------------------------
/.svn/pristine/01/01a2a7517781eb884a5e7900f8dcfb0a61789bee.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Calendar;
5 | import java.util.Date;
6 |
7 | /**
8 | * Author: 灵枢
9 | * Date: 2018/11/15
10 | * Time: 14:44
11 | * Description:获取时间
12 | */
13 | public class TimeUtil {
14 |
15 | /**
16 | * 获取当前时间的月日时分
17 | * @return
18 | */
19 | public static String getDate(String dateFormat){
20 | Date date = new Date();
21 | SimpleDateFormat format = new SimpleDateFormat(dateFormat);
22 | return format.format(date);
23 | }
24 |
25 | /**
26 | * 获取三年后的今天
27 | * @return
28 | */
29 | public static String getTheDayAfter3Years(){
30 | Calendar calendar = Calendar.getInstance();
31 | calendar.setTime(new Date());
32 | calendar.add(Calendar.YEAR,3);
33 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
34 | String time = dateFormat.format(calendar.getTime());
35 | return time;
36 | }
37 | public static void main(String[] args) {
38 | /*Date date = new Date();
39 | SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");//设置当前时间的格式,为年-月-日 时-分-秒
40 | System.out.println(dateFormat_min.format(date));*/
41 | System.out.println(getTheDayAfter3Years());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/.svn/pristine/18/18faef162d97c2e0b772c9b0d11172df30acd690.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * Author: 灵枢
7 | * Date: 2019/1/16
8 | * Time: 15:50
9 | * Description: 文件操作
10 | */
11 | public class FileOperate {
12 |
13 | /**
14 | * 读取某个目录下的全部文件名
15 | * @param filePath
16 | */
17 | public void getFileList(String filePath){
18 | File file = new File(filePath);
19 | File[] fileList = file.listFiles();
20 | for(int i=0;i testsToBeRemoved = new ArrayList();
75 | // collect all id's from passed test
76 | Set passedTestIds = new HashSet();
77 | for (ITestResult passedTest : testContext.getPassedTests()
78 | .getAllResults()) {
79 | // logger.info("PassedTests = " + passedTest.getName());
80 | passedTestIds.add(getId(passedTest));
81 | }
82 |
83 | Set failedTestIds = new HashSet();
84 | for (ITestResult failedTest : testContext.getFailedTests()
85 | .getAllResults()) {
86 | // logger.info("failedTest = " + failedTest.getName());
87 | int failedTestId = getId(failedTest);
88 |
89 | // if we saw this test as a failed test before we mark as to be
90 | // deleted
91 | // or delete this failed test if there is at least one passed
92 | // version
93 | if (failedTestIds.contains(failedTestId)
94 | || passedTestIds.contains(failedTestId)) {
95 | testsToBeRemoved.add(failedTest);
96 | } else {
97 | failedTestIds.add(failedTestId);
98 | }
99 | }
100 |
101 | // finally delete all tests that are marked
102 | for (Iterator iterator = testContext.getFailedTests()
103 | .getAllResults().iterator(); iterator.hasNext();) {
104 | ITestResult testResult = iterator.next();
105 | if (testsToBeRemoved.contains(testResult)) {
106 | // logger.info("Remove repeat Fail Test: " + testResult.getName());
107 | iterator.remove();
108 | }
109 | }
110 |
111 | }
112 |
113 | private int getId(ITestResult result) {
114 | int id = result.getTestClass().getName().hashCode();
115 | id = id + result.getMethod().getMethodName().hashCode();
116 | id = id
117 | + (result.getParameters() != null ? Arrays.hashCode(result
118 | .getParameters()) : 0);
119 | return id;
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/.svn/pristine/1f/1f560bc82955efe93b15a29a61b4872dbc3aed80.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import io.qameta.allure.Attachment;
4 | import org.openqa.selenium.OutputType;
5 | import org.openqa.selenium.TakesScreenshot;
6 | import org.openqa.selenium.WebDriver;
7 | import org.testng.ITestContext;
8 | import org.testng.ITestResult;
9 | import org.testng.Reporter;
10 | import org.testng.TestListenerAdapter;
11 | import test.java.utils.ExcelData;
12 |
13 | import java.io.IOException;
14 | import java.util.*;
15 |
16 | public class TestngListener extends TestListenerAdapter {
17 |
18 | public void onTestFailure(ITestResult tr) {
19 |
20 | super.onTestFailure(tr);
21 | try {
22 | BaseTest bt = (BaseTest) tr.getInstance();
23 | //获取webdriver实例
24 | WebDriver driver = bt.getDriver();
25 | takeScreenShot(driver);
26 | logCaseStep(tr);
27 | logExceptedResult(tr);
28 | } catch (Exception e) {
29 | e.printStackTrace();
30 | }
31 | }
32 |
33 |
34 | /**
35 | * 获取用例失败截图
36 | * @param driver
37 | * @throws IOException
38 | */
39 | @Attachment(value = "失败截图",type = "image/png")
40 | public byte[] takeScreenShot(WebDriver driver){
41 | byte[] screenshotAs = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
42 | return screenshotAs;
43 | }
44 |
45 | /**
46 | * 测试步骤
47 | * @param tr
48 | */
49 | @Attachment(value = "操作步骤")
50 | public String logCaseStep(ITestResult tr){
51 | String filePath = "resource/自动化测试用例.xlsx";
52 | ExcelData sheet1 = new ExcelData(filePath, "搜索");
53 | return sheet1.getCellByCaseName(tr.getName(), 4, 6);
54 | }
55 |
56 | /**
57 | * 期望结果
58 | * @param tr
59 | */
60 | @Attachment(value = "期望结果")
61 | public String logExceptedResult(ITestResult tr){
62 | String filePath = "resource/自动化测试用例.xlsx";
63 | ExcelData sheet1 = new ExcelData(filePath, "搜索");
64 | return sheet1.getCellByCaseName(tr.getName(), 4, 7);
65 | }
66 |
67 |
68 | //------------------------以下是失败用例重跑相关代码----------------------------------
69 | public void onFinish(ITestContext testContext) {
70 | super.onFinish(testContext);
71 |
72 | // List of test results which we will delete later
73 | ArrayList testsToBeRemoved = new ArrayList();
74 | // collect all id's from passed test
75 | Set passedTestIds = new HashSet();
76 | for (ITestResult passedTest : testContext.getPassedTests()
77 | .getAllResults()) {
78 | // logger.info("PassedTests = " + passedTest.getName());
79 | passedTestIds.add(getId(passedTest));
80 | }
81 |
82 | Set failedTestIds = new HashSet();
83 | for (ITestResult failedTest : testContext.getFailedTests()
84 | .getAllResults()) {
85 | // logger.info("failedTest = " + failedTest.getName());
86 | int failedTestId = getId(failedTest);
87 |
88 | // if we saw this test as a failed test before we mark as to be
89 | // deleted
90 | // or delete this failed test if there is at least one passed
91 | // version
92 | if (failedTestIds.contains(failedTestId)
93 | || passedTestIds.contains(failedTestId)) {
94 | testsToBeRemoved.add(failedTest);
95 | } else {
96 | failedTestIds.add(failedTestId);
97 | }
98 | }
99 |
100 | // finally delete all tests that are marked
101 | for (Iterator iterator = testContext.getFailedTests()
102 | .getAllResults().iterator(); iterator.hasNext();) {
103 | ITestResult testResult = iterator.next();
104 | if (testsToBeRemoved.contains(testResult)) {
105 | // logger.info("Remove repeat Fail Test: " + testResult.getName());
106 | iterator.remove();
107 | }
108 | }
109 |
110 | }
111 |
112 | private int getId(ITestResult result) {
113 | int id = result.getTestClass().getName().hashCode();
114 | id = id + result.getMethod().getMethodName().hashCode();
115 | id = id
116 | + (result.getParameters() != null ? Arrays.hashCode(result
117 | .getParameters()) : 0);
118 | return id;
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/.svn/pristine/20/203e1c6476440e563d9bb5e0d1c59cc9454478b5.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.cases;
2 |
3 | import org.openqa.selenium.By;
4 | import org.testng.Assert;
5 | import org.testng.annotations.BeforeMethod;
6 | import org.testng.annotations.Test;
7 | import test.java.common.BaseTest;
8 | import test.java.common.OperateElement;
9 | import test.java.pageobject.BaiduPO;
10 |
11 | /**
12 | * Author: 灵枢
13 | * Date: 2018/12/4
14 | * Time: 16:07
15 | * Description:
16 | */
17 |
18 | public class BaiduTest extends BaseTest {
19 |
20 | private String baiduUrl;
21 |
22 | @BeforeMethod
23 | public void beforeMethod(){
24 | baiduUrl = envProperties.getProperty("baidu.url");
25 | }
26 |
27 | @Test
28 | public void test百度搜索(){
29 | String keywords = envProperties.getProperty("keywords");
30 | driver.get(baiduUrl);
31 | BaiduPO.search(keywords);
32 | BaiduPO.searchResult(1).click();
33 | //切换到第二个窗口
34 | OperateElement.switchToWindowByIndex(1);
35 | //获取博客页的titleName
36 | String titleName = OperateElement.waitByXpath("//*[@class='lt_title']").getText().trim();
37 | //验证titleName是否正确
38 | Assert.assertTrue(titleName.contains("灵枢"),"title不对,显示的是:"+titleName);
39 | }
40 |
41 | @Test
42 | public void test百度登录(){
43 | //读取配置文件中的账号密码
44 | String username = envProperties.getProperty("baidu.username");
45 | String password = envProperties.getProperty("baidu.password");
46 | //回到第一个窗口
47 | OperateElement.closeWindowHandleNotFirst();
48 | driver.get(baiduUrl);
49 | OperateElement.clickByLinkText("登录");
50 | BaiduPO.login(username,password);
51 | //验证用户名是否存在
52 | // boolean exsit = OperateElement.isElementExsit(OperateElement.waitByClassName("user-name"));
53 | boolean exsit = OperateElement.isElementExsitBy(By.className("user-name"));
54 | Assert.assertTrue(exsit);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/.svn/pristine/28/28a11a34935d3de501afdb8314438c69737b1b31.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.openqa.selenium.WebDriver;
4 | import test.java.common.OperateElement;
5 |
6 | import java.io.IOException;
7 |
8 | /**
9 | * Author: 灵枢
10 | * Date: 2019/1/16
11 | * Time: 13:58
12 | * Description:AutoIT方法封装
13 | */
14 | public class AutoITUtil {
15 | /**
16 | * 使用AutoIT上传图片
17 | * @param fileName 文件名
18 | * @param driver
19 | */
20 | public static void uploadPic(String fileName,WebDriver driver){
21 | String path = "resource/"+fileName;
22 | Runtime runtime = Runtime.getRuntime();
23 | try {
24 | runtime.exec(path);
25 | } catch (IOException e) {
26 | e.printStackTrace();
27 | }
28 | OperateElement.threadSleep(1500);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/.svn/pristine/29/29e8aacdb0282ce0d0fd3368b535db3b2c1a1027.svn-base:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/.svn/pristine/29/29e8aacdb0282ce0d0fd3368b535db3b2c1a1027.svn-base
--------------------------------------------------------------------------------
/.svn/pristine/38/380b8d095b3980e176da752b7eca173058a762b7.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import org.openqa.selenium.*;
4 | import org.openqa.selenium.interactions.Actions;
5 | import org.openqa.selenium.support.ui.ExpectedConditions;
6 | import org.openqa.selenium.support.ui.WebDriverWait;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Iterator;
10 | import java.util.List;
11 | import java.util.Set;
12 |
13 | import static org.testng.AssertJUnit.assertTrue;
14 |
15 | /*
16 | *作者:灵枢
17 | *时间:2018-11-22 下午4:45:15
18 | *描述:封装元素的基本操作:等待、点击、输入
19 | **/
20 |
21 |
22 | public class OperateElement {
23 | private static int timeOutInSeconds = 10;
24 | public static WebDriver driver;
25 |
26 | /**
27 | * 通过元素的Xpath,等待元素的出现,返回此元素
28 | * @return 返回等待的元素
29 | */
30 | public static WebElement waitByXpath(String xpath){
31 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
32 | //presenceOfElementLocated:显示等待元素在页面中存在,不用等页面全部加载完
33 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
34 | return targetElement;
35 | }
36 |
37 | /**
38 | * 通过元素的name,等待元素的出现,返回此元素
39 | * @param name 元素的name
40 | * @return 返回等待的元素
41 | */
42 | public static WebElement waitByName(String name){
43 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
44 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.name(name)));
45 | return targetElement;
46 | }
47 |
48 | /**
49 | * 通过元素的id,等待元素的出现,返回此元素
50 | * @param id 元素的id
51 | * @return 返回等待的元素
52 | */
53 | public static WebElement waitById(String id){
54 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
55 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
56 | return targetElement;
57 | }
58 |
59 | /**
60 | * 通过元素的linkText,等待元素的出现,返回此元素
61 | * @param linkText 元素的linkText
62 | * @return 返回等待的元素
63 | */
64 | public static WebElement waitByLinkText(String linkText){
65 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
66 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(linkText)));
67 | return targetElement;
68 | }
69 |
70 | /**
71 | * 通过元素的className,等待元素的出现,返回此元素
72 | * @param className 元素的className
73 | * @return 返回等待的元素
74 | */
75 | public static WebElement waitByClassName(String className){
76 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
77 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.className(className)));
78 | return targetElement;
79 | }
80 |
81 | /**
82 | * 通过元素的Css,等待元素的出现,返回此元素
83 | * @return 返回等待的元素
84 | */
85 | public static WebElement waitByCss(String css){
86 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
87 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(css)));
88 | return targetElement;
89 | }
90 |
91 | /**
92 | * 通过元素的某个属性,等待元素的出现,返回此元素
93 | * @param attribute 属性名
94 | * @param value 属性值
95 | * @return
96 | */
97 | public static WebElement waitByAttribute(String attribute,String value){
98 | String xpath = "//*[@"+attribute+"='"+value+"']";
99 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
100 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
101 | return targetElement;
102 | }
103 |
104 | /**
105 | * 通过元素的className,等待元素列表的出现,返回此元素列表
106 | * @param className 元素的className
107 | * @return 返回等待的元素
108 | */
109 | public static List waitElesByClassName(String className){
110 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
111 | List targetElementList = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.className(className))));
112 | return targetElementList;
113 | }
114 |
115 | /**
116 | * 通过元素的linktext,等待元素列表的出现,返回此元素列表
117 | * @param linkText 元素的linktext
118 | * @return 返回等待的元素
119 | */
120 | public static List waitElesByLinkText(String linkText){
121 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
122 | List targetElementList = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.linkText(linkText))));
123 | return targetElementList ;
124 | }
125 |
126 | /**
127 | * 通过元素的xpath,等待元素列表的出现,返回此元素列表
128 | * @param xpath
129 | * @return
130 | */
131 | public static List waitElesByXpath(String xpath){
132 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
133 | List targetElementList = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.xpath(xpath))));
134 | return targetElementList ;
135 | }
136 |
137 | /**
138 | * 通过元素的linkText,等待元素的出现,并点击它
139 | * @param linkText
140 | */
141 | public static void clickByLinkText(String linkText){
142 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
143 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.linkText(linkText)));
144 | targetElement.click();
145 | }
146 |
147 | /**
148 | * 通过元素的Xpath,等待元素的出现,并点击它
149 | * @param xpath
150 | */
151 | public static void clickByXpath(String xpath) {
152 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
153 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
154 | targetElement.click();
155 | }
156 |
157 | /**
158 | * 通过元素的Xpath,等待元素的出现,并点击它
159 | * @param css
160 | */
161 | public static void clickByCss(String css){
162 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
163 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(css)));
164 | targetElement.click();
165 | }
166 |
167 |
168 | /**
169 | * 通过元素的ID,等待元素的出现,并点击它
170 | * @param Id
171 | */
172 | public static void clickById(String Id){
173 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
174 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.id(Id)));
175 | targetElement.click();
176 | }
177 |
178 | /**
179 | * 通过元素的ClassName,等待元素的出现,并点击它
180 | * @param className
181 | */
182 | public static void clickByClassName(String className) {
183 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
184 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.className(className)));
185 | targetElement.click();
186 | }
187 |
188 | /**
189 | * 通过元素的Name,等待元素的出现,并点击它
190 | * @param name
191 | */
192 | public static void clickByName(String name) {
193 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
194 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.name(name)));
195 | targetElement.click();
196 | }
197 |
198 | /**
199 | * 通过元素的某个特定属性,等待元素的出现,并点击它
200 | * @param attribute
201 | * @param value
202 | * @return
203 | */
204 | public static void clickByAttribute(String attribute,String value){
205 | String xpath = "//*[@"+attribute+"='"+value+"']";
206 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
207 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
208 | targetElement.click();
209 | }
210 |
211 | /**
212 | * 通过元素的Xpath,等待文本框的出现,并输入
213 | */
214 | public static void sendKeysByXpath(String xpath,String keys) {
215 | WebElement targetElement = waitByXpath(xpath);
216 | targetElement.clear();
217 | targetElement.sendKeys(keys);
218 | }
219 |
220 | /**
221 | * 指定元素,再输入
222 | * @param element
223 | * @param keys
224 | */
225 | public static void sendKeysByEle(WebElement element,String keys){
226 | element.clear();
227 | element.sendKeys(keys);
228 | }
229 |
230 | /**
231 | * 通过元素的ID,等待文本框的出现,并输入
232 | */
233 | public static void sendKeysById(String Id,String keys) {
234 | WebElement targetElement = waitById(Id);
235 | targetElement.clear();
236 | targetElement.sendKeys(keys);
237 | }
238 |
239 | /**
240 | * 通过元素的name,等待文本框的出现,并输入
241 | */
242 | public static void sendKeysByName(String name,String keys) {
243 | WebElement targetElement = waitByName(name);
244 | targetElement.clear();
245 | targetElement.sendKeys(keys);
246 | }
247 |
248 | /**
249 | * 通过元素的ClassName,等待文本框的出现,并输入
250 | */
251 | public static void sendKeysByClassName(String className,String keys) {
252 | WebElement targetElement = waitByClassName(className);
253 | targetElement.clear();
254 | targetElement.sendKeys(keys);
255 | }
256 |
257 | /**
258 | * 通过元素的Name,等待文本框的出现,并输入
259 | */
260 | public static void SendKeysByName(String name,String keys){
261 | WebElement targetElement = waitByName(name);
262 | targetElement.clear();
263 | targetElement.sendKeys(keys);
264 | }
265 |
266 |
267 | /**
268 | * 等待元素可见
269 | * @param ele
270 | * @param waitTime
271 | */
272 | public static void waitEleVisible(WebElement ele,int waitTime){
273 | WebDriverWait wait = new WebDriverWait(driver,waitTime);
274 | wait.until(ExpectedConditions.visibilityOf(ele));
275 | }
276 |
277 | /**
278 | * 等待元素可点击
279 | * @param ele
280 | * @param waitTime
281 | */
282 | public static void waitEleCanClickable(WebElement ele,int waitTime){
283 | WebDriverWait wait = new WebDriverWait(driver,waitTime);
284 | wait.until(ExpectedConditions.visibilityOf(ele));
285 | wait.until(ExpectedConditions.elementToBeClickable(ele));
286 | }
287 |
288 | /**
289 | * 拖动元素
290 | * @param element 元素
291 | * @param xOffset 拖到目标的横坐标位置,xOffset为正数时向右移动,为负数时向左移动
292 | * @param yOffset 拖到目标的纵坐标位置
293 | */
294 | public static void dragAndDropBy(WebElement element,int xOffset,int yOffset){
295 | Actions actions = new Actions(driver);
296 | actions.dragAndDropBy(element,xOffset,yOffset).build().perform();
297 | }
298 |
299 | /**
300 | * 拖动滚动条到目标元素的位置
301 | */
302 | public static void scrollIntoView(WebElement target){
303 | ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",target);
304 | }
305 |
306 | /**
307 | * 通过点击一个元素来切换窗口
308 | * @param element 被点击的元素
309 | */
310 | public static void switchToWindowByClick(WebElement element){
311 | String currentWindow = driver.getWindowHandle();
312 | element.click();
313 | threadSleep(1000);
314 | Set windowHandles = driver.getWindowHandles();
315 | //切换窗口
316 | for (String windowHandle:windowHandles){
317 | if(!windowHandle.equals(currentWindow)){
318 | driver.switchTo().window(windowHandle);
319 | }
320 | }
321 | }
322 |
323 | /**
324 | * 通过窗口的索引来切换窗口
325 | * @param index
326 | */
327 | public static void switchToWindowByIndex(int index){
328 | try {
329 | List windowHandles = new ArrayList(driver.getWindowHandles());
330 | driver.switchTo().window((String)windowHandles.get(index));
331 | } catch (IndexOutOfBoundsException var3) {
332 | var3.printStackTrace();
333 | }
334 | }
335 |
336 | /**
337 | * 通过窗口名称来切换窗口
338 | * @param windowTitleName 窗口名称
339 | */
340 | public static void switchToWindowHandle(String windowTitleName){
341 | Set handles = driver.getWindowHandles();
342 | List it = new ArrayList(handles);
343 | Iterator iterator = handles.iterator();
344 | String currentHandle=null;
345 | try{
346 | currentHandle=driver.getWindowHandle();
347 | }
348 | catch(NoSuchWindowException e){
349 | driver.switchTo().window(it.get(0));
350 | }
351 | while (iterator.hasNext()) {
352 | String h = (String) iterator.next();
353 | String currentTitle=driver.getTitle();
354 | if (currentTitle.contains(windowTitleName)){
355 | break;
356 | }
357 | if (h != currentHandle) {
358 | if (driver.switchTo().window(h).getTitle().contains(windowTitleName)) {
359 | driver.switchTo().window(h);
360 | System.out.println("switch to "+driver.getTitle()+" news page successfully");
361 | break;
362 | }
363 | }
364 | }
365 | }
366 |
367 | /**
368 | * 关闭除了第一个窗口外的其它窗口
369 | */
370 | public static void closeWindowHandleNotFirst(){
371 | Set windowHandles = driver.getWindowHandles();
372 | if (windowHandles.size()>1){
373 | for (int i=windowHandles.size()-1;i>0;i--){
374 | driver.close();
375 | switchToWindowByIndex(i-1);
376 | //System.out.println("切到window"+(i-1));
377 | }
378 | }
379 | }
380 |
381 | /**
382 | * 通过frame的ID切换iframe
383 | * @param
384 | * @author 灵枢
385 | */
386 | public static void switchToFrameById(String id){
387 | WebElement frame = driver.findElement(By.id(id));
388 | driver.switchTo().frame(frame);
389 | }
390 |
391 | /**
392 | * 切换iframe
393 | * @param
394 | * @author 灵枢
395 | */
396 | public static void switchToFrameByEle(WebElement iframe){
397 | driver.switchTo().frame(iframe);
398 | }
399 |
400 | /**
401 | * 接收alert
402 | * @param
403 | * @author 灵枢
404 | */
405 | public static void acceptAlert(){
406 | try {
407 | Alert alert = driver.switchTo().alert(); //接收alert
408 | alert.accept();
409 | } catch (Exception e) {
410 | }
411 | }
412 |
413 | /**
414 | * 等待alert出现
415 | * @param alertName
416 | * @return
417 | */
418 | public static Alert waitForAlert(String alertName){
419 | WebDriverWait wait = new WebDriverWait(driver, 10);
420 | Alert targetAlert = null;
421 | try{
422 | targetAlert = wait.until(ExpectedConditions.alertIsPresent());
423 | }catch(Exception e){
424 | assertTrue("没有找到目标元素--"+alertName,false);
425 | }
426 | return targetAlert;
427 | }
428 |
429 | /**
430 | * 用javaScript执行点击事件
431 | * @param
432 | * @author guilin_cui
433 | */
434 | public static void clickByJS(WebElement ele){
435 | JavascriptExecutor js = (JavascriptExecutor)driver;
436 | js.executeScript("arguments[0].click();", ele);
437 | }
438 |
439 | /**
440 | * 用javaScript执行点击事件
441 | * @param by 传入定位
442 | */
443 | public static void clickByJSBy(By by){
444 | WebElement ele = driver.findElement(by);
445 | JavascriptExecutor js = (JavascriptExecutor)driver;
446 | js.executeScript("arguments[0].click();", ele);
447 | }
448 |
449 | /**
450 | * 用javaScript输入
451 | * @param
452 | * @author guilin_cui
453 | */
454 | public static void sendKesyByJS(WebElement ele,String keys){
455 | String str = "arguments[0].value='"+keys+"';";
456 | ele.clear();
457 | JavascriptExecutor js = (JavascriptExecutor)driver;
458 | js.executeScript(str, ele);
459 | }
460 |
461 |
462 | /**
463 | * 打开新的窗口,并切换到该窗口
464 | * @param url 新窗口的url
465 | * @param windwoName 窗口title
466 | */
467 | public static void openWindow(String url,String windwoName){
468 | String js = "window.open(\""+url+"\")";
469 | ((JavascriptExecutor)driver).executeScript(js);
470 | switchToWindowHandle(windwoName);
471 | }
472 |
473 | /**
474 | * 鼠标悬停
475 | * @param ele 元素
476 | * @param ms 悬停时间(毫秒)
477 | */
478 | public static void mouthHover(WebElement ele,int ms){
479 | Actions actions = new Actions(driver);
480 | actions.moveToElement(ele).perform();
481 | threadSleep(ms);
482 | }
483 |
484 | /**
485 | *鼠标点击并悬停
486 | */
487 | public static void clickAndHold(WebElement ele){
488 | Actions actions = new Actions(driver);
489 | actions.clickAndHold(ele).perform();
490 | }
491 |
492 | /**
493 | *双击
494 | */
495 | public static void doubleClick(WebElement ele){
496 | Actions builder = new Actions(driver);
497 | Actions hoverOverRgeistrar = builder.doubleClick(ele);
498 | hoverOverRgeistrar.perform();
499 | }
500 |
501 |
502 | /**
503 | * 线程等待
504 | * @param timeout 单位:毫秒
505 | */
506 | public static void threadSleep(int timeout){
507 | try {
508 | Thread.sleep(timeout);
509 | } catch (InterruptedException e) {
510 | // TODO Auto-generated catch block
511 | e.printStackTrace();
512 | }
513 | }
514 |
515 | /**
516 | * 判断元素是否存在
517 | * @param ele 等待的元素
518 | * @return boolean 是否存在
519 | */
520 | public static boolean isElementExsit(WebElement ele) {
521 | boolean flag ;
522 | try {
523 | WebDriverWait wait = new WebDriverWait(driver,timeOutInSeconds);
524 | WebElement element = wait.until(ExpectedConditions.visibilityOf(ele));
525 | flag = element.isDisplayed();
526 | } catch (Exception e) {
527 | flag = false;
528 | System.out.println("Element:" + ele.toString()
529 | + " is not exsit!");
530 | }
531 | return flag;
532 | }
533 |
534 | /**
535 | * 判断元素是否存在
536 | * @param locator 如:By.id("")
537 | * @return boolean 是否存在
538 | */
539 | public static boolean isElementExsitBy(By locator) {
540 | boolean flag = false;
541 | try {
542 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
543 | wait.until(ExpectedConditions.presenceOfElementLocated(locator));
544 | flag = true;
545 | } catch (NoSuchElementException e) {
546 | System.out.println("Element:" + locator.toString()
547 | + " is not exsit!");
548 | }
549 | return flag;
550 | }
551 |
552 | /**
553 | * 打开url,一直等到页面的readyState为“complete",注意readyState=complete时并不意味着url全部加载完毕,只是这时我们的页面元素可以操作了
554 | * @param url
555 | */
556 | public static void getUrlUntilLoadComplete(String url){
557 | driver.get(url);
558 | for(int i=0;i<10;i++){
559 | if(((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete")){
560 | break;
561 | }
562 | threadSleep(1000);
563 | // System.out.println("readyState:"+((JavascriptExecutor)driver).executeScript("return document.readyState"));
564 | //如果等了10秒还没有加载完,停止加载,往后执行代码
565 | if(i==9){
566 | ((JavascriptExecutor)driver).executeScript("window.stop();");
567 | }
568 | }
569 | }
570 | }
571 |
572 |
--------------------------------------------------------------------------------
/.svn/pristine/3f/3f8f0ab2f5634316def326d1efc27934515e6480.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import java.util.*;
4 |
5 | /**
6 | * Description:随机生成18位身份证号码
7 | * 18位身份证规则:
8 | * 根据〖中华人民共和国国家标准 GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成
9 | * 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 第十八位数字(校验码)的计算方法为:
10 | * 1.将前面的身份证号码17位数分别乘以不同的系数,从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
11 | * 2.将这17位数字和系数相乘的结果相加
12 | * 3.用加出来和除以11,看余数是多少?
13 | * 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字,其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2
14 | * 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ,如果余数是10,身份证的最后一位号码就是2
15 | */
16 | public class IdCardGeneration {
17 | public static final Map idRegion = new HashMap<>();
18 |
19 | public static void main(String[] args) {
20 | IdCardGeneration g = new IdCardGeneration();
21 | /*for (int i = 0; i < 10; i++) {
22 | System.out.print(g.generate());
23 | System.out.print("\t");
24 | System.out.print(g.generate());
25 | System.out.print("\t");
26 | System.out.print(g.generate());
27 | System.out.print("\t");
28 | System.out.println(g.generate());
29 | }*/
30 | System.out.println(g.generate());
31 | }
32 |
33 | static{
34 | IdCardGeneration.idRegion.put("四川省", 510000);
35 | IdCardGeneration.idRegion.put("成都市", 510100);
36 | IdCardGeneration.idRegion.put("市辖区", 510101);
37 | IdCardGeneration.idRegion.put("锦江区", 510104);
38 | IdCardGeneration.idRegion.put("青羊区", 510105);
39 | IdCardGeneration.idRegion.put("金牛区", 510106);
40 | IdCardGeneration.idRegion.put("武侯区", 510107);
41 | IdCardGeneration.idRegion.put("成华区", 510108);
42 | IdCardGeneration.idRegion.put("龙泉驿区", 510112);
43 | IdCardGeneration.idRegion.put("青白江区", 510113);
44 | IdCardGeneration.idRegion.put("新都区", 510114);
45 | IdCardGeneration.idRegion.put("重庆市", 500000);
46 | IdCardGeneration.idRegion.put("市辖区", 500100);
47 | IdCardGeneration.idRegion.put("万州区", 500101);
48 | IdCardGeneration.idRegion.put("涪陵区", 500102);
49 | IdCardGeneration.idRegion.put("渝中区", 500103);
50 | IdCardGeneration.idRegion.put("大渡口区", 500104);
51 | IdCardGeneration.idRegion.put("江北区", 500105);
52 | IdCardGeneration.idRegion.put("沙坪坝区", 500106);
53 | IdCardGeneration.idRegion.put("九龙坡区", 500107);
54 | IdCardGeneration.idRegion.put("南岸区", 500108);
55 | IdCardGeneration.idRegion.put("北碚区", 500109);
56 | IdCardGeneration.idRegion.put("万盛区", 500110);
57 | IdCardGeneration.idRegion.put("双桥区", 500111);
58 | IdCardGeneration.idRegion.put("渝北区", 500112);
59 | IdCardGeneration.idRegion.put("巴南区", 500113);
60 | IdCardGeneration.idRegion.put("黔江区", 500114);
61 | IdCardGeneration.idRegion.put("长寿区", 500115);
62 | IdCardGeneration.idRegion.put("江津区", 500116);
63 | IdCardGeneration.idRegion.put("合川区", 500117);
64 | IdCardGeneration.idRegion.put("永川区", 500118);
65 | IdCardGeneration.idRegion.put("南川区", 500119);
66 | }
67 |
68 | /**
69 | * 生成身份证号码
70 | * @return
71 | */
72 | public String generate() {
73 | StringBuilder generater = new StringBuilder();
74 | generater.append(this.getRandomArea());
75 | generater.append(this.getRandomBirthday());
76 | generater.append(this.getRandomNumber());
77 | generater.append(this.getTestCode(generater.toString().toCharArray()));
78 | return generater.toString();
79 | }
80 |
81 | /**
82 | * 随机获取地区
83 | *
84 | * @return
85 | */
86 | public int getRandomArea() {
87 | int index = (int) (Math.random() * IdCardGeneration.idRegion.size());
88 | Collection values = IdCardGeneration.idRegion.values();
89 | Iterator it = values.iterator();
90 | int i = 0;
91 | int code = 0;
92 | while (i < index && it.hasNext()) {
93 | i++;
94 | code = it.next();
95 | }
96 | return code;
97 | }
98 |
99 | /**
100 | * 随机获取出生日期
101 | *
102 | * @return
103 | */
104 | public String getRandomBirthday() {
105 | Calendar birthday = Calendar.getInstance();
106 | birthday.set(Calendar.YEAR, (int) (Math.random() * 20) + 1985);
107 | birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
108 | birthday.set(Calendar.DATE, (int) (Math.random() * 31));
109 |
110 | StringBuilder builder = new StringBuilder();
111 | builder.append(birthday.get(Calendar.YEAR));
112 | long month = birthday.get(Calendar.MONTH) + 1;
113 | if (month < 10) {
114 | builder.append("0");
115 | }
116 | builder.append(month);
117 | long date = birthday.get(Calendar.DATE);
118 | if (date < 10) {
119 | builder.append("0");
120 | }
121 | builder.append(date);
122 | return builder.toString();
123 | }
124 |
125 |
126 |
127 | /**
128 | * 获取效验码
129 | * @param chars
130 | * @return
131 | */
132 | public char getTestCode(char[] chars) {
133 | if (chars.length < 17) {
134 | return ' ';
135 | }
136 | int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
137 | char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
138 | int[] n = new int[17];
139 | int result = 0;
140 | for (int i = 0; i < n.length; i++) {
141 | n[i] = Integer.parseInt(chars[i] + "");
142 | }
143 | for (int i = 0; i < n.length; i++) {
144 | result += c[i] * n[i];
145 | }
146 | return r[result % 11];
147 | }
148 |
149 | /**
150 | *获取3位随机数
151 | * @return
152 | */
153 | public String getRandomNumber() {
154 | int code = (int) (Math.random() * 1000);
155 | if (code < 10) {
156 | return "00" + code;
157 | } else if (code < 100) {
158 | return "0" + code;
159 | } else {
160 | return "" + code;
161 | }
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/.svn/pristine/4d/4d443d4a562d7d4f23dafb110ee1257e668ab0b4.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.testng.IAnnotationTransformer;
4 | import org.testng.IRetryAnalyzer;
5 | import org.testng.annotations.ITestAnnotation;
6 |
7 | import java.lang.reflect.Constructor;
8 | import java.lang.reflect.Method;
9 |
10 | /**
11 | * Author: 灵枢
12 | * Date: 2018/12/25
13 | * Time: 17:40
14 | * Description:
15 | */
16 | public class RetryListener implements IAnnotationTransformer {
17 |
18 | @Override
19 | public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
20 | IRetryAnalyzer retryAnalyzer = annotation.getRetryAnalyzer();
21 | if(retryAnalyzer==null){
22 | annotation.setRetryAnalyzer(Retry.class);
23 | }
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/.svn/pristine/5c/5c912473203b1302769766f01995c4bef0a24d80.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.apache.poi.xssf.usermodel.XSSFRow;
4 | import org.apache.poi.xssf.usermodel.XSSFSheet;
5 | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
6 |
7 | import java.io.FileInputStream;
8 | import java.text.DecimalFormat;
9 |
10 |
11 | /**
12 | * Author: 灵枢
13 | * Date: 2018/12/05
14 | * Time: 17:21
15 | * Description:读取Excel.xlsx数据
16 | */
17 | public class ExcelData {
18 |
19 | private XSSFSheet sheet;
20 |
21 | /**
22 | * 构造函数,初始化excel数据
23 | * @param filePath excel路径
24 | * @param sheetName sheet表名
25 | */
26 | public ExcelData(String filePath,String sheetName){
27 | FileInputStream fileInputStream = null;
28 | try {
29 | fileInputStream = new FileInputStream(filePath);
30 | XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
31 | //获取sheet
32 | sheet = sheets.getSheet(sheetName);
33 | } catch (Exception e) {
34 | e.printStackTrace();
35 | }
36 | }
37 |
38 |
39 | /**
40 | * 根据行和列的索引获取单元格的数据
41 | * @param row
42 | * @param column
43 | * @return
44 | */
45 | public String getExcelDateByIndex(int row,int column){
46 | XSSFRow row1 = sheet.getRow(row);
47 | String cell = row1.getCell(column).toString();
48 | return cell;
49 | }
50 |
51 |
52 | /**
53 | * 根据某一列值为“******”的这一行,来获取该行第x列的值
54 | * @param caseName
55 | * @param currentColumn 当前单元格列的索引
56 | * @param targetColumn 目标单元格列的索引
57 | * @return
58 | */
59 | public String getCellByCaseName(String caseName,int currentColumn,int targetColumn){
60 | String operateSteps="";
61 | //获取行数
62 | int rows = sheet.getPhysicalNumberOfRows();
63 | for(int i=0;i
16 | * 邮箱:404110318@qq.com
17 | *
18 | * 描述:对xml文件的读写
19 | */
20 | public class XmlUtils {
21 |
22 | private static SAXReader reader = new SAXReader();// 创建解析器
23 |
24 | public static String readXml(String addr,String elementName){
25 | String elementValue = null;
26 | try {
27 | Document doc = reader.read(addr);
28 | Element rootElement = doc.getRootElement();
29 | List elements = rootElement.elements();
30 | for (Element element : elements) {
31 | if (element.getName().equals(elementName)) {
32 | elementValue = element.getText();
33 | // System.out.println(elementName + ":" + elementValue);
34 | }
35 | }
36 |
37 | }catch (Exception e) {
38 | e.printStackTrace();
39 | }
40 | return elementValue;
41 | }
42 |
43 | public static void writeXml(String addr,String elementName,String elementValue){
44 |
45 | try {
46 | // 调用读方法,得到Document
47 | Document doc = reader.read(addr);
48 | //得到根元素
49 | Element rootElement = doc.getRootElement();
50 | //向根元素中添加一个名为elementName的元素!并返回这个元素
51 | Element ele = rootElement.addElement(elementName);
52 |
53 | //给该元素添加值
54 | ele.setText(elementValue);
55 |
56 | // 创建目标输出流,它需要与xml文件绑定
57 | Writer out = new PrintWriter(addr,"UTF-8");
58 | // 创建格式化器
59 | OutputFormat format = new OutputFormat("\t",true);
60 | format.setTrimText(true);//先干掉原来的空白(\t和换行和空格)!
61 |
62 | // 创建XMLWrtier
63 | XMLWriter writer = new XMLWriter(out,format);
64 | // 调用它的写方法,把document对象写到out流中。
65 | writer.write(doc);
66 | // 关闭流
67 | out.close();
68 | writer.close();
69 | }catch (Exception e) {
70 | e.printStackTrace();
71 | }
72 |
73 | }
74 |
75 | public static void removeXml(String addr,String elementName){
76 |
77 | try {
78 | // 调用读方法,得到Document
79 | Document doc = reader.read(addr);
80 | //得到根元素
81 | Element rootElement = doc.getRootElement();
82 | //移除elementName元素
83 | List elements = rootElement.elements();
84 | for (Element element : elements) {
85 | if (element.getName().equals(elementName)) {
86 | rootElement.remove(element);
87 | }
88 | }
89 |
90 | // 创建目标输出流,它需要与xml文件绑定
91 | Writer out = new PrintWriter(addr,"UTF-8");
92 | // 创建格式化器
93 | OutputFormat format = new OutputFormat("\t",true);
94 | format.setTrimText(true);//先干掉原来的空白(\t和换行和空格)!
95 |
96 | // 创建XMLWrtier
97 | XMLWriter writer = new XMLWriter(out,format);
98 | // 调用它的写方法,把document对象写到out流中。
99 | writer.write(doc);
100 | // 关闭流
101 | out.close();
102 | writer.close();
103 | }catch (Exception e) {
104 | e.printStackTrace();
105 | }
106 |
107 | }
108 |
109 | /**
110 | * 删除根节点外的所有其它节点
111 | * @param addr
112 | */
113 | public static void removeAll(String addr){
114 |
115 | try {
116 | // 调用读方法,得到Document
117 | Document doc = reader.read(addr);
118 | //得到根元素
119 | Element rootElement = doc.getRootElement();
120 | //移除elementName元素
121 | List elements = rootElement.elements();
122 | for (Element element : elements) {
123 | rootElement.remove(element);
124 | }
125 | // 创建目标输出流,它需要与xml文件绑定
126 | Writer out = new PrintWriter(addr,"UTF-8");
127 | // 创建格式化器
128 | OutputFormat format = new OutputFormat("\t",true);
129 | format.setTrimText(true);//先干掉原来的空白(\t和换行和空格)!
130 |
131 | // 创建XMLWrtier
132 | XMLWriter writer = new XMLWriter(out,format);
133 | // 调用它的写方法,把document对象写到out流中。
134 | writer.write(doc);
135 | // 关闭流
136 | out.close();
137 | writer.close();
138 | }catch (Exception e) {
139 | e.printStackTrace();
140 | }
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/.svn/pristine/65/65b3036b7309fc047130744241d34602ae911613.svn-base:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.svn/pristine/66/66c70257fbf22ca1e4519e1b146ff82f47e20af6.svn-base:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/.svn/pristine/66/66c70257fbf22ca1e4519e1b146ff82f47e20af6.svn-base
--------------------------------------------------------------------------------
/.svn/pristine/67/67b062a832ff8188fe40893a4f8cccb5a0821c13.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.cases;
2 |
3 | import org.testng.Assert;
4 | import org.testng.annotations.Test;
5 | import test.java.common.BaseTest;
6 | import test.java.common.OperateElement;
7 | import test.java.pageobject.SogouPO;
8 | import test.java.utils.ReadProperties;
9 |
10 | /**
11 | * Author: 灵枢
12 | * Date: 2019/6/12
13 | * Time: 18:07
14 | * Description:
15 | */
16 | public class SogouTest extends BaseTest {
17 |
18 | @Test
19 | public void test搜狗搜索(){
20 | String keywords = envProperties.getProperty("keywords");
21 | String sogouUrl = envProperties.getProperty("sogou.url");
22 | driver.get(sogouUrl);
23 | SogouPO.search(keywords);
24 | //验证搜索结果第一行是否包含“galen2016的博客”
25 | String firstResult = OperateElement.waitByXpath("(//*[@class='vrTitle']/a)[1]").getText();
26 | Assert.assertTrue(firstResult.contains("galen2016的博客"));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/.svn/pristine/6b/6bbbf124c9c9a6caf456f0df2ec5fa55639416ae.svn-base:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/.svn/pristine/6b/6bbbf124c9c9a6caf456f0df2ec5fa55639416ae.svn-base
--------------------------------------------------------------------------------
/.svn/pristine/7f/7f9d84f01368bd9f33d406c64ae76c70fdad002e.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import org.openqa.selenium.chrome.ChromeDriver;
4 | import org.openqa.selenium.chrome.ChromeOptions;
5 | import org.openqa.selenium.firefox.FirefoxDriver;
6 | import org.openqa.selenium.ie.InternetExplorerDriver;
7 | import org.openqa.selenium.remote.CapabilityType;
8 | import org.openqa.selenium.remote.DesiredCapabilities;
9 |
10 | import java.util.HashMap;
11 | import java.util.concurrent.TimeUnit;
12 |
13 | /*
14 | *包名:com.zxkj.operate
15 | *作者:Adien_cui
16 | *时间:2017-8-21 上午10:20:01
17 | *描述:初始化Driver
18 | **/
19 | public class InitDriver {
20 | //初始化ChromeDriver
21 | public static ChromeDriver launchChromeDriver(){
22 | System.setProperty("webdriver.chrome.driver","resource/chromedriver.exe");
23 | DesiredCapabilities capabilities = DesiredCapabilities.chrome();
24 | capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
25 | //允许浏览器运行js脚本
26 | capabilities.setJavascriptEnabled(true);
27 | //设置页面加载策略
28 | // capabilities.setCapability("pageLoadStrategy","none");
29 | //设置chrome下载路径
30 | ChromeOptions options = new ChromeOptions();
31 | String downloadPath ="D:\\AutoTestAttachment\\Downloads";
32 | HashMap hm = new HashMap();
33 | hm.put("download.default_directory",downloadPath);
34 | options.setExperimentalOption("prefs",hm);
35 | //浏览器最大化
36 | options.addArguments("start-maximized");
37 | capabilities.setCapability(ChromeOptions.CAPABILITY,options);
38 | //new一个chromedriver
39 | ChromeDriver driver = new ChromeDriver(capabilities);
40 | //设置超时时间
41 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
42 | driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
43 | return driver;
44 | }
45 |
46 | //初始化firefoxDriver
47 | public static FirefoxDriver launchFirefoxDriver(){
48 | FirefoxDriver driver = new FirefoxDriver();
49 | driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
50 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
51 | driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
52 | driver.manage().window().maximize();
53 | return driver;
54 | }
55 |
56 | //初始化InternetExplorerDriver
57 | public static InternetExplorerDriver launchInternetExplorerDriver(){
58 | System.setProperty("webdriver.chrome.driver","F:/AutoTestAttachment/internetExplorerDriver.exe");
59 | InternetExplorerDriver driver = new InternetExplorerDriver();
60 | driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
61 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
62 | driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
63 | driver.manage().window().maximize();
64 | return driver;
65 | }
66 |
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/.svn/pristine/83/838ac2106539e0b32d37ff350e9a7d5478833207.svn-base:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.svn/pristine/84/84145a3a5fbf680c16a271129f98a9a7041d08bf.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.apache.log4j.Logger;
4 |
5 | /**
6 | * Created by pc on 2018/6/26.
7 | */
8 | public class Log4jTest {
9 | private static Logger logger = Logger.getLogger(Log4jTest.class);
10 |
11 | /**
12 | * @param args
13 | */
14 | public static void main(String[] args) {
15 | // System.out.println("This is println message.");
16 |
17 | // 记录debug级别的信息
18 | logger.debug("This is debug message.");
19 | // 记录info级别的信息
20 | logger.info("This is info message.");
21 | // 记录error级别的信息
22 | logger.error("This is error message.");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/.svn/pristine/8f/8f7506d20d579dbeda5d3cd62db649b84d69c961.svn-base:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.svn/pristine/8f/8fdd2101f6e9b2fc4c41b18f63ca94c1c93e4ce4.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import org.openqa.selenium.chrome.ChromeDriver;
4 | import org.testng.annotations.AfterClass;
5 | import org.testng.annotations.BeforeClass;
6 | import org.testng.annotations.Listeners;
7 | import org.testng.annotations.Parameters;
8 | import test.java.utils.ReadProperties;
9 |
10 | import java.util.Properties;
11 |
12 | /**
13 | * Author: 灵枢
14 | * Date: 2019/3/28
15 | * Time: 17:11
16 | * Description:
17 | */
18 |
19 | public class BaseTest {
20 | public ChromeDriver driver;
21 | public Properties envProperties;
22 |
23 | public ChromeDriver getDriver(){
24 | return driver;
25 | }
26 |
27 | @BeforeClass
28 | public void setUp(){
29 | //初始化webdriver
30 | driver = InitDriver.launchChromeDriver();
31 | OperateElement.driver = driver;
32 | //读取配置文件
33 | envProperties = ReadProperties.readProperties("env.properties");
34 | }
35 |
36 | @AfterClass(enabled = false)
37 | public void tearDown() {
38 | driver.quit();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/.svn/pristine/94/940996d965a99b8a6b600ca24cb8ec2ef74b10dd.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import com.csvreader.CsvReader;
4 |
5 | import java.nio.charset.Charset;
6 | import java.util.ArrayList;
7 |
8 | /*
9 | *作者:Adien_cui
10 | *时间:2017-9-25 下午4:36:29
11 | *描述:读取csv文件
12 | **/
13 | public class ReadCsvFile {
14 | private static ArrayList csvList;
15 |
16 | /**
17 | * 读取csv文件,并存入list集合
18 | * @param filePath
19 | */
20 | public static void readCsvFile(String filePath){
21 | try {
22 | csvList = new ArrayList();
23 | // CsvReader reader = new CsvReader(filePath,',',Charset.forName("SJIS"));
24 | CsvReader reader = new CsvReader(filePath,',',Charset.forName("GBK"));
25 | // reader.readHeaders(); //跳过表头,不跳可以注释掉
26 |
27 | while(reader.readRecord()){
28 | csvList.add(reader.getValues()); //按行读取,并把每一行的数据添加到list集合
29 | }
30 | reader.close();
31 | // System.out.println("读取的行数:"+csvList.size());
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 | /**
38 | * 读取测试用例的描述
39 | * @param filePath csv文件路径
40 | * @param caseName 用例名
41 | * @return
42 | */
43 | public static String getCaseDescription(String filePath,String caseName){
44 | readCsvFile(filePath);
45 | for(int row=0;row
2 |
5 | 4.0.0
6 |
7 | com.lingshu
8 | SelenJA
9 | 1.0-SNAPSHOT
10 |
11 |
12 | UTF-8
13 | 1.8.5
14 |
15 |
16 |
17 |
18 |
19 | org.seleniumhq.selenium
20 | selenium-java
21 | 3.7.1
22 |
23 |
24 |
25 | org.testng
26 | testng
27 | 6.8.8
28 |
29 |
30 |
31 | dom4j
32 | dom4j
33 | 1.6.1
34 |
35 |
36 |
37 | io.appium
38 | java-client
39 | 3.4.0
40 |
41 |
42 |
43 | com.google.inject
44 | guice
45 | 3.0
46 | test
47 |
48 |
49 |
50 | org.slf4j
51 | slf4j-api
52 | 1.7.21
53 |
54 |
55 |
56 | org.slf4j
57 | slf4j-log4j12
58 | 1.7.21
59 |
60 |
61 | commons-logging
62 | commons-logging
63 | 1.2
64 |
65 |
66 |
67 | org.codehaus.jackson
68 | jackson-mapper-asl
69 | 1.9.13
70 |
71 |
72 |
73 |
74 | org.apache.poi
75 | poi-ooxml
76 | 3.15
77 |
78 |
79 |
80 | net.sourceforge.javacsv
81 | javacsv
82 | 2.0
83 |
84 |
85 |
86 |
87 | com.sikulix
88 | sikulixapi
89 | 1.1.0
90 |
91 |
92 |
93 |
94 | com.microsoft.sqlserver
95 | sqljdbc4
96 | 4.0
97 | system
98 | ${basedir}/libs/sqljdbc4.jar
99 |
100 |
101 |
102 |
103 |
104 | io.qameta.allure
105 | allure-testng
106 | 2.0-BETA21
107 |
108 |
109 |
110 |
111 |
112 | compile
113 | src
114 |
115 |
116 | src
117 |
118 | **/*.java
119 |
120 |
121 |
122 |
123 |
124 | org.apache.maven.plugins
125 | maven-surefire-plugin
126 | 2.12.4
127 |
128 |
129 | testng.xml
130 |
131 |
132 | -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
133 |
134 |
135 |
136 |
137 | org.aspectj
138 | aspectjweaver
139 | ${aspectj.version}
140 |
141 |
142 |
143 |
144 | org.apache.maven.plugins
145 | maven-compiler-plugin
146 |
147 | 8
148 | 8
149 |
150 |
151 |
152 |
153 |
154 |
--------------------------------------------------------------------------------
/.svn/pristine/b4/b47ece37f00bf63f04b228baa1cca94819ad0408.svn-base:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/.svn/pristine/b4/b47ece37f00bf63f04b228baa1cca94819ad0408.svn-base
--------------------------------------------------------------------------------
/.svn/pristine/bb/bb20cf436382a1316d10edbc08cbfeafd087cbaf.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.testng.IRetryAnalyzer;
4 | import org.testng.ITestResult;
5 |
6 | /**
7 | * Author: 灵枢
8 | * Date: 2018/12/25
9 | * Time: 17:40
10 | * Description:
11 | */
12 | public class Retry implements IRetryAnalyzer {
13 | private int retryCount = 0;
14 | private int maxRetryCount = 1;
15 | @Override
16 | public boolean retry(ITestResult iTestResult) {
17 | if(retryCount
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.svn/pristine/d5/d51628ab14729fce40821a679393daabb65b0d94.svn-base:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.svn/pristine/d6/d6cb6f7ef9de23237578c0582ef9cb814586629e.svn-base:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.apache.poi.hssf.usermodel.HSSFRow;
4 | import org.apache.poi.hssf.usermodel.HSSFSheet;
5 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
6 | import org.apache.poi.poifs.filesystem.POIFSFileSystem;
7 |
8 | import java.io.File;
9 | import java.io.FileInputStream;
10 | import java.io.FileOutputStream;
11 | import java.util.ArrayList;
12 |
13 | /**
14 | * Author: 灵枢
15 | * Date: 2019/1/16
16 | * Time: 16:39
17 | * Description:获取excel.xls的数据
18 | */
19 | public class ExcelXlsData {
20 |
21 | private HSSFSheet sheetAt;
22 |
23 | public ExcelXlsData(String filePath){
24 | FileInputStream is = null;
25 | try {
26 | is = new FileInputStream(filePath);
27 | HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
28 | sheetAt = hssfWorkbook.getSheetAt(0);
29 | } catch (Exception e) {
30 | e.printStackTrace();
31 | }
32 | }
33 |
34 | /**
35 | * 读取excel的全部数据
36 | * @param filePath
37 | */
38 | public void readXlsData(String filePath){
39 | try {
40 | int rows = sheetAt.getPhysicalNumberOfRows();
41 | for (int i=0;i getCellList(int columnIndex){
72 | ArrayList list = new ArrayList<>();
73 | int rows = sheetAt.getPhysicalNumberOfRows();
74 | for(int i=1;i
59 | ```
60 |
61 | ```
62 | OperateElement.waitElesByClassName(String className) //通过元素的className,等待元素列表的出现,返回List
63 | ```
64 |
65 | ```
66 | OperateElement.clickByText(String text) //通过html某个标签中间的text找到元素,并点击它
67 | ```
68 |
69 | ```
70 | OperateElement.clickByCss(String css) //通过元素的Xpath,等待元素的出现,并点击它
71 | ```
72 |
73 | ```
74 | OperateElement.clickByAttribute(String attribute,String value) //通过元素的某个特定属性,等待元素的出现,并点击它
75 | ```
76 |
77 | ```
78 | OperateElement.clickByJSBy(By by) //用javaScript执行点击事件
79 | ```
80 |
81 | ```
82 | OperateElement.isElementExsitBy(By locator) //判断元素是否存在
83 | ```
84 |
85 | ```
86 | OperateElement.sendKeysByXpath(String xpath,String keys) //通过元素的Xpath,等待文本框的出现,并输入
87 | ```
88 |
89 | ```
90 | OperateElement.dragAndDropBy(WebElement element,int xOffset,int yOffset) //拖动元素
91 | ```
92 |
93 | ```
94 | OperateElement.mouthHover(WebElement ele,int ms) //鼠标悬停
95 | ```
96 | - 浏览器操作
97 |
98 | ```
99 | OperateElement.switchToWindowByIndex(int index) //通过窗口的索引来切换窗口,index从0开始
100 | ```
101 |
102 | ```
103 | OperateElement.closeWindowHandleNotFirst() //关闭除了第一个窗口外的其它窗口
104 | ```
105 |
106 | ```
107 | OperateElement.acceptAlert() //接收alert
108 | ```
109 |
110 | ```
111 | OperateElement.switchToFrameById(String id) //通过frame的ID切换iframe
112 | ```
113 | - 其它
114 |
115 | ```
116 | OperateElement.threadSleep(int timeout) //线程等待,单位:毫秒
117 | ```
118 |
119 | # 安装使用
120 | - 安装jdk1.8
121 | - 安装maven,我的版本是3.3.9
122 | - 安装IDEA
123 | - clone项目到本地,然后用IDEA打开
124 | - 安装Jenkins
125 | - Jenkins添加Allure插件,可参考:https://blog.csdn.net/galen2016/article/details/88015322
126 | - Jenkins新增一个job并配置
127 | - 构建项目
128 | - 查看Allure报告
129 | 
130 | # 调用原理
131 | 
132 | # 项目结构介绍
133 | ### 主目录:
134 | 
135 | ### libs文件夹
136 | 存放外部jar包,如jdbc的jar不能通过maven下载,就可以直接下下来放到这里,然后在pom.xml里引用
137 | ### resource文件夹
138 | 存放一些资源文件,如测试用例.excel文件
139 | ### cases包
140 | 测试用例文件
141 | ### common包
142 | 
143 | BaseTest.java 测试用例的基类,其他测试用例都继承这个类
144 | InitDriver.java 初始webdriver,可以提供获取chrome、firefox、ie三个浏览器的方法
145 | OperateElement.java 对webdriver的API做了二次封装
146 | TestngListener 全局监控器,控制失败用例重跑、截图、打印测试步骤
147 |
148 | ### pageobject包
149 | 对每个测试页面的元素和公共操作封装成一个类
150 |
151 | ### utils包
152 | 封装一些实用的工具类,如:读取excel、xml、配置文件,获取随机身份证号码、地址等
153 |
154 | ### env.properties
155 | 测试要用到的url、账号密码等可以写在这个文件里面
156 | ### pom.xml
157 | 1、管理jar包 2、配置maven-surefire-plugin插件来运行test包下面的代码
158 |
--------------------------------------------------------------------------------
/env.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/env.properties
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.lingshu
8 | SelenJA
9 | 1.0-SNAPSHOT
10 |
11 |
12 | UTF-8
13 | 1.8.5
14 |
15 |
16 |
17 |
18 |
19 | org.seleniumhq.selenium
20 | selenium-java
21 | 3.7.1
22 |
23 |
24 |
25 | org.testng
26 | testng
27 | 6.8.8
28 |
29 |
30 |
31 | dom4j
32 | dom4j
33 | 1.6.1
34 |
35 |
36 |
37 | io.appium
38 | java-client
39 | 3.4.0
40 |
41 |
42 |
43 | com.google.inject
44 | guice
45 | 3.0
46 | test
47 |
48 |
49 |
50 | org.slf4j
51 | slf4j-api
52 | 1.7.21
53 |
54 |
55 |
56 | org.slf4j
57 | slf4j-log4j12
58 | 1.7.21
59 |
60 |
61 | commons-logging
62 | commons-logging
63 | 1.2
64 |
65 |
66 |
67 | org.codehaus.jackson
68 | jackson-mapper-asl
69 | 1.9.13
70 |
71 |
72 |
73 |
74 | org.apache.poi
75 | poi-ooxml
76 | 3.15
77 |
78 |
79 |
80 | net.sourceforge.javacsv
81 | javacsv
82 | 2.0
83 |
84 |
85 |
86 |
87 | com.sikulix
88 | sikulixapi
89 | 1.1.0
90 |
91 |
92 |
93 |
94 | com.microsoft.sqlserver
95 | sqljdbc4
96 | 4.0
97 | system
98 | ${basedir}/libs/sqljdbc4.jar
99 |
100 |
101 |
102 |
103 |
104 | io.qameta.allure
105 | allure-testng
106 | 2.0-BETA21
107 |
108 |
109 |
110 |
111 |
112 | compile
113 | src
114 |
115 |
116 | src
117 |
118 | **/*.java
119 |
120 |
121 |
122 |
123 |
124 | org.apache.maven.plugins
125 | maven-surefire-plugin
126 | 2.12.4
127 |
128 |
129 | testng.xml
130 |
131 |
132 | -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
133 |
134 |
135 |
136 |
137 | org.aspectj
138 | aspectjweaver
139 | ${aspectj.version}
140 |
141 |
142 |
143 |
144 | org.apache.maven.plugins
145 | maven-compiler-plugin
146 |
147 | 8
148 | 8
149 |
150 |
151 |
152 |
153 |
154 |
--------------------------------------------------------------------------------
/resource/UpLoadRoomPic.au3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/resource/UpLoadRoomPic.au3
--------------------------------------------------------------------------------
/resource/UpLoadRoomPic.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/resource/UpLoadRoomPic.exe
--------------------------------------------------------------------------------
/resource/chromedriver.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/resource/chromedriver.exe
--------------------------------------------------------------------------------
/resource/自动化测试用例.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Adien-galen/SelenJA/005d0e74c2bfab98b7416183555d3f05bf1cf661/resource/自动化测试用例.xlsx
--------------------------------------------------------------------------------
/src/test/java/cases/BaiduTest.java:
--------------------------------------------------------------------------------
1 | package test.java.cases;
2 |
3 | import org.openqa.selenium.By;
4 | import org.testng.Assert;
5 | import org.testng.annotations.BeforeMethod;
6 | import org.testng.annotations.Test;
7 | import test.java.common.BaseTest;
8 | import test.java.common.OperateElement;
9 | import test.java.pageobject.BaiduPO;
10 |
11 | /**
12 | * Author: 灵枢
13 | * Date: 2018/12/4
14 | * Time: 16:07
15 | * Description:
16 | */
17 |
18 | public class BaiduTest extends BaseTest {
19 |
20 | private String baiduUrl;
21 |
22 | @BeforeMethod
23 | public void beforeMethod(){
24 | baiduUrl = envProperties.getProperty("baidu.url");
25 | }
26 |
27 | @Test
28 | public void test百度搜索(){
29 | String keywords = envProperties.getProperty("keywords");
30 | driver.get(baiduUrl);
31 | BaiduPO.search(keywords);
32 | BaiduPO.searchResult(1).click();
33 | //切换到第二个窗口
34 | OperateElement.switchToWindowByIndex(1);
35 | //获取博客页的titleName
36 | String titleName = OperateElement.waitByXpath("//*[@class='lt_title']").getText().trim();
37 | //验证titleName是否正确
38 | Assert.assertTrue(titleName.contains("灵枢"),"title不对,显示的是:"+titleName);
39 | }
40 |
41 | @Test
42 | public void test百度登录(){
43 | //读取配置文件中的账号密码
44 | String username = envProperties.getProperty("baidu.username");
45 | String password = envProperties.getProperty("baidu.password");
46 | //回到第一个窗口
47 | OperateElement.closeWindowHandleNotFirst();
48 | driver.get(baiduUrl);
49 | OperateElement.clickByLinkText("登录");
50 | BaiduPO.login(username,password);
51 | //验证用户名是否存在
52 | // boolean exsit = OperateElement.isElementExsit(OperateElement.waitByClassName("user-name"));
53 | boolean exsit = OperateElement.isElementExsitBy(By.className("user-name"));
54 | Assert.assertTrue(exsit);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/test/java/cases/SogouTest.java:
--------------------------------------------------------------------------------
1 | package test.java.cases;
2 |
3 | import org.testng.Assert;
4 | import org.testng.annotations.Test;
5 | import test.java.common.BaseTest;
6 | import test.java.common.OperateElement;
7 | import test.java.pageobject.SogouPO;
8 | import test.java.utils.ReadProperties;
9 |
10 | /**
11 | * Author: 灵枢
12 | * Date: 2019/6/12
13 | * Time: 18:07
14 | * Description:
15 | */
16 | public class SogouTest extends BaseTest {
17 |
18 | @Test
19 | public void test搜狗搜索(){
20 | String keywords = envProperties.getProperty("keywords");
21 | String sogouUrl = envProperties.getProperty("sogou.url");
22 | driver.get(sogouUrl);
23 | SogouPO.search(keywords);
24 | //验证搜索结果第一行是否包含“galen2016的博客”
25 | String firstResult = OperateElement.waitByXpath("(//*[@class='vrTitle']/a)[1]").getText();
26 | Assert.assertTrue(firstResult.contains("galen2016的博客"));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/test/java/common/BaseTest.java:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import org.openqa.selenium.chrome.ChromeDriver;
4 | import org.testng.annotations.AfterClass;
5 | import org.testng.annotations.BeforeClass;
6 | import org.testng.annotations.Listeners;
7 | import test.java.utils.ReadProperties;
8 |
9 | import java.util.Properties;
10 |
11 | /**
12 | * Author: 灵枢
13 | * Date: 2019/3/28
14 | * Time: 17:11
15 | * Description:
16 | */
17 |
18 | @Listeners({TestngListener.class})
19 | public class BaseTest {
20 | public ChromeDriver driver;
21 | public Properties envProperties;
22 |
23 | public ChromeDriver getDriver(){
24 | return driver;
25 | }
26 |
27 | @BeforeClass
28 | public void setUp(){
29 | //初始化webdriver
30 | driver = InitDriver.launchChromeDriver();
31 | OperateElement.driver = driver;
32 | //读取配置文件
33 | envProperties = ReadProperties.readProperties("env.properties");
34 | }
35 |
36 | @AfterClass(enabled = false)
37 | public void tearDown() {
38 | driver.quit();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/common/InitDriver.java:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import org.openqa.selenium.chrome.ChromeDriver;
4 | import org.openqa.selenium.chrome.ChromeOptions;
5 | import org.openqa.selenium.firefox.FirefoxDriver;
6 | import org.openqa.selenium.ie.InternetExplorerDriver;
7 | import org.openqa.selenium.remote.CapabilityType;
8 | import org.openqa.selenium.remote.DesiredCapabilities;
9 |
10 | import java.util.HashMap;
11 | import java.util.concurrent.TimeUnit;
12 |
13 | /*
14 | *包名:com.zxkj.operate
15 | *作者:Adien_cui
16 | *时间:2017-8-21 上午10:20:01
17 | *描述:初始化Driver
18 | **/
19 | public class InitDriver {
20 | //初始化ChromeDriver
21 | public static ChromeDriver launchChromeDriver(){
22 | System.setProperty("webdriver.chrome.driver","resource/chromedriver.exe");
23 | DesiredCapabilities capabilities = DesiredCapabilities.chrome();
24 | capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
25 | //允许浏览器运行js脚本
26 | capabilities.setJavascriptEnabled(true);
27 | //设置页面加载策略
28 | // capabilities.setCapability("pageLoadStrategy","none");
29 | //设置chrome下载路径
30 | ChromeOptions options = new ChromeOptions();
31 | String downloadPath ="D:\\AutoTestAttachment\\Downloads";
32 | HashMap hm = new HashMap();
33 | hm.put("download.default_directory",downloadPath);
34 | options.setExperimentalOption("prefs",hm);
35 | //浏览器最大化
36 | options.addArguments("start-maximized");
37 | capabilities.setCapability(ChromeOptions.CAPABILITY,options);
38 | //new一个chromedriver
39 | ChromeDriver driver = new ChromeDriver(capabilities);
40 | //设置超时时间
41 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
42 | driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
43 | return driver;
44 | }
45 |
46 | //初始化firefoxDriver
47 | public static FirefoxDriver launchFirefoxDriver(){
48 | FirefoxDriver driver = new FirefoxDriver();
49 | driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
50 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
51 | driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
52 | driver.manage().window().maximize();
53 | return driver;
54 | }
55 |
56 | //初始化InternetExplorerDriver
57 | public static InternetExplorerDriver launchInternetExplorerDriver(){
58 | System.setProperty("webdriver.chrome.driver","F:/AutoTestAttachment/internetExplorerDriver.exe");
59 | InternetExplorerDriver driver = new InternetExplorerDriver();
60 | driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
61 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
62 | driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
63 | driver.manage().window().maximize();
64 | return driver;
65 | }
66 |
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/test/java/common/OperateElement.java:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import org.openqa.selenium.*;
4 | import org.openqa.selenium.interactions.Actions;
5 | import org.openqa.selenium.support.ui.ExpectedConditions;
6 | import org.openqa.selenium.support.ui.WebDriverWait;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Iterator;
10 | import java.util.List;
11 | import java.util.Set;
12 |
13 | import static org.testng.AssertJUnit.assertTrue;
14 |
15 | /*
16 | *作者:灵枢
17 | *时间:2018-11-22 下午4:45:15
18 | *描述:封装元素的基本操作:等待、点击、输入
19 | **/
20 |
21 |
22 | public class OperateElement {
23 | private static int timeOutInSeconds = 10;
24 | public static WebDriver driver;
25 |
26 | /**
27 | * 通过元素的Xpath,等待元素的出现,返回此元素
28 | * @return 返回等待的元素
29 | */
30 | public static WebElement waitByXpath(String xpath){
31 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
32 | //presenceOfElementLocated:显示等待元素在页面中存在,不用等页面全部加载完
33 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
34 | return targetElement;
35 | }
36 |
37 | /**
38 | * 通过元素的name,等待元素的出现,返回此元素
39 | * @param name 元素的name
40 | * @return 返回等待的元素
41 | */
42 | public static WebElement waitByName(String name){
43 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
44 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.name(name)));
45 | return targetElement;
46 | }
47 |
48 | /**
49 | * 通过元素的id,等待元素的出现,返回此元素
50 | * @param id 元素的id
51 | * @return 返回等待的元素
52 | */
53 | public static WebElement waitById(String id){
54 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
55 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
56 | return targetElement;
57 | }
58 |
59 | /**
60 | * 通过元素的linkText,等待元素的出现,返回此元素
61 | * @param linkText 元素的linkText
62 | * @return 返回等待的元素
63 | */
64 | public static WebElement waitByLinkText(String linkText){
65 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
66 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(linkText)));
67 | return targetElement;
68 | }
69 |
70 | /**
71 | * 通过元素的className,等待元素的出现,返回此元素
72 | * @param className 元素的className
73 | * @return 返回等待的元素
74 | */
75 | public static WebElement waitByClassName(String className){
76 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
77 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.className(className)));
78 | return targetElement;
79 | }
80 |
81 | /**
82 | * 通过元素的Css,等待元素的出现,返回此元素
83 | * @return 返回等待的元素
84 | */
85 | public static WebElement waitByCss(String css){
86 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
87 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(css)));
88 | return targetElement;
89 | }
90 |
91 | /**
92 | * 通过元素的某个属性,等待元素的出现,返回此元素
93 | * @param attribute 属性名
94 | * @param value 属性值
95 | * @return
96 | */
97 | public static WebElement waitByAttribute(String attribute,String value){
98 | String xpath = "//*[@"+attribute+"='"+value+"']";
99 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
100 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
101 | return targetElement;
102 | }
103 |
104 | /**
105 | * 通过html某个标签中间的text,等待元素的出现,返回此元素
106 | * @param text
107 | * @return
108 | */
109 | public static WebElement waitByText(String text){
110 | String xpath = "//*[text()='"+text+"']";
111 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
112 | WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
113 | return targetElement;
114 | }
115 |
116 | /**
117 | * 通过元素的className,等待元素列表的出现,返回此元素列表
118 | * @param className 元素的className
119 | * @return 返回等待的元素
120 | */
121 | public static List waitElesByClassName(String className){
122 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
123 | List targetElementList = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.className(className))));
124 | return targetElementList;
125 | }
126 |
127 | /**
128 | * 通过元素的linktext,等待元素列表的出现,返回此元素列表
129 | * @param linkText 元素的linktext
130 | * @return 返回等待的元素
131 | */
132 | public static List waitElesByLinkText(String linkText){
133 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
134 | List targetElementList = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.linkText(linkText))));
135 | return targetElementList ;
136 | }
137 |
138 | /**
139 | * 通过元素的xpath,等待元素列表的出现,返回此元素列表
140 | * @param xpath
141 | * @return
142 | */
143 | public static List waitElesByXpath(String xpath){
144 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
145 | List targetElementList = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.xpath(xpath))));
146 | return targetElementList ;
147 | }
148 |
149 | /**
150 | * 通过元素的linkText,等待元素的出现,并点击它
151 | * @param linkText
152 | */
153 | public static void clickByLinkText(String linkText){
154 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
155 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.linkText(linkText)));
156 | targetElement.click();
157 | }
158 |
159 | /**
160 | * 通过元素的Xpath,等待元素的出现,并点击它
161 | * @param xpath
162 | */
163 | public static void clickByXpath(String xpath) {
164 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
165 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
166 | targetElement.click();
167 | }
168 |
169 | /**
170 | * 通过元素的Xpath,等待元素的出现,并点击它
171 | * @param css
172 | */
173 | public static void clickByCss(String css){
174 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
175 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(css)));
176 | targetElement.click();
177 | }
178 |
179 |
180 | /**
181 | * 通过元素的ID,等待元素的出现,并点击它
182 | * @param Id
183 | */
184 | public static void clickById(String Id){
185 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
186 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.id(Id)));
187 | targetElement.click();
188 | }
189 |
190 | /**
191 | * 通过元素的ClassName,等待元素的出现,并点击它
192 | * @param className
193 | */
194 | public static void clickByClassName(String className) {
195 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
196 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.className(className)));
197 | targetElement.click();
198 | }
199 |
200 | /**
201 | * 通过元素的Name,等待元素的出现,并点击它
202 | * @param name
203 | */
204 | public static void clickByName(String name) {
205 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
206 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.name(name)));
207 | targetElement.click();
208 | }
209 |
210 | /**
211 | * 通过元素的某个特定属性,等待元素的出现,并点击它
212 | * @param attribute
213 | * @param value
214 | * @return
215 | */
216 | public static void clickByAttribute(String attribute,String value){
217 | String xpath = "//*[@"+attribute+"='"+value+"']";
218 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
219 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
220 | targetElement.click();
221 | }
222 |
223 | /**
224 | * 通过html某个标签中间的text找到元素,并点击它
225 | * @param text
226 | */
227 | public static void clickByText(String text){
228 | String xpath = "//*[text()='"+text+"']";
229 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
230 | WebElement targetElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
231 | targetElement.click();
232 | }
233 |
234 | /**
235 | * 用javaScript执行点击事件
236 | * @param
237 | * @author guilin_cui
238 | */
239 | public static void clickByJS(WebElement ele){
240 | JavascriptExecutor js = (JavascriptExecutor)driver;
241 | js.executeScript("arguments[0].click();", ele);
242 | }
243 |
244 | /**
245 | * 用javaScript执行点击事件
246 | * @param by 传入定位
247 | */
248 | public static void clickByJSBy(By by){
249 | WebElement ele = driver.findElement(by);
250 | JavascriptExecutor js = (JavascriptExecutor)driver;
251 | js.executeScript("arguments[0].click();", ele);
252 | }
253 |
254 | /**
255 | * 通过元素的Xpath,等待文本框的出现,并输入
256 | */
257 | public static void sendKeysByXpath(String xpath,String keys) {
258 | WebElement targetElement = waitByXpath(xpath);
259 | targetElement.clear();
260 | targetElement.sendKeys(keys);
261 | }
262 |
263 | /**
264 | * 指定元素,再输入
265 | * @param element
266 | * @param keys
267 | */
268 | public static void sendKeysByEle(WebElement element,String keys){
269 | element.clear();
270 | element.sendKeys(keys);
271 | }
272 |
273 | /**
274 | * 通过元素的ID,等待文本框的出现,并输入
275 | */
276 | public static void sendKeysById(String Id,String keys) {
277 | WebElement targetElement = waitById(Id);
278 | targetElement.clear();
279 | targetElement.sendKeys(keys);
280 | }
281 |
282 | /**
283 | * 通过元素的name,等待文本框的出现,并输入
284 | */
285 | public static void sendKeysByName(String name,String keys) {
286 | WebElement targetElement = waitByName(name);
287 | targetElement.clear();
288 | targetElement.sendKeys(keys);
289 | }
290 |
291 | /**
292 | * 通过元素的ClassName,等待文本框的出现,并输入
293 | */
294 | public static void sendKeysByClassName(String className,String keys) {
295 | WebElement targetElement = waitByClassName(className);
296 | targetElement.clear();
297 | targetElement.sendKeys(keys);
298 | }
299 |
300 | /**
301 | * 通过元素的Name,等待文本框的出现,并输入
302 | */
303 | public static void SendKeysByName(String name,String keys){
304 | WebElement targetElement = waitByName(name);
305 | targetElement.clear();
306 | targetElement.sendKeys(keys);
307 | }
308 |
309 | /**
310 | * 用javaScript输入
311 | * @param
312 | * @author guilin_cui
313 | */
314 | public static void sendKesyByJS(WebElement ele,String keys){
315 | String str = "arguments[0].value='"+keys+"';";
316 | ele.clear();
317 | JavascriptExecutor js = (JavascriptExecutor)driver;
318 | js.executeScript(str, ele);
319 | }
320 |
321 | /**
322 | * 等待元素可见
323 | * @param ele
324 | * @param waitTime
325 | */
326 | public static void waitEleVisible(WebElement ele,int waitTime){
327 | WebDriverWait wait = new WebDriverWait(driver,waitTime);
328 | wait.until(ExpectedConditions.visibilityOf(ele));
329 | }
330 |
331 | /**
332 | * 等待元素可点击
333 | * @param ele
334 | * @param waitTime
335 | */
336 | public static void waitEleCanClickable(WebElement ele,int waitTime){
337 | WebDriverWait wait = new WebDriverWait(driver,waitTime);
338 | wait.until(ExpectedConditions.visibilityOf(ele));
339 | wait.until(ExpectedConditions.elementToBeClickable(ele));
340 | }
341 |
342 | /**
343 | * 拖动元素
344 | * @param element 元素
345 | * @param xOffset 拖到目标的横坐标位置,xOffset为正数时向右移动,为负数时向左移动
346 | * @param yOffset 拖到目标的纵坐标位置
347 | */
348 | public static void dragAndDropBy(WebElement element,int xOffset,int yOffset){
349 | Actions actions = new Actions(driver);
350 | actions.dragAndDropBy(element,xOffset,yOffset).build().perform();
351 | }
352 |
353 | /**
354 | * 拖动滚动条到目标元素的位置
355 | */
356 | public static void scrollIntoView(WebElement target){
357 | ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",target);
358 | }
359 |
360 | /**
361 | * 鼠标悬停
362 | * @param ele 元素
363 | * @param ms 悬停时间(毫秒)
364 | */
365 | public static void mouthHover(WebElement ele,int ms){
366 | Actions actions = new Actions(driver);
367 | actions.moveToElement(ele).perform();
368 | threadSleep(ms);
369 | }
370 |
371 | /**
372 | *鼠标点击并悬停
373 | */
374 | public static void clickAndHold(WebElement ele){
375 | Actions actions = new Actions(driver);
376 | actions.clickAndHold(ele).perform();
377 | }
378 |
379 | /**
380 | *双击
381 | */
382 | public static void doubleClick(WebElement ele){
383 | Actions builder = new Actions(driver);
384 | Actions hoverOverRgeistrar = builder.doubleClick(ele);
385 | hoverOverRgeistrar.perform();
386 | }
387 |
388 | /**
389 | * 判断元素是否存在
390 | * @param ele 等待的元素
391 | * @return boolean 是否存在
392 | */
393 | public static boolean isElementExsit(WebElement ele) {
394 | boolean flag ;
395 | try {
396 | WebDriverWait wait = new WebDriverWait(driver,timeOutInSeconds);
397 | WebElement element = wait.until(ExpectedConditions.visibilityOf(ele));
398 | flag = element.isDisplayed();
399 | } catch (Exception e) {
400 | flag = false;
401 | System.out.println("Element:" + ele.toString()
402 | + " is not exsit!");
403 | }
404 | return flag;
405 | }
406 |
407 | /**
408 | * 判断元素是否存在
409 | * @param locator 如:By.id("")
410 | * @return boolean 是否存在
411 | */
412 | public static boolean isElementExsitBy(By locator) {
413 | boolean flag = false;
414 | try {
415 | WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
416 | wait.until(ExpectedConditions.presenceOfElementLocated(locator));
417 | flag = true;
418 | } catch (NoSuchElementException e) {
419 | System.out.println("Element:" + locator.toString()
420 | + " is not exsit!");
421 | }
422 | return flag;
423 | }
424 |
425 | /**
426 | * 通过点击一个元素来切换窗口
427 | * @param element 被点击的元素
428 | */
429 | public static void switchToWindowByClick(WebElement element){
430 | String currentWindow = driver.getWindowHandle();
431 | element.click();
432 | threadSleep(1000);
433 | Set windowHandles = driver.getWindowHandles();
434 | //切换窗口
435 | for (String windowHandle:windowHandles){
436 | if(!windowHandle.equals(currentWindow)){
437 | driver.switchTo().window(windowHandle);
438 | }
439 | }
440 | }
441 |
442 | /**
443 | * 通过窗口的索引来切换窗口,index从0开始
444 | * @param index
445 | */
446 | public static void switchToWindowByIndex(int index){
447 | try {
448 | List windowHandles = new ArrayList(driver.getWindowHandles());
449 | driver.switchTo().window((String)windowHandles.get(index));
450 | } catch (IndexOutOfBoundsException var3) {
451 | var3.printStackTrace();
452 | }
453 | }
454 |
455 | /**
456 | * 通过窗口名称来切换窗口
457 | * @param windowTitleName 窗口名称
458 | */
459 | public static void switchToWindowHandle(String windowTitleName){
460 | Set handles = driver.getWindowHandles();
461 | List it = new ArrayList(handles);
462 | Iterator iterator = handles.iterator();
463 | String currentHandle=null;
464 | try{
465 | currentHandle=driver.getWindowHandle();
466 | }
467 | catch(NoSuchWindowException e){
468 | driver.switchTo().window(it.get(0));
469 | }
470 | while (iterator.hasNext()) {
471 | String h = (String) iterator.next();
472 | String currentTitle=driver.getTitle();
473 | if (currentTitle.contains(windowTitleName)){
474 | break;
475 | }
476 | if (h != currentHandle) {
477 | if (driver.switchTo().window(h).getTitle().contains(windowTitleName)) {
478 | driver.switchTo().window(h);
479 | System.out.println("switch to "+driver.getTitle()+" news page successfully");
480 | break;
481 | }
482 | }
483 | }
484 | }
485 |
486 | /**
487 | * 关闭除了第一个窗口外的其它窗口
488 | */
489 | public static void closeWindowHandleNotFirst(){
490 | Set windowHandles = driver.getWindowHandles();
491 | if (windowHandles.size()>1){
492 | for (int i=windowHandles.size()-1;i>0;i--){
493 | driver.close();
494 | switchToWindowByIndex(i-1);
495 | //System.out.println("切到window"+(i-1));
496 | }
497 | }
498 | }
499 |
500 | /**
501 | * 通过frame的ID切换iframe
502 | * @param
503 | * @author 灵枢
504 | */
505 | public static void switchToFrameById(String id){
506 | WebElement frame = driver.findElement(By.id(id));
507 | driver.switchTo().frame(frame);
508 | }
509 |
510 | /**
511 | * 切换iframe
512 | * @param
513 | * @author 灵枢
514 | */
515 | public static void switchToFrameByEle(WebElement iframe){
516 | driver.switchTo().frame(iframe);
517 | }
518 |
519 | /**
520 | * 接收alert
521 | * @param
522 | * @author 灵枢
523 | */
524 | public static void acceptAlert(){
525 | try {
526 | Alert alert = driver.switchTo().alert(); //接收alert
527 | alert.accept();
528 | } catch (Exception e) {
529 | }
530 | }
531 |
532 | /**
533 | * 等待alert出现
534 | * @param alertName
535 | * @return
536 | */
537 | public static Alert waitForAlert(String alertName){
538 | WebDriverWait wait = new WebDriverWait(driver, 10);
539 | Alert targetAlert = null;
540 | try{
541 | targetAlert = wait.until(ExpectedConditions.alertIsPresent());
542 | }catch(Exception e){
543 | assertTrue("没有找到目标元素--"+alertName,false);
544 | }
545 | return targetAlert;
546 | }
547 |
548 |
549 | /**
550 | * 打开新的窗口,并切换到该窗口
551 | * @param url 新窗口的url
552 | * @param windwoName 窗口title
553 | */
554 | public static void openWindow(String url,String windwoName){
555 | String js = "window.open(\""+url+"\")";
556 | ((JavascriptExecutor)driver).executeScript(js);
557 | switchToWindowHandle(windwoName);
558 | }
559 |
560 | /**
561 | * 线程等待
562 | * @param timeout 单位:毫秒
563 | */
564 | public static void threadSleep(int timeout){
565 | try {
566 | Thread.sleep(timeout);
567 | } catch (InterruptedException e) {
568 | // TODO Auto-generated catch block
569 | e.printStackTrace();
570 | }
571 | }
572 |
573 |
574 | /**
575 | * 打开url,一直等到页面的readyState为“complete",注意readyState=complete时并不意味着url全部加载完毕,只是这时我们的页面元素可以操作了
576 | * @param url
577 | */
578 | public static void getUrlUntilLoadComplete(String url){
579 | driver.get(url);
580 | for(int i=0;i<10;i++){
581 | if(((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete")){
582 | break;
583 | }
584 | threadSleep(1000);
585 | // System.out.println("readyState:"+((JavascriptExecutor)driver).executeScript("return document.readyState"));
586 | //如果等了10秒还没有加载完,停止加载,往后执行代码
587 | if(i==9){
588 | ((JavascriptExecutor)driver).executeScript("window.stop();");
589 | }
590 | }
591 | }
592 | }
593 |
594 |
--------------------------------------------------------------------------------
/src/test/java/common/TestngListener.java:
--------------------------------------------------------------------------------
1 | package test.java.common;
2 |
3 | import io.qameta.allure.Attachment;
4 | import org.openqa.selenium.OutputType;
5 | import org.openqa.selenium.TakesScreenshot;
6 | import org.openqa.selenium.WebDriver;
7 | import org.testng.ITestContext;
8 | import org.testng.ITestResult;
9 | import org.testng.Reporter;
10 | import org.testng.TestListenerAdapter;
11 | import test.java.utils.ExcelData;
12 |
13 | import java.io.IOException;
14 | import java.util.*;
15 |
16 | public class TestngListener extends TestListenerAdapter {
17 |
18 | public void onTestFailure(ITestResult tr) {
19 |
20 | super.onTestFailure(tr);
21 | try {
22 | BaseTest bt = (BaseTest) tr.getInstance();
23 | //获取webdriver实例
24 | WebDriver driver = bt.getDriver();
25 | takeScreenShot(driver);
26 | logCaseStep(tr);
27 | logExceptedResult(tr);
28 | } catch (Exception e) {
29 | e.printStackTrace();
30 | }
31 | }
32 |
33 |
34 | /**
35 | * 获取用例失败截图
36 | * @param driver
37 | * @throws IOException
38 | */
39 | @Attachment(value = "失败截图",type = "image/png")
40 | public byte[] takeScreenShot(WebDriver driver){
41 | byte[] screenshotAs = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
42 | return screenshotAs;
43 | }
44 |
45 | /**
46 | * 测试步骤
47 | * @param tr
48 | */
49 | @Attachment(value = "操作步骤")
50 | public String logCaseStep(ITestResult tr){
51 | String filePath = "resource/自动化测试用例.xlsx";
52 | ExcelData sheet1 = new ExcelData(filePath, "搜索");
53 | return sheet1.getCellByCaseName(tr.getName(), 4, 6);
54 | }
55 |
56 | /**
57 | * 期望结果
58 | * @param tr
59 | */
60 | @Attachment(value = "期望结果")
61 | public String logExceptedResult(ITestResult tr){
62 | String filePath = "resource/自动化测试用例.xlsx";
63 | ExcelData sheet1 = new ExcelData(filePath, "搜索");
64 | return sheet1.getCellByCaseName(tr.getName(), 4, 7);
65 | }
66 |
67 |
68 | //------------------------以下是失败用例重跑相关代码----------------------------------
69 | public void onFinish(ITestContext testContext) {
70 | super.onFinish(testContext);
71 |
72 | // List of test results which we will delete later
73 | ArrayList testsToBeRemoved = new ArrayList();
74 | // collect all id's from passed test
75 | Set passedTestIds = new HashSet();
76 | for (ITestResult passedTest : testContext.getPassedTests()
77 | .getAllResults()) {
78 | // logger.info("PassedTests = " + passedTest.getName());
79 | passedTestIds.add(getId(passedTest));
80 | }
81 |
82 | Set failedTestIds = new HashSet();
83 | for (ITestResult failedTest : testContext.getFailedTests()
84 | .getAllResults()) {
85 | // logger.info("failedTest = " + failedTest.getName());
86 | int failedTestId = getId(failedTest);
87 |
88 | // if we saw this test as a failed test before we mark as to be
89 | // deleted
90 | // or delete this failed test if there is at least one passed
91 | // version
92 | if (failedTestIds.contains(failedTestId)
93 | || passedTestIds.contains(failedTestId)) {
94 | testsToBeRemoved.add(failedTest);
95 | } else {
96 | failedTestIds.add(failedTestId);
97 | }
98 | }
99 |
100 | // finally delete all tests that are marked
101 | for (Iterator iterator = testContext.getFailedTests()
102 | .getAllResults().iterator(); iterator.hasNext();) {
103 | ITestResult testResult = iterator.next();
104 | if (testsToBeRemoved.contains(testResult)) {
105 | // logger.info("Remove repeat Fail Test: " + testResult.getName());
106 | iterator.remove();
107 | }
108 | }
109 |
110 | }
111 |
112 | private int getId(ITestResult result) {
113 | int id = result.getTestClass().getName().hashCode();
114 | id = id + result.getMethod().getMethodName().hashCode();
115 | id = id
116 | + (result.getParameters() != null ? Arrays.hashCode(result
117 | .getParameters()) : 0);
118 | return id;
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/src/test/java/pageobject/BaiduPO.java:
--------------------------------------------------------------------------------
1 | package test.java.pageobject;
2 |
3 | import org.openqa.selenium.WebElement;
4 | import test.java.common.OperateElement;
5 |
6 | /**
7 | * Author: 灵枢
8 | * Date: 2019/6/12
9 | * Time: 17:40
10 | * Description: 百度搜索元素及操作封装
11 | */
12 | public class BaiduPO {
13 |
14 | /**
15 | * 搜索框
16 | * @return
17 | */
18 | public static WebElement searchInput(){
19 | return OperateElement.waitById("kw");
20 | }
21 |
22 | /**
23 | * 百度一下 按钮
24 | * @return
25 | */
26 | public static WebElement searchBtn(){
27 | return OperateElement.waitById("su");
28 | }
29 |
30 | /**
31 | * 搜索结果的第几行
32 | * @param row 第几行
33 | * @return
34 | */
35 | public static WebElement searchResult(int row){
36 | String xpath = "//*[@id='"+row+"']/h3/a";
37 | return OperateElement.waitByXpath(xpath);
38 | }
39 |
40 | /**
41 | * 搜索操作
42 | * @param keywords
43 | */
44 | public static void search(String keywords){
45 | searchInput().clear();
46 | searchInput().sendKeys(keywords);
47 | searchBtn().click();
48 | OperateElement.threadSleep(2000);
49 | }
50 |
51 | /**
52 | * 使用用户名登录
53 | * @param username
54 | * @param password
55 | */
56 | public static void login(String username,String password){
57 | OperateElement.clickByXpath("//p[text()='用户名登录']");
58 | OperateElement.sendKeysById("TANGRAM__PSP_10__userName",username);
59 | OperateElement.sendKeysById("TANGRAM__PSP_10__password",password);
60 | OperateElement.clickById("TANGRAM__PSP_10__submit");
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/test/java/pageobject/SogouPO.java:
--------------------------------------------------------------------------------
1 | package test.java.pageobject;
2 |
3 | import org.openqa.selenium.WebElement;
4 | import test.java.common.OperateElement;
5 |
6 | /**
7 | * Author: 灵枢
8 | * Date: 2019/6/12
9 | * Time: 18:09
10 | * Description:搜狗搜索页面封装
11 | */
12 | public class SogouPO {
13 |
14 | /**
15 | * 搜索框
16 | * @return
17 | */
18 | public static WebElement searchInput(){
19 | return OperateElement.waitById("query");
20 | }
21 |
22 | /**
23 | * 搜索按钮
24 | * @return
25 | */
26 | public static WebElement searchBtn(){
27 | return OperateElement.waitById("stb");
28 | }
29 |
30 | /**
31 | * 搜索操作封装
32 | * @param keywords
33 | */
34 | public static void search(String keywords){
35 | searchInput().clear();
36 | searchInput().sendKeys(keywords);
37 | searchBtn().click();
38 | OperateElement.threadSleep(2000);
39 | }
40 |
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/utils/AutoITUtil.java:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.openqa.selenium.WebDriver;
4 | import test.java.common.OperateElement;
5 |
6 | import java.io.IOException;
7 |
8 | /**
9 | * Author: 灵枢
10 | * Date: 2019/1/16
11 | * Time: 13:58
12 | * Description:AutoIT方法封装
13 | */
14 | public class AutoITUtil {
15 | /**
16 | * 使用AutoIT上传图片
17 | * @param fileName 文件名
18 | * @param driver
19 | */
20 | public static void uploadPic(String fileName,WebDriver driver){
21 | String path = "resource/"+fileName;
22 | Runtime runtime = Runtime.getRuntime();
23 | try {
24 | runtime.exec(path);
25 | } catch (IOException e) {
26 | e.printStackTrace();
27 | }
28 | OperateElement.threadSleep(1500);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/test/java/utils/DBUtil.java:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.SQLException;
6 | import java.sql.Statement;
7 |
8 | public class DBUtil {
9 |
10 | //设置数据库名称
11 | private final static String URL = "jdbc:sqlserver://*********;DatabaseName=Star-Notify";
12 | private static final String USER="****";
13 | private static final String PASSWORD="****";
14 |
15 | private static Connection conn=null;
16 | //静态代码块(将加载驱动、连接数据库放入静态块中)
17 | static{
18 | try {
19 | //1.加载驱动程序
20 | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
21 | //2.获得数据库的连接
22 | conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
23 | } catch (ClassNotFoundException e) {
24 | e.printStackTrace();
25 | } catch (SQLException e) {
26 | e.printStackTrace();
27 | }
28 | }
29 |
30 | //对外提供一个方法来获取数据库连接
31 | public Connection getConnection(){
32 | return conn;
33 | }
34 |
35 |
36 | /**
37 | * 执行一条或多条sql,但是查询和删除不要同时进行
38 | * @param sqlArr
39 | */
40 | public static void executeSql(String[] sqlArr){
41 | DBUtil dbUtil = new DBUtil();
42 | try {
43 | Connection conn = dbUtil.getConnection();
44 | Statement stmt = conn.createStatement();
45 | for(int i=0;i getCellList(int columnIndex){
72 | ArrayList list = new ArrayList<>();
73 | int rows = sheetAt.getPhysicalNumberOfRows();
74 | for(int i=1;i idRegion = new HashMap<>();
18 |
19 | public static void main(String[] args) {
20 | IdCardGeneration g = new IdCardGeneration();
21 | /*for (int i = 0; i < 10; i++) {
22 | System.out.print(g.generate());
23 | System.out.print("\t");
24 | System.out.print(g.generate());
25 | System.out.print("\t");
26 | System.out.print(g.generate());
27 | System.out.print("\t");
28 | System.out.println(g.generate());
29 | }*/
30 | System.out.println(g.generate());
31 | }
32 |
33 | static{
34 | IdCardGeneration.idRegion.put("四川省", 510000);
35 | IdCardGeneration.idRegion.put("成都市", 510100);
36 | IdCardGeneration.idRegion.put("市辖区", 510101);
37 | IdCardGeneration.idRegion.put("锦江区", 510104);
38 | IdCardGeneration.idRegion.put("青羊区", 510105);
39 | IdCardGeneration.idRegion.put("金牛区", 510106);
40 | IdCardGeneration.idRegion.put("武侯区", 510107);
41 | IdCardGeneration.idRegion.put("成华区", 510108);
42 | IdCardGeneration.idRegion.put("龙泉驿区", 510112);
43 | IdCardGeneration.idRegion.put("青白江区", 510113);
44 | IdCardGeneration.idRegion.put("新都区", 510114);
45 | IdCardGeneration.idRegion.put("重庆市", 500000);
46 | IdCardGeneration.idRegion.put("市辖区", 500100);
47 | IdCardGeneration.idRegion.put("万州区", 500101);
48 | IdCardGeneration.idRegion.put("涪陵区", 500102);
49 | IdCardGeneration.idRegion.put("渝中区", 500103);
50 | IdCardGeneration.idRegion.put("大渡口区", 500104);
51 | IdCardGeneration.idRegion.put("江北区", 500105);
52 | IdCardGeneration.idRegion.put("沙坪坝区", 500106);
53 | IdCardGeneration.idRegion.put("九龙坡区", 500107);
54 | IdCardGeneration.idRegion.put("南岸区", 500108);
55 | IdCardGeneration.idRegion.put("北碚区", 500109);
56 | IdCardGeneration.idRegion.put("万盛区", 500110);
57 | IdCardGeneration.idRegion.put("双桥区", 500111);
58 | IdCardGeneration.idRegion.put("渝北区", 500112);
59 | IdCardGeneration.idRegion.put("巴南区", 500113);
60 | IdCardGeneration.idRegion.put("黔江区", 500114);
61 | IdCardGeneration.idRegion.put("长寿区", 500115);
62 | IdCardGeneration.idRegion.put("江津区", 500116);
63 | IdCardGeneration.idRegion.put("合川区", 500117);
64 | IdCardGeneration.idRegion.put("永川区", 500118);
65 | IdCardGeneration.idRegion.put("南川区", 500119);
66 | }
67 |
68 | /**
69 | * 生成身份证号码
70 | * @return
71 | */
72 | public String generate() {
73 | StringBuilder generater = new StringBuilder();
74 | generater.append(this.getRandomArea());
75 | generater.append(this.getRandomBirthday());
76 | generater.append(this.getRandomNumber());
77 | generater.append(this.getTestCode(generater.toString().toCharArray()));
78 | return generater.toString();
79 | }
80 |
81 | /**
82 | * 随机获取地区
83 | *
84 | * @return
85 | */
86 | public int getRandomArea() {
87 | int index = (int) (Math.random() * IdCardGeneration.idRegion.size());
88 | Collection values = IdCardGeneration.idRegion.values();
89 | Iterator it = values.iterator();
90 | int i = 0;
91 | int code = 0;
92 | while (i < index && it.hasNext()) {
93 | i++;
94 | code = it.next();
95 | }
96 | return code;
97 | }
98 |
99 | /**
100 | * 随机获取出生日期
101 | *
102 | * @return
103 | */
104 | public String getRandomBirthday() {
105 | Calendar birthday = Calendar.getInstance();
106 | birthday.set(Calendar.YEAR, (int) (Math.random() * 20) + 1985);
107 | birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
108 | birthday.set(Calendar.DATE, (int) (Math.random() * 31));
109 |
110 | StringBuilder builder = new StringBuilder();
111 | builder.append(birthday.get(Calendar.YEAR));
112 | long month = birthday.get(Calendar.MONTH) + 1;
113 | if (month < 10) {
114 | builder.append("0");
115 | }
116 | builder.append(month);
117 | long date = birthday.get(Calendar.DATE);
118 | if (date < 10) {
119 | builder.append("0");
120 | }
121 | builder.append(date);
122 | return builder.toString();
123 | }
124 |
125 |
126 |
127 | /**
128 | * 获取效验码
129 | * @param chars
130 | * @return
131 | */
132 | public char getTestCode(char[] chars) {
133 | if (chars.length < 17) {
134 | return ' ';
135 | }
136 | int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
137 | char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
138 | int[] n = new int[17];
139 | int result = 0;
140 | for (int i = 0; i < n.length; i++) {
141 | n[i] = Integer.parseInt(chars[i] + "");
142 | }
143 | for (int i = 0; i < n.length; i++) {
144 | result += c[i] * n[i];
145 | }
146 | return r[result % 11];
147 | }
148 |
149 | /**
150 | *获取3位随机数
151 | * @return
152 | */
153 | public String getRandomNumber() {
154 | int code = (int) (Math.random() * 1000);
155 | if (code < 10) {
156 | return "00" + code;
157 | } else if (code < 100) {
158 | return "0" + code;
159 | } else {
160 | return "" + code;
161 | }
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/src/test/java/utils/Log4jTest.java:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import org.apache.log4j.Logger;
4 |
5 | /**
6 | * Created by pc on 2018/6/26.
7 | */
8 | public class Log4jTest {
9 | private static Logger logger = Logger.getLogger(Log4jTest.class);
10 |
11 | /**
12 | * @param args
13 | */
14 | public static void main(String[] args) {
15 | // System.out.println("This is println message.");
16 |
17 | // 记录debug级别的信息
18 | logger.debug("This is debug message.");
19 | // 记录info级别的信息
20 | logger.info("This is info message.");
21 | // 记录error级别的信息
22 | logger.error("This is error message.");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/test/java/utils/ReadCsvFile.java:
--------------------------------------------------------------------------------
1 | package test.java.utils;
2 |
3 | import com.csvreader.CsvReader;
4 |
5 | import java.nio.charset.Charset;
6 | import java.util.ArrayList;
7 |
8 | /*
9 | *作者:Adien_cui
10 | *时间:2017-9-25 下午4:36:29
11 | *描述:读取csv文件
12 | **/
13 | public class ReadCsvFile {
14 | private static ArrayList csvList;
15 |
16 | /**
17 | * 读取csv文件,并存入list集合
18 | * @param filePath
19 | */
20 | public static void readCsvFile(String filePath){
21 | try {
22 | csvList = new ArrayList();
23 | // CsvReader reader = new CsvReader(filePath,',',Charset.forName("SJIS"));
24 | CsvReader reader = new CsvReader(filePath,',',Charset.forName("GBK"));
25 | // reader.readHeaders(); //跳过表头,不跳可以注释掉
26 |
27 | while(reader.readRecord()){
28 | csvList.add(reader.getValues()); //按行读取,并把每一行的数据添加到list集合
29 | }
30 | reader.close();
31 | // System.out.println("读取的行数:"+csvList.size());
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 | /**
38 | * 读取测试用例的描述
39 | * @param filePath csv文件路径
40 | * @param caseName 用例名
41 | * @return
42 | */
43 | public static String getCaseDescription(String filePath,String caseName){
44 | readCsvFile(filePath);
45 | for(int row=0;row
16 | * 邮箱:404110318@qq.com
17 | *
18 | * 描述:对xml文件的读写
19 | */
20 | public class XmlUtils {
21 |
22 | private static SAXReader reader = new SAXReader();// 创建解析器
23 |
24 | public static String readXml(String addr,String elementName){
25 | String elementValue = null;
26 | try {
27 | Document doc = reader.read(addr);
28 | Element rootElement = doc.getRootElement();
29 | List elements = rootElement.elements();
30 | for (Element element : elements) {
31 | if (element.getName().equals(elementName)) {
32 | elementValue = element.getText();
33 | // System.out.println(elementName + ":" + elementValue);
34 | }
35 | }
36 |
37 | }catch (Exception e) {
38 | e.printStackTrace();
39 | }
40 | return elementValue;
41 | }
42 |
43 | public static void writeXml(String addr,String elementName,String elementValue){
44 |
45 | try {
46 | // 调用读方法,得到Document
47 | Document doc = reader.read(addr);
48 | //得到根元素
49 | Element rootElement = doc.getRootElement();
50 | //向根元素中添加一个名为elementName的元素!并返回这个元素
51 | Element ele = rootElement.addElement(elementName);
52 |
53 | //给该元素添加值
54 | ele.setText(elementValue);
55 |
56 | // 创建目标输出流,它需要与xml文件绑定
57 | Writer out = new PrintWriter(addr,"UTF-8");
58 | // 创建格式化器
59 | OutputFormat format = new OutputFormat("\t",true);
60 | format.setTrimText(true);//先干掉原来的空白(\t和换行和空格)!
61 |
62 | // 创建XMLWrtier
63 | XMLWriter writer = new XMLWriter(out,format);
64 | // 调用它的写方法,把document对象写到out流中。
65 | writer.write(doc);
66 | // 关闭流
67 | out.close();
68 | writer.close();
69 | }catch (Exception e) {
70 | e.printStackTrace();
71 | }
72 |
73 | }
74 |
75 | public static void removeXml(String addr,String elementName){
76 |
77 | try {
78 | // 调用读方法,得到Document
79 | Document doc = reader.read(addr);
80 | //得到根元素
81 | Element rootElement = doc.getRootElement();
82 | //移除elementName元素
83 | List elements = rootElement.elements();
84 | for (Element element : elements) {
85 | if (element.getName().equals(elementName)) {
86 | rootElement.remove(element);
87 | }
88 | }
89 |
90 | // 创建目标输出流,它需要与xml文件绑定
91 | Writer out = new PrintWriter(addr,"UTF-8");
92 | // 创建格式化器
93 | OutputFormat format = new OutputFormat("\t",true);
94 | format.setTrimText(true);//先干掉原来的空白(\t和换行和空格)!
95 |
96 | // 创建XMLWrtier
97 | XMLWriter writer = new XMLWriter(out,format);
98 | // 调用它的写方法,把document对象写到out流中。
99 | writer.write(doc);
100 | // 关闭流
101 | out.close();
102 | writer.close();
103 | }catch (Exception e) {
104 | e.printStackTrace();
105 | }
106 |
107 | }
108 |
109 | /**
110 | * 删除根节点外的所有其它节点
111 | * @param addr
112 | */
113 | public static void removeAll(String addr){
114 |
115 | try {
116 | // 调用读方法,得到Document
117 | Document doc = reader.read(addr);
118 | //得到根元素
119 | Element rootElement = doc.getRootElement();
120 | //移除elementName元素
121 | List elements = rootElement.elements();
122 | for (Element element : elements) {
123 | rootElement.remove(element);
124 | }
125 | // 创建目标输出流,它需要与xml文件绑定
126 | Writer out = new PrintWriter(addr,"UTF-8");
127 | // 创建格式化器
128 | OutputFormat format = new OutputFormat("\t",true);
129 | format.setTrimText(true);//先干掉原来的空白(\t和换行和空格)!
130 |
131 | // 创建XMLWrtier
132 | XMLWriter writer = new XMLWriter(out,format);
133 | // 调用它的写方法,把document对象写到out流中。
134 | writer.write(doc);
135 | // 关闭流
136 | out.close();
137 | writer.close();
138 | }catch (Exception e) {
139 | e.printStackTrace();
140 | }
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/testng.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------