测试代码
package LoginPage;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginPageTest extends InitAndEnd{
/**
* 已注册的用户+正确密码
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "TestLO001.csv")
void TestLO001(String login_url,String username, String password, String client_url, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入账号admin
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入密码admin
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(3000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "登录成功!"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 跳转到列表页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/client.html,测试通过,否则测试不通过
Assertions.assertEquals(client_url, cur_url);
// 列表页展示用户信息是admin
// 用户名是admin测试通过,否则测试不通过
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String cur_admin = webDriver.findElement(By.cssSelector("body > div.client-container > div.main > div.left > div.user")).getText();
Assertions.assertEquals(username, cur_admin);
}
/**
* 已注册的用户+错误密码
*/
@Order(2)
@ParameterizedTest
@CsvFileSource(resources = "TestLO002.csv")
void TestLO002(String login_url,String username, String password, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入账号admin
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入错误密码123456
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(2000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "登录失败!用户名或者密码错误"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 不转跳,页面停留在登录页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/login.html,测试通过,否则测试不通过
Assertions.assertEquals(login_url, cur_url);
}
/**
* 未注册的用户登录
*/
@Order(3)
@ParameterizedTest
@CsvFileSource(resources = "TestLO003.csv")
void TestLO003(String login_url,String username, String password, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入不存在的账号admin2
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入密码admin
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(2000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "登录失败!用户名或者密码错误"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 不转跳,页面停留在登录页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/login.html,测试通过,否则测试不通过
Assertions.assertEquals(login_url, cur_url);
}
/**
* 用户名和密码都为空登录
*/
@Order(4)
@ParameterizedTest
@CsvFileSource(resources = "TestLO004.csv")
void TestLO004(String login_url, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(2000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "当前用户名或者密码为空!"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 不转跳,页面停留在登录页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/login.html,测试通过,否则测试不通过
Assertions.assertEquals(login_url, cur_url);
}
/**
* 用户名为空登录
*/
@Order(5)
@ParameterizedTest
@CsvFileSource(resources = "TestLO005.csv")
void TestLO005(String login_url,String password, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 不输入用户名
// 输入密码admin
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(2000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "当前用户名或者密码为空!"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 不转跳,页面停留在登录页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/login.html,测试通过,否则测试不通过
Assertions.assertEquals(login_url, cur_url);
}
/**
* 密码为空登录
*/
@Order(6)
@ParameterizedTest
@CsvFileSource(resources = "TestLO006.csv")
void TestLO006(String login_url,String username, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入账号admin
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 不输入密码
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(2000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "当前用户名或者密码为空!"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 不转跳,页面停留在登录页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/login.html,测试通过,否则测试不通过
Assertions.assertEquals(login_url, cur_url);
}
/**
* 用户名和密码是否大小写敏感
*/
@Order(7)
@ParameterizedTest
@CsvFileSource(resources = "TestLO007.csv")
void TestLO007(String login_url,String username, String password, String alert_text) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入账号ADMIN
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入密码ADMIN
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//切换到alert,获取alert
sleep(2000);
Alert alert = webDriver.switchTo().alert();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String alertText = alert.getText();
//alertText = "登录失败!用户名或者密码错误"测试通过,否则不通过
Assertions.assertEquals(alert_text, alertText);
alert.accept();
// 不转跳,页面停留在登录页
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/login.html,测试通过,否则测试不通过
Assertions.assertEquals(login_url, cur_url);
}
/**
* 点击注册按钮
*/
@Order(8)
@ParameterizedTest
@CsvFileSource(resources = "TestLO008.csv")
void TestLO008(String login_url, String register_url) throws InterruptedException {
// 打开登录页面
webDriver.get(login_url);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击注册按钮
webDriver.findElement(By.cssSelector("#register")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 获取到当前页面url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 如果url=http://139.196.92.170:8080/register.html,测试通过,否则测试不通过
Assertions.assertEquals(register_url, cur_url);
}
}
测试数据(.csv)
TestLO001.csv
http://139.196.92.170:8080/login.html,admin,admin,http://139.196.92.170:8080/client.html,登录成功!
TestLO002.csv
http://139.196.92.170:8080/login.html,admin,123456,登录失败!用户名或者密码错误
TestLO003.csv
http://139.196.92.170:8080/login.html,admin2,admin,登录失败!用户名或者密码错误
TestLO004.csv
http://139.196.92.170:8080/login.html,当前用户名或者密码为空!
TestLO005.csv
http://139.196.92.170:8080/login.html,admin,当前用户名或者密码为空!
TestLO006.csv
http://139.196.92.170:8080/login.html,admin,当前用户名或者密码为空!
TestLO007.csv
http://139.196.92.170:8080/login.html,ADMIN,ADMIN,登录失败!用户名或者密码错误
TestLO008.csv
http://139.196.92.170:8080/login.html,http://139.196.92.170:8080/register.html