什么网站可以做问卷调查,番禺网站制作价格,wordpress主页 摘要,苏州外包公司排名环境搭建#xff1a;
浏览器#xff1a; 本次测试使用Chrome浏览器在jdk的bin目录下安装对应浏览器驱动#xff08;尽量选择与浏览器版本相近的驱动#xff09;chromedriver.storage.googleapis.com/index.htmlJunit依赖#xff1a; !-- https://mvnreposit…环境搭建
浏览器 本次测试使用Chrome浏览器在jdk的bin目录下安装对应浏览器驱动尽量选择与浏览器版本相近的驱动chromedriver.storage.googleapis.com/index.htmlJunit依赖 !-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --dependencygroupIdorg.junit.jupiter/groupId!--编写用例的基本注解--artifactIdjunit-jupiter-api/artifactIdversion5.9.1/version/dependencydependency!--参数化测试依赖--groupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-params/artifactIdversion5.9.1/version/dependencydependency!--这个库提供JUnit平台的核心功能比如共享的测试接口、注解和工具类--groupIdorg.junit.platform/groupIdartifactIdjunit-platform-commons/artifactIdversion1.9.1/version !-- 请根据需要调整为与JUnit 5.9.1兼容的版本 --/dependency!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite --dependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-suite/artifactIdversion1.9.1/versionscopetest/scope/dependencydependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-suite-api/artifactIdversion1.9.1/version/dependency!--JUnit Jupiter的测试引擎实现了JUnit Platform的TestEngine接口。它负责发现和执行使用JUnit Jupiter API编写的测试。--dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.9.1/versionscopetest/scope/dependency Selenium依赖 dependencygroupIdorg.seleniumhq.selenium/groupIdartifactIdselenium-java/artifactIdversion3.141.59/version/dependencydependencygroupIdorg.seleniumhq.selenium/groupIdartifactIdselenium-support/artifactIdversion3.141.59/version/dependency
测试用例
网站登陆页面
项目结构
InitAndQuit进行测试初始化和收尾工作
TestItem包含对各个页面基本功能的自动化测试用例 初始化以及资源关闭
/*** ClassName InitAndQuit* Description 初识化测试相关以及测试结束资源关闭* Author 86153* Date 2024/4/30 10:20* Version 1.0**/
public class InitAndQuit {static WebDriver webDriver;BeforeAllstatic void init() {webDriver new ChromeDriver();}AfterAllstatic void quit() {webDriver.quit();}
}
登录页面测试用例 输入给定邮箱点击获取验证码
//验证码private static String emailCode;//在注册页面点击获取验证码按钮ParameterizedTestCsvFileSource(resources login.csv)Order(0)void loginTest(String account) throws InterruptedException {webDriver.get(http://8.130.70.131:8080/login.html);webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);webDriver.findElement(By.cssSelector(#username)).sendKeys(account);WebDriverWait wait new WebDriverWait(webDriver,10);//这里 获取验证码 和 提交按钮为俩个一样的button所以需要进行按钮的选择WebElement clickElement wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(#submit)));ListWebElement elements webDriver.findElements(By.cssSelector(#submit));elements.get(0).click();}
从邮箱中拿到验证码 /*邮箱登录拿到验证码*/TestOrder(1)void getCaptcha() throws InterruptedException {webDriver.get(https://www.baidu.com/);webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);webDriver.findElement(By.cssSelector(.s_ipt)).sendKeys(https://mail.qq.com/);webDriver.findElement(By.cssSelector(#su)).click();webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);//保存当前窗口的句柄String originalWindow webDriver.getWindowHandle();webDriver.findElement(By.cssSelector(#\\31 div div:nth-child(1) h3 a)).click();//切换窗口SetString set webDriver.getWindowHandles();for(String cur : set) {if(!cur.equals(originalWindow)) {webDriver.switchTo().window(cur);}}//登录邮箱//注意这里切换frame时要先去选择获取到frameWebElement iframe webDriver.findElement(By.cssSelector(#QQMailSdkTool_login_loginBox_qq iframe));webDriver.switchTo().frame(iframe);WebElement iframe1 webDriver.findElement(By.cssSelector(#ptlogin_iframe));webDriver.switchTo().frame(iframe1);//点击用户头像进行登录电脑登陆了QQwebDriver.findElement(By.cssSelector(#img_out_3224881242)).click();sleep(10000);//进入对应邮件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();//拿到验证码WebElement element webDriver.findElement(By.cssSelector(#readmail_content_html span));String text element.getText();emailCode text;}
登录 //登陆页面测试ParameterizedTestCsvFileSource(resources login.csv)Order(2)void loginTest(String account,String password) throws InterruptedException {//进入登陆页面webDriver.get(http://8.130.70.131:8080/login.html);//隐式等待webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//输入账号密码验证码webDriver.findElement(By.cssSelector(#username)).sendKeys(account);webDriver.findElement(By.cssSelector(#password)).sendKeys(password);webDriver.findElement(By.cssSelector(#captcha)).sendKeys(emailCode);//显示等待WebDriverWait wait new WebDriverWait(webDriver,10);//这里 获取验证码 和 提交按钮为俩个一样的button所以需要进行按钮的选择WebElement clickElement wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(#submit)));ListWebElement elements webDriver.findElements(By.cssSelector(#submit));elements.get(1).click();//显示等待等待弹窗出现//注意这里是个坑弹窗不属于页面内的元素不能使用隐式等待wait.until(ExpectedConditions.alertIsPresent());//弹窗选择webDriver.switchTo().alert().accept();//等待进入新页面webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//校验urlString currentURL webDriver.getCurrentUrl();Assertions.assertEquals(http://8.130.70.131:8080/myblog_list.html,currentURL);}列表页测试用例 //列表页自动化测试/*** 如果列表页博客数量不为0表示测试通过**/TestOrder(3)void listTest() {webDriver.get(hhttp://8.130.70.131:8080/blog_list.html);webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);int size webDriver.findElements(By.cssSelector(.title)).size();System.out.println(size);Assertions.assertNotEquals(0,size);}
个人列表页测试用例 //个人列表页测试TestOrder(4)void selfListTest() {webDriver.get(http://8.130.70.131:8080/myblog_list.html);//文章数量不为0测试ListWebElement list webDriver.findElements(By.cssSelector(.title));int size webDriver.findElements(By.cssSelector(.title)).size();System.out.println(size);Assertions.assertNotEquals(0,list.size());//测试“主页”按钮webDriver.findElement(By.cssSelector(body div.nav a:nth-child(4))).click();webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);String curUrl webDriver.getCurrentUrl();Assertions.assertEquals(http://8.130.70.131:8080/blog_list.html,curUrl);//回退到个人主页webDriver.navigate().back();//测试“写博客按钮”webDriver.findElement(By.cssSelector(body div.nav a:nth-child(5))).click();webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);curUrl webDriver.getCurrentUrl();Assertions.assertEquals(http://8.130.70.131:8080/blog_add.html,curUrl);webDriver.navigate().back();/* //测试“注销”按钮webDriver.findElement(By.cssSelector(body div.nav a:nth-child(6))).click();webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);curUrl webDriver.getCurrentUrl();Assertions.assertEquals(http://8.130.70.131:8080/login.html,curUrl);*/}
博客详情页测试用例 //博客详情页测试ParameterizedTestOrder(5)CsvFileSource(resources detail.csv)void detailTest(String destUrl,String destPageTitle,String destBlogTitle) {//进入列表页webDriver.get(http://8.130.70.131:8080/blog_list.html);//点击文章详情按钮进入博客详情页webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) a)).click();//校验页面url页面title博客题目webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);String url webDriver.getCurrentUrl();String pageTitle webDriver.getTitle();String blogTitle webDriver.findElement(By.cssSelector(#title)).getText();Assertions.assertEquals(destUrl,url);Assertions.assertEquals(destPageTitle,pageTitle);Assertions.assertEquals(destBlogTitle,blogTitle);}
编辑页测试用例 //博客编辑页测试ParameterizedTestOrder(6)CsvFileSource(resources edit.csv)void editTest(String data,String title) {webDriver.get(http://8.130.70.131:8080/blog_add.html);webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);/* //直接使用通过js设置文章标题((JavascriptExecutor)webDriver).executeScript(document.getElementById(\title\).value\自动化测试\);*/webDriver.findElement(By.cssSelector(#title)).sendKeys(自动化测试);//发布文章webDriver.findElement(By.cssSelector(body div.blog-edit-container div.title button)).click();//这里有一个”是否继续添加博客“的弹窗进行选择后才跳转WebDriverWait wait new WebDriverWait(webDriver,5);wait.until(ExpectedConditions.alertIsPresent());webDriver.switchTo().alert().dismiss();webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);//校验url跳转String cur_url webDriver.getCurrentUrl();Assertions.assertEquals(http://8.130.70.131:8080/myblog_list.html,cur_url);//校验第一篇博客的发布时间标题String publishDate webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) div.date)).getText();String publishTitle webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) div.title)).getText();Assertions.assertEquals(title,publishTitle);if(publishDate.contains(data)) {System.out.println(测试通过);}else {System.out.println(发布时间错误);}//删除博客webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) a:nth-child(6))).click();webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);//弹窗选择wait.until(ExpectedConditions.alertIsPresent());webDriver.switchTo().alert().accept();//校验第一篇博客是否被删除WebElement after_delete_title webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) div.title));Assertions.assertNotEquals(title,after_delete_title);}
注销 //注销TestOrder(7)void login_out() {webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);//点击注销按钮webDriver.findElement(By.cssSelector(body div.nav a:nth-child(6))).click();//显示等待alter弹窗出现WebDriverWait wait new WebDriverWait(webDriver,5);wait.until(ExpectedConditions.alertIsPresent());//选择弹框并进行确定webDriver.switchTo().alert().accept();webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);String cur_url webDriver.getCurrentUrl();Assertions.assertEquals(http://8.130.70.131:8080/login.html,cur_url);}