Selenium脚本的录制和导出

本篇文章将会介绍Eclipse中Junit与Eclemma插件安装与使用

完整代码

一、打开Selenium IDE附加组件

  • 选择较老版本的Firefox浏览器到应用商店下载Selenium IDE,安装后打开如图所示:

    img

  • 配置Selenium WebDriver环境

    向工程内导入如下的jar包(可以准备一个javacsv来辅助后面的实验)

img

  • 录制脚本

    使用Selenium IDE录制脚本后可以很方便的导出,可以直接在导出的代码上进行修改。

    打开Selenium IDE

img

    点击右上角的红点开始录制

    进入网页输入账号密码登陆,其他网站类似

img

    点击登录跳转后,选择页面内的github地址,右键选择assertText

img

    再次点击红点完成录制

  • 导出脚本

    点击Optinons->Options,勾选Enable experimental features

    通过Options–>Format选择要导出成的相应格式。

    选择Java / JUnit 4/ WebDriver

    得到生成的Java文件,复制到工程中完成脚本导出

  • 编写完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.WebDriveTest;

import java.util.regex.Pattern;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.csvreader.CsvReader;

@RunWith(Parameterized.class)
public class WebDriveTest {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private String id, pwd,address;

public WebDriveTest(String id, String address)
{
this.id = id;
this.pwd = id.substring(4);
this.address = address;
}

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://121.193.130.195:8080";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Parameters
public static Collection<Object[]> getData() throws IOException {
Object[][] obj = new Object[118][];
CsvReader r = new CsvReader("D:\\CIA\\大三下\\软测\\inputgit.csv", ',',
Charset.forName("GBK"));
int count = 0;
r.readHeaders();
while(r.readRecord()){
obj[count] = new Object[]{r.get(0), r.get(2)};
count++;
}
return Arrays.asList(obj);
}

@Test
public void testUntitled2() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("name")).clear();
driver.findElement(By.id("name")).sendKeys(this.id);
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys(this.pwd);
driver.findElement(By.id("submit")).click();
assertEquals(this.address, driver.findElement(By.xpath("//tbody[@id='table-main']/tr[3]/td[2]")).getText());
}

@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}

private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}

private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
  • 实验结果

    img