商城系统——项目测试
1.测试用例
从功能测试+性能测试+界面测试+兼容性测试+易用测试+安全测试去完成测试用例。
2.功能测试
以注册功能为例:
注册:注册功能通过等价划分法,边界值分析,正交法去设计测试用例,高效覆盖多因素组合场景,用最少的测试用例覆盖尽可能多的参数组合。
注册测试用例1:
都不输入的情况下点击注册按钮
预期结果:弹出对应的提示信息
注册测试用例2:
用户名输入1个字符的情况下
预期结果:弹出对应的提示信息
注册测试用例3:
用户名输入2个字符的情况下
预期结果:无提示信息,点击注册后光标自动来到登录密码框
注册测试用例4:
用户名输入18个字符的情况下
预期结果:无提示信息,点击注册后光标自动来到登录密码框
注册测试用例5:
用户名输入19个字符的情况下
预期结果:弹出对应的提示信息
注册测试用例6:
密码输入5个字符的情况下
预期结果:弹出对应的提示信息
注册测试用例7:
密码输入6个字符的情况下
预期结果:无提示信息,点击注册后光标自动来到验证码框
注册测试用例8:
密码输入18个字符的情况下
预期结果:无提示信息,点击注册后光标自动来到验证码框
注册测试用例9:
密码输入19个字符的情况下
预期结果:弹出对应的提示信息
注册测试用例10:
正确填写用户名,不填写密码+验证码
预期结果:弹出对应的提示信息
注册测试用例11:
正确填写密码,不填写用户名+验证码
预期结果:弹出对应的提示信息
注册测试用例12:
正确填写验证码,不填写用户名+密码
预期结果:弹出对应的提示信息
3.界面测试
以首页模块为例:
布局大小合理,列间距一致。
图片清晰无模糊,自适应不同屏幕和设备。
页面下滑流畅,分类明确,无错别字。
交互元素如搜索框和导航栏始终置顶,便于使用。
点击图片可跳转至详情页,功能完整。在这里插入图片描述
4.自动化测试
引入依赖:
<dependencies><dependency><groupId>io.github.bonigarcia</groupId><artifactId>webdrivermanager</artifactId><version>5.8.0</version><scope>test</scope></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.0.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency></dependencies>
Utils类
public class Utils {public static WebDriver driver;public static WebDriver CommonDriver(){if(driver==null){System.setProperty("webdriver.edge.driver","D:/cs/edgedriver_win64/msedgedriver.exe");EdgeOptions options = new EdgeOptions();options.addArguments("--remote-allow-origins=*");driver=new EdgeDriver(options);//隐式等待driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(4));}return driver;}public static WebDriverWait wait = null;public Utils(String Url) throws IOException {CommonDriver();driver.get(Url);wait = new WebDriverWait(driver, Duration.ofSeconds(10));}/* public Utils(String url) {//调用driver对象CommonDriver();driver.get(url);WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));}
*///屏幕截图进阶public void CommonScreenshot(String str) throws IOException {SimpleDateFormat sim1 = new SimpleDateFormat("yyy-MM-dd");SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");String dirTime = sim1.format(System.currentTimeMillis());String fileTime = sim2.format(System.currentTimeMillis());String filename = "./src/test/image" + dirTime + "/" + str + "-" + fileTime + ".png";System.out.println("filename:"+filename);File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(srcFile, new File(filename));}public static void CommonQuit() {//退出浏览器if (driver != null) {driver.quit();}}
以注册页为例:
package tests;import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;import java.io.IOException;
import java.time.Instant;public class RegistrationRageTest extends Utils {public static String url = "http://8.155.1.153/?s=user/regInfo.html";public RegistrationRageTest() throws IOException {super(url);}//异常注册//都不输入的情况下public void AbnormalRegistrationPage() throws IOException {//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());// 等待提示框消失的组合策略// 首先等待提示框出现WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg']")));// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//用户名输入1个字符的情况下public void AbnormalRegistrationPage1() throws IOException {//点击用户输入框并输入WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.click();userName.sendKeys("l");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='请使用字母、数字、下划线2~18个字符']")));String text1 = prompt.getText();assert text1.contains("请使用字母、数字、下划线2~18个字符");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//输入2个字符的情况下,无错误提示public void AbnormalRegistrationPage2() throws InterruptedException, IOException {//点击用户输入框并输入WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.sendKeys(Keys.CONTROL + "a");userName.sendKeys(Keys.DELETE);userName.sendKeys("lm");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//确认无错误提示Boolean prompt = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[@class='prompt-msg']")));assert prompt;}//输入18个字符的情况下public void AbnormalRegistrationPage3() throws IOException {//点击用户输入框并输入WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.sendKeys(Keys.CONTROL + "a");userName.sendKeys(Keys.DELETE);userName.sendKeys("lm1231231231231231");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//确认无错误提示Boolean prompt = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[@class='prompt-msg']")));assert prompt;}//输入19个字符的情况下public void AbnormalRegistrationPage4() throws IOException {//点击用户输入框并输入WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.sendKeys(Keys.CONTROL + "a");userName.sendKeys(Keys.DELETE);userName.sendKeys("lm12312312312312312");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='请使用字母、数字、下划线2~18个字符']")));String text1 = prompt.getText();assert text1.contains("请使用字母、数字、下划线2~18个字符");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//密码输入5个字符的情况下public void AbnormalRegistrationPage5() throws IOException {//点击密码输入框并输入WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);password.sendKeys("lm123");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='密码格式6~18个字符之间']")));String text1 = prompt.getText();assert text1.contains("密码格式6~18个字符之间");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//输入6个字符的情况下public void AbnormalRegistrationPage6() throws IOException {//点击密码输入框并输入WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);password.sendKeys("lm1234");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//确认无错误提示Boolean prompt = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[@class='prompt-msg']")));assert prompt;}//输入18个字符的情况下public void AbnormalRegistrationPage7() throws IOException {//点击密码输入框并输入WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);password.sendKeys("lm1231231231231231");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//确认无错误提示Boolean prompt = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[@class='prompt-msg']")));assert prompt;}//输入19个字符的情况下public void AbnormalRegistrationPage8() throws IOException {//点击密码输入框并输入WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);password.sendKeys("lm12312312312312312");//点击注册按钮driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button")).click();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='密码格式6~18个字符之间']")));String text1 = prompt.getText();assert text1.contains("密码格式6~18个字符之间");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//正确填写用户名,不填写密码+验证码public void AbnormalRegistrationPage9() throws IOException {//点击用户输入框并输入WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.sendKeys(Keys.CONTROL + "a");userName.sendKeys(Keys.DELETE);userName.sendKeys("lm123");//清空密码框WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);//点击注册按钮WebElement click = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button"));new Actions(driver).doubleClick(click).perform();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='密码格式6~18个字符之间']")));String text1 = prompt.getText();assert text1.contains("密码格式6~18个字符之间");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//正确填写密码,不填写用户名+验证码public void AbnormalRegistrationPage10() throws IOException {//清空用户名框WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.sendKeys(Keys.CONTROL + "a");userName.sendKeys(Keys.DELETE);//点击密码输入框并输入WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);password.sendKeys("lm123456");//点击注册按钮WebElement click = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button"));new Actions(driver).doubleClick(click).perform();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='请使用字母、数字、下划线2~18个字符']")));String text1 = prompt.getText();assert text1.contains("请使用字母、数字、下划线2~18个字符");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}//正确填写验证码,不填写用户名+密码public void AbnormalRegistrationPage11() throws IOException {//清空用户名框WebElement userName = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(1) > input"));userName.sendKeys(Keys.CONTROL + "a");userName.sendKeys(Keys.DELETE);//清空密码框WebElement password = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(2) > input"));password.sendKeys(Keys.CONTROL + "a");password.sendKeys(Keys.DELETE);//输入验证码WebElement verificationInput = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div:nth-child(3) > input"));verificationInput.sendKeys("1234");//点击注册按钮WebElement click = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > div.am-form-group.am-margin-top-main.am-padding-0 > button"));new Actions(driver).doubleClick(click).perform();//屏幕截图CommonScreenshot(Thread.currentThread().getStackTrace()[1].getMethodName());//等待提示信息WebElement prompt = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='prompt-msg' and text()='请使用字母、数字、下划线2~18个字符']")));String text1 = prompt.getText();assert text1.contains("请使用字母、数字、下划线2~18个字符");// 然后等待提示框消失wait.until(ExpectedConditions.invisibilityOf(prompt));}
}
进行测试
public class runtest {public static void main(String[] args) throws InterruptedException, IOException {RegistrationRageTest registrationRageTest=new RegistrationRageTest();registrationRageTest.AbnormalRegistrationPage();registrationRageTest.AbnormalRegistrationPage1();registrationRageTest.AbnormalRegistrationPage2();registrationRageTest.AbnormalRegistrationPage3();registrationRageTest.AbnormalRegistrationPage4();registrationRageTest.AbnormalRegistrationPage5();registrationRageTest.AbnormalRegistrationPage6();registrationRageTest.AbnormalRegistrationPage7();registrationRageTest.AbnormalRegistrationPage8();registrationRageTest.AbnormalRegistrationPage9();registrationRageTest.AbnormalRegistrationPage10();registrationRageTest.AbnormalRegistrationPage11();}