当前位置: 首页 > news >正文

一般做网站价格怎样做搜索引擎推广

一般做网站价格,怎样做搜索引擎推广,广东深圳属于什么地区,网站建设mng目录 前言: 1.博客前端页面测试用例图 2.测试用例的代码实现 2.1登录页面的测试 2.2博客列表页面的测试 2.3写博客测试 2.4博客详情页面的测试 2.5已发布博客的标题和时间的测试 2.6注销用户的测试 结束语: 前言: 之前小编给大家讲…

目录

前言:

1.博客前端页面测试用例图

2.测试用例的代码实现

2.1登录页面的测试

2.2博客列表页面的测试

2.3写博客测试

2.4博客详情页面的测试

2.5已发布博客的标题和时间的测试

2.6注销用户的测试

结束语:


前言:

之前小编给大家讲解了有关于Selenium和Junit5自动化测试的一些基础知识,那么下面我们就针对于我们自己做的一个项目来使用Junit来进行一下自动化测试。

博客项目的前后端博客链接:前端☞http://t.csdn.cn/jZkQd  后端☞http://t.csdn.cn/sN1Uq

博客项目的源码Gitee链接☞https://gitee.com/YAUGAOLELE/project

1.博客前端页面测试用例图

2.测试用例的代码实现

代码侧边的展示:

我们一共创建两个类一个是BlogCase另一个是InitAndEnd。具体代码的创建请看下边。

初始化代码的实现:

package BlogTest;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;//用来放置初始化的操作以及最后的收尾工作
public class InitAndEnd {//创建驱动static WebDriver webDriver;//初识化操作,打开浏览器@BeforeAllstatic void SetUp() {webDriver = new ChromeDriver();}//关闭浏览器@AfterAllstatic void TearDown() {webDriver.quit();}
}

2.1登录页面的测试

代码展示:

package BlogTest;import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;import java.util.concurrent.TimeUnit;@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{/*** 输入正确的账号,密码,登录成功*/@Order(1)@ParameterizedTest@CsvFileSource(resources = "LoginSuccess.csv")void LoginSuccess(String username, String password, String blog_list_url){System.out.println(username + password + blog_list_url);//打开博客登录页面webDriver.get("http://43.138.29.216:8080/blog_system/login.html");webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入账号:zhangsan /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString cur_url = webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//列表页展示的是zhangsan/lisi//用户名是zhangsan测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(username,cur_admin);}
}


结果展示:

2.2博客列表页面的测试

代码展示:

package BlogTest;import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;import java.util.concurrent.TimeUnit;@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{/*** 输入正确的账号,密码,登录成功*/@Order(1)@ParameterizedTest@CsvFileSource(resources = "LoginSuccess.csv")void LoginSuccess(String username, String password, String blog_list_url){System.out.println(username + password + blog_list_url);//打开博客登录页面webDriver.get("http://43.138.29.216:8080/blog_system/login.html");webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入账号:zhangsan /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString cur_url = webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//列表页展示的是zhangsan/lisi//用户名是zhangsan测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(username,cur_admin);}/***博客列表页面的博客数量不为0*/@Order(2)@Testvoid BlogList() {//打开博客列表页webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过//加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);int title_num = webDriver.findElements(By.cssSelector(".title")).size();//断言:如果这个元素的数量不为0,则认为测试通过Assertions.assertNotEquals(0,title_num);}
}


结果展示:

2.3写博客测试

代码展示:

package BlogTest;import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;import java.util.concurrent.TimeUnit;import static java.lang.Thread.sleep;@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{/*** 输入正确的账号,密码,登录成功*/@Order(1)@ParameterizedTest@CsvFileSource(resources = "LoginSuccess.csv")void LoginSuccess(String username, String password, String blog_list_url){System.out.println(username + password + blog_list_url);//打开博客登录页面webDriver.get("http://43.138.29.216:8080/blog_system/login.html");webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入账号:zhangsan /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString cur_url = webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//列表页展示的是zhangsan/lisi//用户名是zhangsan测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(username,cur_admin);}/***博客列表页面的博客数量不为0*/@Order(2)@Testvoid BlogList() {//打开博客列表页webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过//加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);int title_num = webDriver.findElements(By.cssSelector(".title")).size();//断言:如果这个元素的数量不为0,则认为测试通过Assertions.assertNotEquals(0,title_num);}/*** 写博客*/@Order(3)@Testvoid EditBlog() throws InterruptedException {//找到写博客按钮,点击webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//找到对应的输入框,输入对应的标题。webDriver.findElement(By.cssSelector("#title-input"));//通过JS进行标题输入webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");sleep(3000);//点击提交webDriver.findElement(By.cssSelector("#submit")).click();sleep(3000);//校验//获取当前页面的urlString cur_url = webDriver.getCurrentUrl();Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);}
}


结果展示:

2.4博客详情页面的测试

代码展示:

package BlogTest;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.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;import static java.lang.Thread.sleep;@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{private static Stream<Arguments> Generator() {return Stream.of(Arguments.arguments("http://43.138.29.216:8080/blog_system/blog_detail.html","博客详情页","自动化测试"));}/*** 输入正确的账号,密码,登录成功*/@Order(1)@ParameterizedTest@CsvFileSource(resources = "LoginSuccess.csv")void LoginSuccess(String username, String password, String blog_list_url){System.out.println(username + password + blog_list_url);//打开博客登录页面webDriver.get("http://43.138.29.216:8080/blog_system/login.html");webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入账号:zhangsan /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString cur_url = webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//列表页展示的是zhangsan/lisi//用户名是zhangsan测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(username,cur_admin);}/***博客列表页面的博客数量不为0*/@Order(2)@Testvoid BlogList() {//打开博客列表页webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过//加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);int title_num = webDriver.findElements(By.cssSelector(".title")).size();//断言:如果这个元素的数量不为0,则认为测试通过Assertions.assertNotEquals(0,title_num);}/*** 写博客*/@Order(3)@Testvoid EditBlog() throws InterruptedException {//找到写博客按钮,点击webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//找到对应的输入框,输入对应的标题。webDriver.findElement(By.cssSelector("#title-input"));//通过JS进行标题输入webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");sleep(3000);//点击提交webDriver.findElement(By.cssSelector("#submit")).click();sleep(3000);//校验//获取当前页面的urlString cur_url = webDriver.getCurrentUrl();Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);}/*** 博客详情页面的校验* 1.校验url* 2.校验博客标题* 3.页面title是“博客详情页”的测试*/@Order(4)@ParameterizedTest@MethodSource("Generator")void BlogDetail(String expected_url, String expected_title, String expected_blog_title) {//找到第一篇博客对应查看全文的按钮webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/a")).click();//获取当前页面的urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_title = webDriver.getTitle();//获取博客标题webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();//校验webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);Assertions.assertEquals(expected_title,cur_title);Assertions.assertEquals(expected_blog_title,cur_blog_title);if (cur_url.contains(expected_url)) {System.out.println("测试通过");}else {System.out.println(cur_url);System.out.println("测试不通过");}}
}


结果展示:

2.5已发布博客的标题和时间的测试

代码展示:

package BlogTest;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.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;import static java.lang.Thread.sleep;@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{private static Stream<Arguments> Generator() {return Stream.of(Arguments.arguments("http://43.138.29.216:8080/blog_system/blog_detail.html","博客详情页","自动化测试"));}/*** 输入正确的账号,密码,登录成功*/@Order(1)@ParameterizedTest@CsvFileSource(resources = "LoginSuccess.csv")void LoginSuccess(String username, String password, String blog_list_url){System.out.println(username + password + blog_list_url);//打开博客登录页面webDriver.get("http://43.138.29.216:8080/blog_system/login.html");webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入账号:zhangsan /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString cur_url = webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//列表页展示的是zhangsan/lisi//用户名是zhangsan测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(username,cur_admin);}/***博客列表页面的博客数量不为0*/@Order(2)@Testvoid BlogList() {//打开博客列表页webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过//加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);int title_num = webDriver.findElements(By.cssSelector(".title")).size();//断言:如果这个元素的数量不为0,则认为测试通过Assertions.assertNotEquals(0,title_num);}/*** 写博客*/@Order(3)@Testvoid EditBlog() throws InterruptedException {//找到写博客按钮,点击webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//找到对应的输入框,输入对应的标题。webDriver.findElement(By.cssSelector("#title-input"));//通过JS进行标题输入webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");sleep(3000);//点击提交webDriver.findElement(By.cssSelector("#submit")).click();sleep(3000);//校验//获取当前页面的urlString cur_url = webDriver.getCurrentUrl();Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);}/*** 博客详情页面的校验* 1.校验url* 2.校验博客标题* 3.页面title是“博客详情页”的测试*/@Order(4)@ParameterizedTest@MethodSource("Generator")void BlogDetail(String expected_url, String expected_title, String expected_blog_title) {//找到第一篇博客对应查看全文的按钮webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/a")).click();//获取当前页面的urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_title = webDriver.getTitle();//获取博客标题webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();//校验webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);Assertions.assertEquals(expected_title,cur_title);Assertions.assertEquals(expected_blog_title,cur_blog_title);if (cur_url.contains(expected_url)) {System.out.println("测试通过");}else {System.out.println(cur_url);System.out.println("测试不通过");}}@Order(5)@Test/*** 校验已发布博客的标题* 校验已发布博客时间*/void BlogInfoChecked() {webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取第一篇博客的标题String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();//获取第一篇博客的发布时间String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();//校验博客标题是不是自动化测试Assertions.assertEquals("自动化测试",first_blog_title);//如果是2023-09-03发布的,测试通过if (first_blog_time.contains("2023-09-03")) {System.out.println("测试通过");}else {System.out.println("当前时间是:" + first_blog_time);System.out.println("测试不通过");}}}


结果展示: 

2.6注销用户的测试

代码展示:

package BlogTest;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.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;import static java.lang.Thread.sleep;@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{private static Stream<Arguments> Generator() {return Stream.of(Arguments.arguments("http://43.138.29.216:8080/blog_system/blog_detail.html","博客详情页","自动化测试"));}/*** 输入正确的账号,密码,登录成功*/@Order(1)@ParameterizedTest@CsvFileSource(resources = "LoginSuccess.csv")void LoginSuccess(String username, String password, String blog_list_url){System.out.println(username + password + blog_list_url);//打开博客登录页面webDriver.get("http://43.138.29.216:8080/blog_system/login.html");webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入账号:zhangsan /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString cur_url = webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//列表页展示的是zhangsan/lisi//用户名是zhangsan测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(username,cur_admin);}/***博客列表页面的博客数量不为0*/@Order(2)@Testvoid BlogList() {//打开博客列表页webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过//加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);int title_num = webDriver.findElements(By.cssSelector(".title")).size();//断言:如果这个元素的数量不为0,则认为测试通过Assertions.assertNotEquals(0,title_num);}/*** 写博客*/@Order(3)@Testvoid EditBlog() throws InterruptedException {//找到写博客按钮,点击webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//找到对应的输入框,输入对应的标题。webDriver.findElement(By.cssSelector("#title-input"));//通过JS进行标题输入webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");sleep(3000);//点击提交webDriver.findElement(By.cssSelector("#submit")).click();sleep(3000);//校验//获取当前页面的urlString cur_url = webDriver.getCurrentUrl();Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);}/*** 博客详情页面的校验* 1.校验url* 2.校验博客标题* 3.页面title是“博客详情页”的测试*/@Order(4)@ParameterizedTest@MethodSource("Generator")void BlogDetail(String expected_url, String expected_title, String expected_blog_title) {//找到第一篇博客对应查看全文的按钮webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/a")).click();//获取当前页面的urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_title = webDriver.getTitle();//获取博客标题webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();//校验webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);Assertions.assertEquals(expected_title,cur_title);Assertions.assertEquals(expected_blog_title,cur_blog_title);if (cur_url.contains(expected_url)) {System.out.println("测试通过");}else {System.out.println(cur_url);System.out.println("测试不通过");}}@Order(5)@Test/*** 校验已发布博客的标题* 校验已发布博客时间*/void BlogInfoChecked() {webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");//获取第一篇博客的标题String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();//获取第一篇博客的发布时间String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();//校验博客标题是不是自动化测试Assertions.assertEquals("自动化测试",first_blog_title);//如果是2023-09-03发布的,测试通过if (first_blog_time.contains("2023-09-03")) {System.out.println("测试通过");}else {System.out.println("当前时间是:" + first_blog_time);System.out.println("测试不通过");}}/*** 注销*/@Order(6)@Testvoid Logout() {webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();//校验url(登录)webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();Assertions.assertEquals("http://43.138.29.216:8080/blog_system/login.html",cur_url);//校验提交按钮webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);Assertions.assertNotNull(webElement);}
}


结果展示:

结束语:

好了这节小编就给大分享到这里啦,希望这节对大家有关于使用Junit5的自动化测试有一定帮助,想要学习的同学记得关注小编和小编一起学习吧!如果文章中有任何错误也欢迎各位大佬及时为小编指点迷津(在此小编先谢过各位大佬啦!)

http://www.hkea.cn/news/409172/

相关文章:

  • 阜蒙县建设学校网站是什么北京seo编辑
  • 珠海建设局网站十大经典事件营销案例分析
  • 创建网站开发公司互联网推广引流是做什么的
  • 万盛集团网站建设seo网站推广全程实例
  • 做教育的网站需要资质吗网站怎么开发
  • 微网站怎么做滚动中国万网域名注册官网
  • 个人如何免费建网站seo在线优化工具 si
  • 双线主机可以做彩票网站吗网络推广合作协议
  • 做外贸的b2b网站域名批量查询系统
  • 建设网站需要哪些职位网站建设策划书
  • 苏州网站建设哪里好网站点击排名优化
  • 网站建设收费标准策划百度推广关键词越多越好吗
  • 网站怎么做更新吗如何建立网页
  • 国外建设工程招聘信息网站tool站长工具
  • 专业做相册书的网站电商网站建设制作
  • 银川网站开发公司电话东莞网
  • 环境保护局网站管理制度建设百度指数的主要功能有
  • 安装wordpress提示500错误关键词优化的策略有哪些
  • 企业网站建设公司排名深圳高端seo公司助力企业
  • 做网站套餐网站seo
  • 网站上的代码网页怎么做的下载百度软件
  • 网站功能模块建设搜狗推广
  • 网站做推广有用吗网站页面设计
  • 做简报的网站广州搜发网络科技有限公司
  • 南乐县住房和城乡建设局网站制作网站的步骤是什么
  • 金华做网站最专业的公司搜易网提供的技术服务
  • wordpress适合门户网站吗怎么营销自己的产品
  • 常用的网站类型有哪些seo优化专员编辑
  • 网站专题框架怎么做海阳seo排名
  • 手机网站代码下载黄页网站推广服务