收银台项目——Web自动化测试(简单高效)

 使用Java语言Spring框架实现的收银台项目。用户完成注册登录后进入首页,可以进行购买商品和浏览商品订单的功能,收银员可以对商品进行上架,更新商品。双方都能够浏览到商品信息。

一,测试介绍

使用Java语言实现Web自动化测试,对各页面的元素进行查找确认是否存在,对页面中各功能按按钮进行测试。使用junit简化测试,直观显示哪些代码通过哪些不通过,显示不通过的原因。

相关技术栈

Java、Maven、seleniumWeb自动工具、junit单元测试框架

二,收银台项目的主要功能:

三,Web自动化测试

1)设计测试用例

二)编写测试用例代码

页面测试

对注册页面进行测试,首先检查注册页面元素是否正常展示,之后输入用户名和密码,点击注册按钮成功跳转到登录页面为注册成功

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.*;

  4. import org.openqa.selenium.By;

  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. /**

  7. * @author hu

  8. * @date 2022/9/12 15:00

  9. */

  10. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)

  11. public class RegisterTest {

  12. private static ChromeDriver driver = CommonDriver.getDriver();

  13. @BeforeAll

  14. public static void getUrl(){

  15. driver.get("http://127.0.0.1:8080/");

  16. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(2)")).click();

  17. }

  18. /**

  19. * 检查页面元素是否正确显示

  20. */

  21. @Test

  22. @Order(1)

  23. public void checkHTMLElement(){

  24. String registerText = driver.findElement(By.cssSelector("body > div.内容区域 > form > h2")).getText();

  25. String nameText = driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).getAttribute("placeholder");

  26. String passwordText = driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).getAttribute("placeholder");

  27. Assertions.assertEquals("注册",registerText);

  28. Assertions.assertEquals("用户名",nameText);

  29. Assertions.assertEquals("密码",passwordText);

  30. }

  31. /**

  32. * 检查页面能否注册成功

  33. * 成功跳转到登录页面

  34. */

  35. @Test

  36. @Order(2)

  37. public void checkRegister(){

  38. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaoliu");

  39. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123");

  40. driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();

  41. //检查是否跳转成功

  42. String text = driver.findElement(By.cssSelector("body > div.内容区域 > form > h2")).getText();

  43. Assertions.assertEquals("登录",text);

  44. }

  45. }

 进入项目首页,对首页个元素进行检查,对各按钮功能进行检查,点击按钮是否跳转到相应页面

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.*;

  4. import org.openqa.selenium.By;

  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. /**

  7. * @author hu

  8. * @date 2022/9/12 11:14

  9. */

  10. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)

  11. public class FrontPageTest {

  12. private static ChromeDriver driver = CommonDriver.getDriver();

  13. /**

  14. * 跳转url

  15. */

  16. @BeforeAll

  17. private static void getUrl(){

  18. // driver.get("http://127.0.0.1:8080/login.html");

  19. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaohu");

  20. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123");

  21. driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();

  22. }

  23. /**

  24. *校验首页功能是否正常展示

  25. */

  26. @Test

  27. @Order(1)

  28. public void checkFrontPage(){

  29. String registerText = driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(2)")).getText();

  30. String updateRootText = driver.findElement(By.xpath("/html/body/div[1]/a[2]")).getText();

  31. String restProductText = driver.findElement(By.xpath("/html/body/div[1]/a[3]")).getText();

  32. String browseProductText = driver.findElement(By.xpath("/html/body/div[1]/a[4]")).getText();

  33. String updateProductText = driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(6)")).getText();

  34. String browseOrderText = driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(7)")).getText();

  35. String buyText = driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(8)")).getText();

  36. Assertions.assertEquals("注册账号",registerText);

  37. Assertions.assertEquals("切换账号",updateRootText);

  38. Assertions.assertEquals("上架商品",restProductText);

  39. Assertions.assertEquals("浏览商品",browseProductText);

  40. Assertions.assertEquals("更新商品",updateProductText);

  41. Assertions.assertEquals("浏览订单",browseOrderText);

  42. Assertions.assertEquals("购买商品",buyText);

  43. }

  44. /**

  45. * 检查首页功能是否正确

  46. */

  47. @Test

  48. @Order(2)

  49. public void checkPageRight(){

  50. //注册账号

  51. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(4)")).click();

  52. //从上架商品页面元素对应的文本,校验文本是否符合预期

  53. String restProduct = driver.findElement(By.cssSelector("body > div.内容区域 > form > h2")).getText();

  54. Assertions.assertEquals("上架商品",restProduct);

  55. }

  56. }

 测试商品上架页面,检查页面元素是否正确展示,使用参数化对商品进行上架操作,检查上架功能是否正常

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import com.webAutoTest.common.ParamsUtil;

  4. import com.webAutoTest.model.Goods;

  5. import org.junit.jupiter.api.*;

  6. import org.junit.jupiter.params.ParameterizedTest;

  7. import org.junit.jupiter.params.provider.Arguments;

  8. import org.junit.jupiter.params.provider.MethodSource;

  9. import org.openqa.selenium.By;

  10. import org.openqa.selenium.WebElement;

  11. import org.openqa.selenium.chrome.ChromeDriver;

  12. import java.util.stream.Stream;

  13. /**

  14. * @author hu

  15. * @date 2022/9/12 11:57

  16. */

  17. public class RestProductTest {

  18. private static ChromeDriver driver = CommonDriver.getDriver();

  19. @BeforeAll

  20. public static void getUrl() throws InterruptedException {

  21. // driver.get("http://127.0.0.1:8080/login.html");

  22. // Thread.sleep(2);

  23. // driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaohu");

  24. // driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123");

  25. // Thread.sleep(2);

  26. // driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();

  27. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(4)")).click();

  28. }

  29. @BeforeEach

  30. public void getUrlIn(){

  31. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(4)")).click();

  32. }

  33. /**

  34. * 校验商品页面是否正常展示

  35. */

  36. @Test

  37. public void checkRestProduct(){

  38. String restText = driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).getText();

  39. String productNameText = driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).getAttribute("placeholder");

  40. String unitText = driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(5) > input[type=text]")).getAttribute("placeholder");

  41. Assertions.assertEquals("添加",restText);

  42. Assertions.assertEquals("名称",productNameText);

  43. Assertions.assertEquals("单位",unitText);

  44. }

  45. /**

  46. * 添加商品之后会跳转到浏览商品页面

  47. */

  48. @ParameterizedTest

  49. @MethodSource

  50. public void addGoods(String goodsName,int count,String introduce,String unit,int price,int discount){

  51. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).

  52. sendKeys(goodsName);

  53. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).

  54. sendKeys(count+"");

  55. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(4) > input[type=text]")).

  56. sendKeys(introduce);

  57. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(5) > input[type=text]")).

  58. sendKeys(unit);

  59. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(6) > input[type=text]")).

  60. sendKeys(price+"");

  61. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(7) > input[type=text]")).

  62. sendKeys(discount+"");

  63. driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();

  64. //是否跳转到浏览商品页面

  65. String element = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > h2")).getText();

  66. Assertions.assertEquals("浏览商品",element);

  67. }

  68. public static Stream<Arguments> addGoods(){

  69. Goods goods = ParamsUtil.getGoodsName();

  70. Goods goods1 = ParamsUtil.getGoodsName();

  71. return Stream.of(Arguments.arguments(goods.getName(),goods.getCount(),goods.getIntroduce()

  72. ,goods.getUnit(),goods.getPrice(),goods.getDiscount()),

  73. Arguments.arguments(goods1.getName(),goods1.getCount(),goods1.getIntroduce()

  74. ,goods1.getUnit(),goods1.getPrice(),goods1.getDiscount()));

  75. }

  76. }

测试商品浏览页面,检查页面元素是否存在,检查下架功能是否正常

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.AfterAll;

  4. import org.junit.jupiter.api.Assertions;

  5. import org.junit.jupiter.api.BeforeAll;

  6. import org.junit.jupiter.api.Test;

  7. import org.openqa.selenium.By;

  8. import org.openqa.selenium.chrome.ChromeDriver;

  9. /**

  10. * @author hu

  11. * @date 2022/9/12 13:54

  12. */

  13. public class BrowseProductTest {

  14. private static ChromeDriver driver = CommonDriver.getDriver();

  15. /**

  16. * 跳转到浏览商品页面

  17. */

  18. @BeforeAll

  19. public static void getUrl(){

  20. /* driver.get("http://127.0.0.1:8080/login.html");

  21. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaohu");

  22. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123");

  23. driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();*/

  24. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(5)")).click();

  25. }

  26. /**

  27. * 浏览页面元素是否展示元素

  28. */

  29. @Test

  30. public void checkHTMLElement(){

  31. String countText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > thead > tr > th:nth-child(1)")).getText();

  32. String introduceText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > thead > tr > th:nth-child(3)")).getText();

  33. Assertions.assertEquals("编号",countText);

  34. Assertions.assertEquals("介绍",introduceText);

  35. }

  36. /**

  37. * 检查下架按钮是否能正常使用

  38. */

  39. @Test

  40. public void checkPull(){

  41. // driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > tbody > tr:nth-child(1) > td:nth-child(8) > a")).click();

  42. }

  43. }

 测试更新商品页面,检查页面元素正确显示,对更新功能进行测试

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.*;

  4. import org.openqa.selenium.By;

  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. /**

  7. * @author hu

  8. * @date 2022/9/12 15:34

  9. */

  10. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)

  11. public class UpdateTest {

  12. private static ChromeDriver driver = CommonDriver.getDriver();

  13. @BeforeAll

  14. public static void getUrl(){

  15. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(6)")).click();

  16. }

  17. /**

  18. * 检查页面元素是否正确显示

  19. */

  20. @Test

  21. @Order(1)

  22. public void checkHTMLElement(){

  23. String updateText = driver.findElement(By.cssSelector("body > div.内容区域 > form > h2")).getText();

  24. String idText = driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).getAttribute("placeholder");

  25. Assertions.assertEquals("更新商品",updateText);

  26. Assertions.assertEquals("商品 id",idText);

  27. }

  28. /**

  29. * 更新商品功能是否正常

  30. */

  31. @Test

  32. @Order(2)

  33. public void checkUpdateRight(){

  34. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(5)")).click();

  35. String idText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > tbody > tr:nth-child(1) > td:nth-child(1)")).getText();

  36. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(6)")).click();

  37. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(2) > input[type=text]")).sendKeys(idText);

  38. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(3) > input[type=text]")).sendKeys("西瓜");

  39. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(4) > input[type=text]")).sendKeys("50");

  40. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(5) > input[type=text]")).sendKeys("大西瓜");

  41. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(6) > input[type=text]")).sendKeys("斤");

  42. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(7) > input[type=text]")).sendKeys("2");

  43. driver.findElement(By.cssSelector("body > div.内容区域 > form > div:nth-child(8) > input[type=text]")).sendKeys("70");

  44. driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();

  45. //更新成功跳转到浏览商品

  46. String browseText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > h2")).getText();

  47. Assertions.assertEquals("浏览商品",browseText);

  48. }

  49. }

 对购买商品页面进行测试,检查页面元素是否正常显示,购买功能是否正常,成功跳转到支付页面,失败回到购买页面

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.*;

  4. import org.junit.jupiter.params.ParameterizedTest;

  5. import org.junit.jupiter.params.provider.MethodSource;

  6. import org.openqa.selenium.By;

  7. import org.openqa.selenium.chrome.ChromeDriver;

  8. /**

  9. * @author hu

  10. * @date 2022/9/12 16:09

  11. */

  12. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)

  13. public class BuyTest {

  14. private static ChromeDriver driver = CommonDriver.getDriver();

  15. @BeforeAll

  16. public static void getUrl(){

  17. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(8)")).click();

  18. }

  19. /**

  20. * 检查页面元素是否正确显示

  21. */

  22. @Test

  23. @Order(1)

  24. public void checkHTMLElement(){

  25. String text = driver.findElement(By.cssSelector("body > div.内容区域 > form > h2")).getText();

  26. Assertions.assertEquals("购买商品",text);

  27. }

  28. /**

  29. * 检查购买功能

  30. */

  31. @Test

  32. @Order(2)

  33. public void checkBuyRight(){

  34. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(5)")).click();

  35. String idText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > tbody > tr:nth-child(3) > td:nth-child(1)")).getText();

  36. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(8)")).click();

  37. driver.findElement(By.cssSelector("body > div.内容区域 > form > div > input[type=text]")).sendKeys(idText + "-" + 10);

  38. driver.findElement(By.cssSelector("body > div.内容区域 > form > button")).click();

  39. //购买成功跳转到支付页面

  40. String text = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示区域 > a.btn.btn-confirm")).getText();

  41. Assertions.assertEquals("确认",text);

  42. //购买失败跳转到购买页面

  43. }

  44. }

 测试购买订单页面,检查页面元素是否正确展示,对支付功能进行测试,成功跳转到支付页面进行支付操作

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.*;

  4. import org.openqa.selenium.By;

  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. /**

  7. * @author hu

  8. * @date 2022/9/12 16:01

  9. */

  10. public class BrowseOrder {

  11. private static ChromeDriver driver = CommonDriver.getDriver();

  12. @BeforeAll

  13. public static void getUrl(){

  14. driver.findElement(By.cssSelector("body > div.导航栏 > a:nth-child(7)")).click();

  15. }

  16. /**

  17. * 检查页面元素是否正确显示

  18. */

  19. @Test

  20. public void checkHTMLElement(){

  21. String orderText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > h2")).getText();

  22. String informationText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > thead > tr > th:nth-child(1)")).getText();

  23. Assertions.assertEquals("浏览订单",orderText);

  24. Assertions.assertEquals("信息",informationText);

  25. }

  26. }

 测试支付页面,进入浏览订单页面,点击订单中的未支付,进入支付页面,对支付功能进行测试,支付成功跳转到浏览订单页面,取消支付清除此订单并跳转到浏览商品页面

  1. package com.webAutoTest.tests;

  2. import com.webAutoTest.common.CommonDriver;

  3. import org.junit.jupiter.api.*;

  4. import org.openqa.selenium.By;

  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. /**

  7. * @author hu

  8. * @date 2022/9/12 17:06

  9. */

  10. public class PayTest {

  11. private static ChromeDriver driver = CommonDriver.getDriver();

  12. @BeforeAll

  13. private static void getUrl() {

  14. driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > tbody > tr:nth-child(1) > td:nth-child(1) > p:nth-child(2) > a")).click();

  15. ;

  16. }

  17. /**

  18. * 对功能进行测试

  19. */

  20. @Test

  21. public void CheckHTMLElement() {

  22. //取消支付订单清除,跳转到商品浏览页面

  23. driver.findElement(By.cssSelector("body > div.内容区域 > div.展示区域 > a.btn.btn-confirm")).click();

  24. //检查是否跳转到商品浏览页面

  25. String browseProductText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > h2")).getText();

  26. Assertions.assertSame("浏览商品",browseProductText);

  27. //确认支付跳转到订单浏览页面

  28. driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > table > tbody > tr:nth-child(1) > td:nth-child(1) > p:nth-child(2) > a")).click();

  29. driver.findElement(By.cssSelector("body > div.内容区域 > div.展示区域 > a.btn.btn-confirm")).click();

  30. //检查是否跳转到订单浏览页面

  31. String browseOrderText = driver.findElement(By.cssSelector("body > div.内容区域 > div.展示列表 > h2")).getText();

  32. Assertions.assertEquals("浏览订单",browseOrderText);

  33. }

  34. }

三)测试结果

过程中观察测试数据,线程等待,共通过测试用例7,耗时25s

测试用例全部通过

总结

1.使用selenium4web自动化工具和Junit5单元测试框架,通过注解,提升测试效率。

2.使用单例模式,将ChromeDriver私有化,保证所有的测试都使用同一个实例对象,减少创建和销毁对象的时间,

3.使用测试套件,一次执行所有的测试用例。

4.使用隐式等待和强制等待,提升自动化测试用例的稳定性。

为什么使用强制等待,不使用显示等待:

  • 显示等待书写麻烦
  • 显示等待和隐式等待容易出现问题
  • 弹窗无法定位

5.使用屏幕截图,方便定位问题的出处。

 

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

          视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

  • 16
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值