l Lab05项目为实验5用到的项目,在大家的班级QQ群中,数据库采用MySQL,数据库文件在项目根目录下CreateDataBase.txt
第一步 在login.jsp登陆页面加上id属性值
<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head><title>用户登录</title></head>
<body>
<form action="validate.jsp" method="post">
<div>
用户登录<br><br>
用户名:<input type="text"><br><br>
密 码:<input type="text"><br><br>
<input type="submit" value="登录">
</div>
</form>
</body>
</html>
2、在Mysql数据库中添加test表和测试用例
第二步:往数据库添加自己的账户数据
Create Table user
(
id int auto_increment not null,
username varchar(8) not null,
password varchar(6) not null,
primary key(id)
);
insert into user values(1,\'lala\',\'123456\');
3、测试代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class cc {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "E:\\Program Files\\Mozilla Firefox\\firefox.exe");
//打开火狐浏览器
WebDriver driver = new FirefoxDriver();
//打开要测试的页面
demo.get("http://localhost:8080/jsp_jdbc/login.jsp");
//设置等待超出的时间(100秒)
WebDriverWait wait = new WebDriverWait(driver, 100);
//找到用户名输入框,并自动输入用户名
WebElement txtSearchBox =demo.findElement(By.name("un")).sendKeys("ccj");
//找到密码输入框,并自动输入密码
WebElement txtSearchBox =demo.findElement(By.name("pw")).sendKeys("ccj");
//找到登陆按钮,并点击它
WebElement btn = demo.findElement(By.id("la")).click();
//关闭浏览器
//driver.close();
}
}
在火狐浏览器上安装SeleniumIDE,并录制,回放录制的步骤,将脚本转换成Java代码
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
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;
public class bb {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.firefox.bin", "E:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
baseUrl = "http://localhost:8080/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/jsp_jdbc/login.jsp");
driver.findElement(By.id("user")).clear();
driver.findElement(By.id("user")).sendKeys("lala");
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys("123456");
driver.findElement(By.id("la")).click();
}
@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;