当前位置:   article > 正文

对博客系统基本功能进行自动化测试(Junit + Selenium)

对博客系统基本功能进行自动化测试(Junit + Selenium)

环境搭建:

  1. 浏览器:
    1. 本次测试使用Chrome浏览器
    2. 在jdk的bin目录下安装对应浏览器驱动(尽量选择与浏览器版本相近的驱动)chromedriver.storage.googleapis.com/index.html
  2. Junit依赖:
    1. <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    2. <dependency>
    3. <groupId>org.junit.jupiter</groupId><!--编写用例的基本注解-->
    4. <artifactId>junit-jupiter-api</artifactId>
    5. <version>5.9.1</version>
    6. </dependency>
    7. <dependency><!--参数化测试依赖-->
    8. <groupId>org.junit.jupiter</groupId>
    9. <artifactId>junit-jupiter-params</artifactId>
    10. <version>5.9.1</version>
    11. </dependency>
    12. <dependency><!--这个库提供JUnit平台的核心功能,比如共享的测试接口、注解和工具类-->
    13. <groupId>org.junit.platform</groupId>
    14. <artifactId>junit-platform-commons</artifactId>
    15. <version>1.9.1</version> <!-- 请根据需要调整为与JUnit 5.9.1兼容的版本 -->
    16. </dependency>
    17. <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
    18. <dependency>
    19. <groupId>org.junit.platform</groupId>
    20. <artifactId>junit-platform-suite</artifactId>
    21. <version>1.9.1</version>
    22. <scope>test</scope>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.junit.platform</groupId>
    26. <artifactId>junit-platform-suite-api</artifactId>
    27. <version>1.9.1</version>
    28. </dependency>
    29. <!--JUnit Jupiter的测试引擎,实现了JUnit Platform的TestEngine接口。
    30. 它负责发现和执行使用JUnit Jupiter API编写的测试。-->
    31. <dependency>
    32. <groupId>org.junit.jupiter</groupId>
    33. <artifactId>junit-jupiter-engine</artifactId>
    34. <version>5.9.1</version>
    35. <scope>test</scope>
    36. </dependency>
  3. Selenium依赖
  1. <dependency>
  2. <groupId>org.seleniumhq.selenium</groupId>
  3. <artifactId>selenium-java</artifactId>
  4. <version>3.141.59</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.seleniumhq.selenium</groupId>
  8. <artifactId>selenium-support</artifactId>
  9. <version>3.141.59</version>
  10. </dependency>

测试用例:

网站:登陆页面

项目结构:

InitAndQuit进行测试初始化和收尾工作

TestItem包含对各个页面基本功能的自动化测试用例

初始化以及资源关闭:

  1. /**
  2. * @ClassName InitAndQuit
  3. * @Description 初识化测试相关以及测试结束资源关闭
  4. * @Author 86153
  5. * @Date 2024/4/30 10:20
  6. * @Version 1.0
  7. **/
  8. public class InitAndQuit {
  9. static WebDriver webDriver;
  10. @BeforeAll
  11. static void init() {
  12. webDriver = new ChromeDriver();
  13. }
  14. @AfterAll
  15. static void quit() {
  16. webDriver.quit();
  17. }
  18. }

登录页面测试用例:

  • 输入给定邮箱点击获取验证码:
  1. //验证码
  2. private static String emailCode;
  3. //在注册页面点击获取验证码按钮
  4. @ParameterizedTest
  5. @CsvFileSource(resources = "login.csv")
  6. @Order(0)
  7. void loginTest(String account) throws InterruptedException {
  8. webDriver.get("http://8.130.70.131:8080/login.html");
  9. webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  10. webDriver.findElement(By.cssSelector("#username")).sendKeys(account);
  11. WebDriverWait wait = new WebDriverWait(webDriver,10);
  12. //这里 获取验证码 和 提交按钮为俩个一样的button,所以需要进行按钮的选择
  13. WebElement clickElement = wait.until(ExpectedConditions
  14. .elementToBeClickable(By.cssSelector("#submit")));
  15. List<WebElement> elements = webDriver.findElements(By.cssSelector("#submit"));
  16. elements.get(0).click();
  17. }
  • 从邮箱中拿到验证码
  1. /*邮箱登录拿到验证码*/
  2. @Test
  3. @Order(1)
  4. void getCaptcha() throws InterruptedException {
  5. webDriver.get("https://www.baidu.com/");
  6. webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  7. webDriver.findElement(By.cssSelector(".s_ipt")).sendKeys("https://mail.qq.com/");
  8. webDriver.findElement(By.cssSelector("#su")).click();
  9. webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  10. //保存当前窗口的句柄
  11. String originalWindow = webDriver.getWindowHandle();
  12. webDriver.findElement(By.cssSelector("#\\31 > div > div:nth-child(1) > h3 > a")).click();
  13. //切换窗口
  14. Set<String> set = webDriver.getWindowHandles();
  15. for(String cur : set) {
  16. if(!cur.equals(originalWindow)) {
  17. webDriver.switchTo().window(cur);
  18. }
  19. }
  20. //登录邮箱
  21. //注意这里切换frame时要先去选择获取到frame
  22. WebElement iframe = webDriver.findElement(By.cssSelector("#QQMailSdkTool_login_loginBox_qq > iframe"));
  23. webDriver.switchTo().frame(iframe);
  24. WebElement iframe1 = webDriver.findElement(By.cssSelector("#ptlogin_iframe"));
  25. webDriver.switchTo().frame(iframe1);
  26. //点击用户头像进行登录(电脑登陆了QQ)
  27. webDriver.findElement(By.cssSelector("#img_out_3224881242")).click();
  28. sleep(10000);
  29. //进入对应邮件
  30. webDriver.findElement(By.cssSelector("#mailMainApp > div.frame_main.mail_app > div > div > div > div.mailList_listWrapper > div.mailList_group > div:nth-child(1) > div.mailList_group_item_cnt > table > tbody > tr:nth-child(1)")).click();
  31. //拿到验证码
  32. WebElement element = webDriver.findElement(By.cssSelector("#readmail_content_html > span"));
  33. String text = element.getText();
  34. emailCode = text;
  35. }
  • 登录
  1. //登陆页面测试
  2. @ParameterizedTest
  3. @CsvFileSource(resources = "login.csv")
  4. @Order(2)
  5. void loginTest(String account,String password) throws InterruptedException {
  6. //进入登陆页面
  7. webDriver.get("http://8.130.70.131:8080/login.html");
  8. //隐式等待
  9. webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  10. //输入账号密码验证码
  11. webDriver.findElement(By.cssSelector("#username")).sendKeys(account);
  12. webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
  13. webDriver.findElement(By.cssSelector("#captcha")).sendKeys(emailCode);
  14. //显示等待
  15. WebDriverWait wait = new WebDriverWait(webDriver,10);
  16. //这里 获取验证码 和 提交按钮为俩个一样的button,所以需要进行按钮的选择
  17. WebElement clickElement = wait.until(ExpectedConditions
  18. .elementToBeClickable(By.cssSelector("#submit")));
  19. List<WebElement> elements = webDriver.findElements(By.cssSelector("#submit"));
  20. elements.get(1).click();
  21. //显示等待,等待弹窗出现
  22. //注意这里是个坑,弹窗不属于页面内的元素,不能使用隐式等待
  23. wait.until(ExpectedConditions.alertIsPresent());
  24. //弹窗选择
  25. webDriver.switchTo().alert().accept();
  26. //等待进入新页面
  27. webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  28. //校验url
  29. String currentURL = webDriver.getCurrentUrl();
  30. Assertions.assertEquals("http://8.130.70.131:8080/myblog_list.html",currentURL);
  31. }

列表页测试用例:

  1. //列表页自动化测试
  2. /**
  3. * 如果列表页博客数量不为0表示测试通过
  4. **/
  5. @Test
  6. @Order(3)
  7. void listTest() {
  8. webDriver.get("hhttp://8.130.70.131:8080/blog_list.html");
  9. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  10. int size = webDriver.findElements(By.cssSelector(".title")).size();
  11. System.out.println(size);
  12. Assertions.assertNotEquals(0,size);
  13. }

个人列表页测试用例:

  1. //个人列表页测试
  2. @Test
  3. @Order(4)
  4. void selfListTest() {
  5. webDriver.get("http://8.130.70.131:8080/myblog_list.html");
  6. //文章数量不为0测试
  7. List<WebElement> list = webDriver.findElements(By.cssSelector(".title"));
  8. int size = webDriver.findElements(By.cssSelector(".title")).size();
  9. System.out.println(size);
  10. Assertions.assertNotEquals(0,list.size());
  11. //测试“主页”按钮
  12. webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
  13. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  14. String curUrl = webDriver.getCurrentUrl();
  15. Assertions.assertEquals("http://8.130.70.131:8080/blog_list.html",curUrl);
  16. //回退到个人主页
  17. webDriver.navigate().back();
  18. //测试“写博客按钮”
  19. webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
  20. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  21. curUrl = webDriver.getCurrentUrl();
  22. Assertions.assertEquals("http://8.130.70.131:8080/blog_add.html",curUrl);
  23. webDriver.navigate().back();
  24. /* //测试“注销”按钮
  25. webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
  26. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  27. curUrl = webDriver.getCurrentUrl();
  28. Assertions.assertEquals("http://8.130.70.131:8080/login.html",curUrl);*/
  29. }

博客详情页测试用例:

  1. //博客详情页测试
  2. @ParameterizedTest
  3. @Order(5)
  4. @CsvFileSource(resources = "detail.csv")
  5. void detailTest(String destUrl,String destPageTitle,String destBlogTitle) {
  6. //进入列表页
  7. webDriver.get("http://8.130.70.131:8080/blog_list.html");
  8. //点击文章详情按钮,进入博客详情页
  9. webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a")).click();
  10. //校验页面url,页面title,博客题目
  11. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  12. String url = webDriver.getCurrentUrl();
  13. String pageTitle = webDriver.getTitle();
  14. String blogTitle = webDriver.findElement(By.cssSelector("#title")).getText();
  15. Assertions.assertEquals(destUrl,url);
  16. Assertions.assertEquals(destPageTitle,pageTitle);
  17. Assertions.assertEquals(destBlogTitle,blogTitle);
  18. }

编辑页测试用例:

  1. //博客编辑页测试
  2. @ParameterizedTest
  3. @Order(6)
  4. @CsvFileSource(resources = "edit.csv")
  5. void editTest(String data,String title) {
  6. webDriver.get("http://8.130.70.131:8080/blog_add.html");
  7. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  8. /* //直接使用通过js设置文章标题
  9. ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");*/
  10. webDriver.findElement(By.cssSelector("#title")).sendKeys("自动化测试");
  11. //发布文章
  12. webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
  13. //这里有一个”是否继续添加博客“的弹窗,进行选择后才跳转
  14. WebDriverWait wait = new WebDriverWait(webDriver,5);
  15. wait.until(ExpectedConditions.alertIsPresent());
  16. webDriver.switchTo().alert().dismiss();
  17. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  18. //校验url跳转
  19. String cur_url = webDriver.getCurrentUrl();
  20. Assertions.assertEquals("http://8.130.70.131:8080/myblog_list.html",cur_url);
  21. //校验第一篇博客的发布时间,标题
  22. String publishDate = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date")).getText();
  23. String publishTitle = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
  24. Assertions.assertEquals(title,publishTitle);
  25. if(publishDate.contains(data)) {
  26. System.out.println("测试通过");
  27. }else {
  28. System.out.println("发布时间错误");
  29. }
  30. //删除博客
  31. webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(6)")).click();
  32. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  33. //弹窗选择
  34. wait.until(ExpectedConditions.alertIsPresent());
  35. webDriver.switchTo().alert().accept();
  36. //校验第一篇博客是否被删除
  37. WebElement after_delete_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title"));
  38. Assertions.assertNotEquals(title,after_delete_title);
  39. }

注销:

  1. //注销
  2. @Test
  3. @Order(7)
  4. void login_out() {
  5. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  6. //点击注销按钮
  7. webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
  8. //显示等待alter弹窗出现
  9. WebDriverWait wait = new WebDriverWait(webDriver,5);
  10. wait.until(ExpectedConditions.alertIsPresent());
  11. //选择弹框并进行确定
  12. webDriver.switchTo().alert().accept();
  13. webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
  14. String cur_url = webDriver.getCurrentUrl();
  15. Assertions.assertEquals("http://8.130.70.131:8080/login.html",cur_url);
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/599963
推荐阅读
相关标签
  

闽ICP备14008679号