关闭

使用Selenium IDE进行自动化测试

发表于:2016-4-20 10:35

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:F.永恒    来源:51Testing软件测试网采编

  (5)在文件夹里找到seleniumSuite.java,其内容如下,稍加修改就可作为JUnit测试类。修改后的代码https://github.com/yongheng20/SeleniumJavaWebDriver
1 package com.example.tests;
2
3 import java.util.regex.Pattern;
4 import java.util.concurrent.TimeUnit;
5 import org.junit.*;
6 import static org.junit.Assert.*;
7 import static org.hamcrest.CoreMatchers.*;
8 import org.openqa.selenium.*;
9 import org.openqa.selenium.firefox.FirefoxDriver;
10 import org.openqa.selenium.support.ui.Select;
11
12 public class SeleniumSuite {
13   private WebDriver driver;
14   private String baseUrl;
15   private boolean acceptNextAlert = true;
16   private StringBuffer verificationErrors = new StringBuffer();
17
18   @Before
19   public void setUp() throws Exception {
20     driver = new FirefoxDriver();
21     baseUrl = "http://www.ncfxy.com/";
22     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
23   }
24
25   @Test
26   public void testSeleniumSuite() throws Exception {
27     driver.get(baseUrl + "/");
28     driver.findElement(By.id("name")).clear();
29     driver.findElement(By.id("name")).sendKeys("0123456789");
30     driver.findElement(By.id("pwd")).clear();
31     driver.findElement(By.id("pwd")).sendKeys("000000");
32     driver.findElement(By.id("submit")).click();
33   }
34
35   @After
36   public void tearDown() throws Exception {
37     driver.quit();
38     String verificationErrorString = verificationErrors.toString();
39     if (!"".equals(verificationErrorString)) {
40       fail(verificationErrorString);
41     }
42   }
43
44   private boolean isElementPresent(By by) {
45     try {
46       driver.findElement(by);
47       return true;
48     } catch (NoSuchElementException e) {
49       return false;
50     }
51   }
52
53   private boolean isAlertPresent() {
54     try {
55       driver.switchTo().alert();
56       return true;
57     } catch (NoAlertPresentException e) {
58       return false;
59     }
60   }
61
62   private String closeAlertAndGetItsText() {
63     try {
64       Alert alert = driver.switchTo().alert();
65       String alertText = alert.getText();
66       if (acceptNextAlert) {
67         alert.accept();
68       } else {
69         alert.dismiss();
70       }
71       return alertText;
72     } finally {
73       acceptNextAlert = true;
74     }
75   }
76 }
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号