• 个人博客系统-测试用例+自动化测试


    一、个人博客系统测试用例

    二、自动化测试

            使用selenium4 + Junit5单元测试框架,来进行简单的自动化测试。

    1. 准备工作

    (1)引入依赖,此时的pom.xml文件:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>myblog-seleniumartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.source>8maven.compiler.source>
    11. <maven.compiler.target>8maven.compiler.target>
    12. properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>org.seleniumhq.seleniumgroupId>
    16. <artifactId>selenium-javaartifactId>
    17. <version>4.0.0version>
    18. dependency>
    19. <dependency>
    20. <groupId>commons-iogroupId>
    21. <artifactId>commons-ioartifactId>
    22. <version>2.6version>
    23. dependency>
    24. <dependency>
    25. <groupId>org.junit.jupitergroupId>
    26. <artifactId>junit-jupiterartifactId>
    27. <version>5.8.2version>
    28. <scope>testscope>
    29. dependency>
    30. <dependency>
    31. <groupId>org.junit.platformgroupId>
    32. <artifactId>junit-platform-commonsartifactId>
    33. <version>1.8.2version>
    34. <scope>testscope>
    35. dependency>
    36. <dependency>
    37. <groupId>org.junit.platformgroupId>
    38. <artifactId>junit-platform-reportingartifactId>
    39. <version>1.8.2version>
    40. <scope>testscope>
    41. dependency>
    42. <dependency>
    43. <groupId>org.junit.platformgroupId>
    44. <artifactId>junit-platform-suiteartifactId>
    45. <version>1.8.2version>
    46. <scope>testscope>
    47. dependency>
    48. dependencies>
    49. project>

    (2)创建公共类

    创建common包,存放公共类。首先创建CommonDriver类来获取驱动。

    1. package com.webAutoTest.common;
    2. import org.openqa.selenium.chrome.ChromeDriver;
    3. import org.openqa.selenium.chrome.ChromeOptions;
    4. import java.time.Duration;
    5. /**
    6. * Created with IntelliJ IDEA.
    7. * Description: 创建驱动对象,并返回该对象
    8. * User: WangWZ
    9. * Date: 2023-09-05
    10. * Time: 9:48
    11. */
    12. public class CommonDriver {
    13. //使用单例模式创建驱动
    14. private static ChromeOptions options = new ChromeOptions();
    15. public static ChromeDriver driver = null;
    16. public static ChromeDriver getDriver(){
    17. if (driver == null) {
    18. options.addArguments("--remote-allow-origins=*");
    19. driver = new ChromeDriver(options);
    20. //创建驱动的时候就加上隐式等待
    21. //整个测试就都会隐式等待了
    22. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    23. }
    24. return driver;
    25. }
    26. }

            如果代码中使用到了进行截图、存储文件的操作以及使用了参数化实现数据来源时,也可以在创建公共类。方便使用。

    2. 注册页面 

            后续使用Juit中的 Suit套件 进行执行,所以关闭驱动的方法可以单独一个类。使用@SelectClasses注解执行。

    1. package com.webAutoTest.tests;
    2. import com.webAutoTest.common.CommonDriver;
    3. import org.junit.jupiter.api.Assertions;
    4. import org.junit.jupiter.api.BeforeAll;
    5. import org.junit.jupiter.api.Test;
    6. import org.junit.jupiter.params.ParameterizedTest;
    7. import org.junit.jupiter.params.provider.CsvSource;
    8. import org.openqa.selenium.Alert;
    9. import org.openqa.selenium.By;
    10. import org.openqa.selenium.WebElement;
    11. import org.openqa.selenium.chrome.ChromeDriver;
    12. import java.time.Duration;
    13. /**
    14. * Created with IntelliJ IDEA.
    15. * Description: 注册页面测试
    16. * User: WangWZ
    17. * Date: 2023-09-05
    18. * Time: 9:48
    19. */
    20. public class RegTest {
    21. //获取驱动对象
    22. static ChromeDriver driver = CommonDriver.getDriver();
    23. @BeforeAll
    24. public static void getURL() {
    25. driver.get("http://58.87.89.71:8009/reg.html");
    26. //使用隐式等待渲染页面完成
    27. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    28. }
    29. /**
    30. * 用户名已存在
    31. * @param username
    32. * @param password1
    33. * @param password2
    34. */
    35. @ParameterizedTest
    36. @CsvSource({"王文哲", "456", "456"})
    37. public void RegNameExist(String username, String password1, String password2) {
    38. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    39. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    40. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    41. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
    42. driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
    43. driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
    44. Alert alert = driver.switchTo().alert();
    45. String str =alert.getText();
    46. Assertions.assertEquals("该用户名已被使用,请重新输入", str);
    47. alert.accept();
    48. }
    49. /**
    50. * 用户名为空
    51. * @param username
    52. * @param password1
    53. * @param password2
    54. */
    55. @ParameterizedTest
    56. @CsvSource({"", "456", "456"})
    57. public void RegNameNull(String username, String password1, String password2) {
    58. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    59. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    60. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    61. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
    62. driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
    63. driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
    64. Alert alert = driver.switchTo().alert();
    65. String str =alert.getText();
    66. Assertions.assertEquals("请先输入用户名", str);
    67. alert.accept();
    68. }
    69. /**
    70. * 密码为空
    71. * @param username
    72. * @param password1
    73. * @param password2
    74. */
    75. @ParameterizedTest
    76. @CsvSource({"王王文哲", "", ""})
    77. public void RegPasswordNull(String username, String password1, String password2) {
    78. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    79. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    80. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    81. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
    82. driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
    83. driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
    84. Alert alert = driver.switchTo().alert();
    85. String str =alert.getText();
    86. Assertions.assertEquals("请先输入密码", str);
    87. alert.accept();
    88. }
    89. /**
    90. * 确认密码和密码不一致
    91. * @param username
    92. * @param password1
    93. * @param password2
    94. */
    95. @ParameterizedTest
    96. @CsvSource({"汪汪", "456", "1456"})
    97. public void RegPasswordDe(String username, String password1, String password2) {
    98. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    99. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    100. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    101. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
    102. driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
    103. driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
    104. Alert alert = driver.switchTo().alert();
    105. String str =alert.getText();
    106. Assertions.assertEquals("两次密码输入的不一致,请先检查", str);
    107. alert.accept();
    108. }
    109. /**
    110. * 用户名或密码中存在特殊字符
    111. * @param username
    112. * @param password1
    113. * @param password2
    114. */
    115. @ParameterizedTest
    116. @CsvSource({"@w王1", "45@6", "45@6"})
    117. public void RegExistSpecial(String username, String password1, String password2) {
    118. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    119. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    120. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    121. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
    122. driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
    123. driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
    124. Alert alert = driver.switchTo().alert();
    125. String str =alert.getText();
    126. Assertions.assertEquals("恭喜,注册成功", str);
    127. alert.accept();
    128. }
    129. /**
    130. * 注册标题
    131. */
    132. @Test
    133. public void RegTitle() {
    134. String str = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
    135. Assertions.assertEquals("注册",str);
    136. }
    137. /**
    138. * 输入框显示
    139. */
    140. @Test
    141. public void RegNameInput(){
    142. WebElement element = driver.findElement(By.xpath("//*[@id=\"username\"]"));
    143. Assertions.assertNotNull(element);
    144. }
    145. /**
    146. * 输入框文字
    147. */
    148. @Test
    149. public void RegUName() {
    150. String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/span")).getText();
    151. Assertions.assertEquals("用户名",str);
    152. }
    153. /**
    154. * 密码框显示
    155. */
    156. @Test
    157. public void RegPasswordInput(){
    158. WebElement element = driver.findElement(By.xpath("//*[@id=\"password\"]"));
    159. Assertions.assertNotNull(element);
    160. }
    161. /**
    162. * 密码框文字
    163. */
    164. @Test
    165. public void RegPassword() {
    166. String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/span")).getText();
    167. Assertions.assertEquals("密码",str);
    168. }
    169. /**
    170. * 确认密码框显示
    171. */
    172. @Test
    173. public void RegPassword2Input(){
    174. WebElement element = driver.findElement(By.xpath("//*[@id=\"password2\"]"));
    175. Assertions.assertNotNull(element);
    176. }
    177. /**
    178. * 确认密码框文字
    179. */
    180. @Test
    181. public void RegPassword2() {
    182. String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/span")).getText();
    183. Assertions.assertEquals("确认密码",str);
    184. }
    185. /**
    186. * 验证码图片显示
    187. */
    188. @Test
    189. public void RegPhoto(){
    190. WebElement element = driver.findElement(By.xpath("//*[@id=\"codeimg\"]"));
    191. Assertions.assertNotNull(element);
    192. }
    193. /**
    194. * 验证码文字
    195. */
    196. @Test
    197. public void RegDraft() {
    198. String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[4]/span")).getText();
    199. Assertions.assertEquals("验证码",str);
    200. }
    201. }

    3. 登录页面

    1. package com.webAutoTest.tests;
    2. import com.webAutoTest.common.CommonDriver;
    3. import org.junit.jupiter.api.Assertions;
    4. import org.junit.jupiter.api.BeforeEach;
    5. import org.junit.jupiter.api.Test;
    6. import org.junit.jupiter.params.ParameterizedTest;
    7. import org.junit.jupiter.params.provider.CsvSource;
    8. import org.openqa.selenium.Alert;
    9. import org.openqa.selenium.By;
    10. import org.openqa.selenium.WebElement;
    11. import org.openqa.selenium.chrome.ChromeDriver;
    12. import java.time.Duration;
    13. /**
    14. * Created with IntelliJ IDEA.
    15. * Description: 登录页面
    16. * User: WangWZ
    17. * Date: 2023-09-05
    18. * Time: 9:49
    19. */
    20. public class LoginTest {
    21. ChromeDriver driver = CommonDriver.getDriver();
    22. @BeforeEach
    23. public void getUrl(){
    24. driver.get("http://58.87.89.71:8009/login.html");
    25. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    26. }
    27. /**
    28. * 正确的用户名和密码
    29. */
    30. @ParameterizedTest
    31. @CsvSource({"王文哲","123"})
    32. public void LoginTrue(String username, String password) {
    33. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    34. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    35. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    36. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
    37. driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
    38. String str = driver.getCurrentUrl();
    39. Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
    40. driver.navigate();
    41. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    42. }
    43. /**
    44. * 错误的用户名和密码
    45. */
    46. @ParameterizedTest
    47. @CsvSource({"王文哲","1234"})
    48. public void LoginFalse(String username, String password) {
    49. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    50. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    51. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    52. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
    53. driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
    54. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    55. Alert alert = driver.switchTo().alert();
    56. String str = alert.getText();
    57. Assertions.assertEquals("抱歉登录失败,用户名或密码输入错误,请重试!",str);
    58. alert.accept();
    59. }
    60. /**
    61. * 登录密码为空
    62. */
    63. @ParameterizedTest
    64. @CsvSource({"王文哲",""})
    65. public void LoginPasswordNull(String username, String password) {
    66. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    67. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    68. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    69. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
    70. driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
    71. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    72. Alert alert = driver.switchTo().alert();
    73. String str = alert.getText();
    74. Assertions.assertEquals("请先输入密码!",str);
    75. alert.accept();
    76. }
    77. /**
    78. * 用户名为空
    79. */
    80. @ParameterizedTest
    81. @CsvSource({"","123"})
    82. public void LoginNameNull(String username, String password) {
    83. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    84. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    85. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    86. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
    87. driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
    88. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    89. Alert alert = driver.switchTo().alert();
    90. String str = alert.getText();
    91. Assertions.assertEquals("请先输入用户名!",str);
    92. alert.accept();
    93. }
    94. /**
    95. * 密码或用户名有特殊字符
    96. */
    97. @ParameterizedTest
    98. @CsvSource({"@w王1", "45@6"})
    99. public void Login(String username, String password) {
    100. driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
    101. driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
    102. driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
    103. driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
    104. driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
    105. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    106. String str = driver.getCurrentUrl();
    107. Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
    108. driver.navigate();
    109. }
    110. /**
    111. * 注册按钮功能
    112. */
    113. @Test
    114. public void LoginToReg() {
    115. driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
    116. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    117. String str = driver.getCurrentUrl();
    118. Assertions.assertEquals("http://58.87.89.71:8009/reg.html",str);
    119. driver.navigate();
    120. }
    121. /**
    122. * 登录标题
    123. */
    124. @Test
    125. public void LoginTitle() {
    126. String str = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
    127. Assertions.assertEquals("登录",str);
    128. }
    129. /**
    130. * 输入框显示
    131. */
    132. @Test
    133. public void LoginNameInput(){
    134. WebElement element = driver.findElement(By.xpath("//*[@id=\"username\"]"));
    135. Assertions.assertNotNull(element);
    136. }
    137. /**
    138. * 输入框文字
    139. */
    140. @Test
    141. public void LoginUName() {
    142. String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/span")).getText();
    143. Assertions.assertEquals("用户名",str);
    144. }
    145. /**
    146. * 密码框显示
    147. */
    148. @Test
    149. public void LoginPasswordInput(){
    150. WebElement element = driver.findElement(By.xpath("//*[@id=\"password\"]"));
    151. Assertions.assertNotNull(element);
    152. }
    153. /**
    154. * 密码框文字
    155. */
    156. @Test
    157. public void LoginPassword() {
    158. String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/span")).getText();
    159. Assertions.assertEquals("密码",str);
    160. }
    161. /**
    162. * 登录按钮
    163. */
    164. @Test
    165. public void LoginSub() {
    166. String str = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getText();
    167. Assertions.assertEquals("提交",str);
    168. }
    169. }

    4. 列表页面

    1. package com.webAutoTest.tests;
    2. import com.webAutoTest.common.CommonDriver;
    3. import org.junit.jupiter.api.Assertions;
    4. import org.junit.jupiter.api.BeforeEach;
    5. import org.junit.jupiter.api.Test;
    6. import org.openqa.selenium.Alert;
    7. import org.openqa.selenium.By;
    8. import org.openqa.selenium.chrome.ChromeDriver;
    9. import java.time.Duration;
    10. /**
    11. * Created with IntelliJ IDEA.
    12. * Description: 列表页面
    13. * User: WangWZ
    14. * Date: 2023-09-05
    15. * Time: 9:49
    16. */
    17. public class ListTest {
    18. ChromeDriver driver = CommonDriver.getDriver();
    19. @BeforeEach
    20. public void getUrl(){
    21. driver.get("http://58.87.89.71:8009/login.html");
    22. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    23. }
    24. /**
    25. * 主页按钮
    26. */
    27. @Test
    28. public void ListToL() {
    29. driver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
    30. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    31. String str = driver.getCurrentUrl();
    32. Assertions.assertEquals("http://58.87.89.71:8009/login.html",str);
    33. driver.navigate();
    34. }
    35. /**
    36. * 写博客按钮
    37. */
    38. @Test
    39. public void ListToEdit() {
    40. driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
    41. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    42. String str = driver.getCurrentUrl();
    43. Assertions.assertEquals("http://58.87.89.71:8009/blog_add.html",str);
    44. driver.navigate();
    45. }
    46. /**
    47. * 我的主页按钮
    48. */
    49. @Test
    50. public void ListToMyL() {
    51. driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
    52. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    53. String str = driver.getCurrentUrl();
    54. Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
    55. driver.navigate();
    56. }
    57. /**
    58. * 分页功能,第一页
    59. */
    60. @Test
    61. public void ListByPage() {
    62. driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/button[2]")).click();
    63. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    64. Alert alert = driver.switchTo().alert();
    65. String str = alert.getText();
    66. Assertions.assertEquals("当前已经在首页了",str);
    67. alert.accept();
    68. }
    69. }

    5. 写博客页面

    1. package com.webAutoTest.tests;
    2. import com.webAutoTest.common.CommonDriver;
    3. import org.junit.jupiter.api.Assertions;
    4. import org.junit.jupiter.api.BeforeEach;
    5. import org.junit.jupiter.params.ParameterizedTest;
    6. import org.junit.jupiter.params.provider.CsvSource;
    7. import org.openqa.selenium.Alert;
    8. import org.openqa.selenium.By;
    9. import org.openqa.selenium.chrome.ChromeDriver;
    10. import java.time.Duration;
    11. /**
    12. * Created with IntelliJ IDEA.
    13. * Description:写博客页面
    14. * User: WangWZ
    15. * Date: 2023-09-05
    16. * Time: 9:50
    17. */
    18. public class EditTest {
    19. ChromeDriver driver = CommonDriver.getDriver();
    20. @BeforeEach
    21. public void getUrl(){
    22. driver.get("http://58.87.89.71:8009/blog_add.html");
    23. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    24. }
    25. /**
    26. * 标题为空
    27. */
    28. @ParameterizedTest
    29. @CsvSource({"","111111"})
    30. public void EditTitleNull(String title,String content) {
    31. driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
    32. driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
    33. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
    34. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
    35. driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[1]")).click();
    36. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    37. Alert alert = driver.switchTo().alert();
    38. alert.accept();
    39. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    40. Alert alert2 = driver.switchTo().alert();
    41. String str2 =alert.getText();
    42. Assertions.assertEquals("请先输入标题!", str2);
    43. alert.accept();
    44. }
    45. /**
    46. * 内容为空
    47. */
    48. @ParameterizedTest
    49. @CsvSource({"222",""})
    50. public void EditContentNull(String title,String content) {
    51. driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
    52. driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
    53. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
    54. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
    55. driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[1]")).click();
    56. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    57. Alert alert = driver.switchTo().alert();
    58. alert.accept();
    59. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    60. Alert alert2 = driver.switchTo().alert();
    61. String str2 =alert.getText();
    62. Assertions.assertEquals("请先输入文章内容!", str2);
    63. alert.accept();
    64. }
    65. /**
    66. * 发布文章
    67. */
    68. @ParameterizedTest
    69. @CsvSource({"Java精选","数据类型"})
    70. public void EditSub(String title,String content) {
    71. driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
    72. driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
    73. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
    74. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
    75. driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[1]")).click();
    76. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    77. Alert alert = driver.switchTo().alert();
    78. alert.accept();
    79. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    80. Alert alert2 = driver.switchTo().alert();
    81. String str2 =alert.getText();
    82. Assertions.assertEquals("恭喜:文章添加成功!是否继续添加文章?", str2);
    83. alert.dismiss();
    84. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    85. String url = driver.getCurrentUrl();
    86. Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html", url);
    87. driver.navigate();
    88. }
    89. /**
    90. * 存为草稿
    91. */
    92. @ParameterizedTest
    93. @CsvSource({"Java精选2","数据类型2"})
    94. public void EditSubDraft(String title,String content) {
    95. driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
    96. driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
    97. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
    98. driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
    99. driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[2]")).click();
    100. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    101. Alert alert = driver.switchTo().alert();
    102. String str =alert.getText();
    103. Assertions.assertEquals("恭喜:保存草稿成功!", str);
    104. alert.accept();
    105. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    106. String url = driver.getCurrentUrl();
    107. Assertions.assertEquals("http://58.87.89.71:8009/mydraftblog_list.html", url);
    108. driver.navigate();
    109. }
    110. }

    6.  草稿箱页面

    1. package com.webAutoTest.tests;
    2. import com.webAutoTest.common.CommonDriver;
    3. import org.junit.jupiter.api.Assertions;
    4. import org.junit.jupiter.api.BeforeEach;
    5. import org.junit.jupiter.api.Test;
    6. import org.openqa.selenium.Alert;
    7. import org.openqa.selenium.By;
    8. import org.openqa.selenium.chrome.ChromeDriver;
    9. import java.time.Duration;
    10. /**
    11. * Created with IntelliJ IDEA.
    12. * Description: 草稿箱页面
    13. * User: WangWZ
    14. * Date: 2023-09-05
    15. * Time: 9:49
    16. */
    17. public class DraftTest {
    18. ChromeDriver driver = CommonDriver.getDriver();
    19. @BeforeEach
    20. public void getUrl(){
    21. driver.get("http://58.87.89.71:8009/login.html");
    22. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    23. }
    24. /**
    25. * 主页按钮
    26. */
    27. @Test
    28. public void ListToL() {
    29. driver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
    30. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    31. String str = driver.getCurrentUrl();
    32. Assertions.assertEquals("http://58.87.89.71:8009/login.html",str);
    33. driver.navigate();
    34. }
    35. /**
    36. * 写博客按钮
    37. */
    38. @Test
    39. public void ListToEdit() {
    40. driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
    41. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    42. String str = driver.getCurrentUrl();
    43. Assertions.assertEquals("http://58.87.89.71:8009/blog_add.html",str);
    44. driver.navigate();
    45. }
    46. /**
    47. * 我的主页按钮
    48. */
    49. @Test
    50. public void ListToMyL() {
    51. driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
    52. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    53. String str = driver.getCurrentUrl();
    54. Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
    55. driver.navigate();
    56. }
    57. }

    7. 文章详情页面

    1. package com.webAutoTest.tests;
    2. import com.webAutoTest.common.CommonDriver;
    3. import org.junit.jupiter.api.Assertions;
    4. import org.junit.jupiter.api.BeforeEach;
    5. import org.junit.jupiter.api.Test;
    6. import org.openqa.selenium.By;
    7. import org.openqa.selenium.chrome.ChromeDriver;
    8. /**
    9. * Created with IntelliJ IDEA.
    10. * Description:
    11. * User: WangWZ
    12. * Date: 2023-09-05
    13. * Time: 9:49
    14. */
    15. public class DetailTest {
    16. static ChromeDriver driver = CommonDriver.getDriver();
    17. @BeforeEach
    18. public void getUrl(){
    19. driver.get("http://58.87.89.71:8009/mydraftblog_list.html");
    20. }
    21. /*
    22. * 文章文字是否正确
    23. * */
    24. @Test
    25. public void testWz(){
    26. String text = driver.findElement(By.cssSelector("/html/body/div[2]/div[1]/div/div[1]/span[1]")).getText();
    27. Assertions.assertEquals("文章",text);
    28. }
    29. /*
    30. * 分类文字是否正确
    31. * */
    32. @Test
    33. public void testFl(){
    34. String text = driver.findElement(By.cssSelector("/html/body/div[2]/div[1]/div/div[1]/span[2]")).getText();
    35. Assertions.assertEquals("分类",text);
    36. }
    37. }

    三、使用套件Suit执行

    具体Junit注解:Junit 单元测试框架(简单使用)

    1. package com.webAutoTest.tests;
    2. import org.junit.platform.suite.api.SelectClasses;
    3. import org.junit.platform.suite.api.Suite;
    4. /**
    5. * Created with IntelliJ IDEA.
    6. * Description:
    7. * User: WangWZ
    8. * Date: 2023-09-05
    9. * Time: 16:54
    10. */
    11. @Suite
    12. @SelectClasses({LoginTest.class,RegTest.class,LoginTest.class,ListTest.class,EditTest.class,DetailTest.class,DriverQuitTest.class})
    13. public class RunSuit {
    14. }

  • 相关阅读:
    面试官都震惊,你这网络基础可以啊!
    8.1 C++ 标准输入输出流
    java.lang.reflect.Field 解读
    16 Python使用多线程
    嵌入式项目分享| 终极智能手表,全过程+全开源分享
    【毕业设计】python 机器视觉 车牌识别 - opencv 深度学习 机器学习
    农产品行业的春天来了,乘风破浪颠覆传统营销,实现一户带一户,共同致富,这套农产品生态方案可收藏借鉴
    Django + Nginx https部署实战(第一辑)
    基于全同态加密的安全多方计算协议
    信息化发展77
  • 原文地址:https://blog.csdn.net/weixin_60358891/article/details/132684862