2.1在Eclipse中使用JUnit和英文语言进行行为驱动测试
测试逻辑:
(1)访问126邮箱
(2)再访问163邮箱
(3)输入用户名密码登录163邮箱
(4)成功登录后退出邮箱
被测试网址:
http://mail.163.com
BBD实施步骤:
(1)在Eclipse的BDDProj工程文件夹上单击鼠标右键,在弹出的快捷菜单中选择new-》Folder命令,在弹出的对话框的Folder name 输入框中输入Feature,并单击Finish按钮Feature文件夹新的新建
(2)在Feature文件夹上单击鼠标右键,在弹出的快捷菜单中选择new-》File命令,在弹出的对话框中输入Login_test.feature,单击Finish按钮
(3)在Eclipse中双击Login_test.feature文件进行编辑,在编辑区输入如下内容
@tag Feature: login and logout @tag1 Scenario: Successful Login with Valid Credentials Given User is on EmailTypeList Page When User Navigata to 163 Mail LogIn Page And User enters UserName and Password Then Message displayed Login Successfully @tag2 Scenario: Successful LogOut When User LogOut from the Application Then Message displayed LogOut Successfully
(4)在cucumberTest 包下新建一个文件类,名称为TestRunner.java其源码如下
package cn.cucumberTest; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; /*设定feature在工程中的路径,“Feature”表示在工程根目录下 * glue参数设定BDD自动化测试代码所在的Package名称 * format参数设定生成HTML格式的报告,并将报告生成路径target/cucumber-html-report * */ @RunWith(Cucumber.class) @CucumberOptions(features = "Feature",glue = {"cn.stepDefintion"},format = {"html:target/cucumber-html-report"}) public class TestRunner { }
(5)在编辑区单击鼠标右键,在弹出的快捷菜单中选择Run As-》JUnit Test命令运行后Console控制台显示如下代码
@Given("^User is on EmailTypeList Page$") public void user_is_on_EmailTypeList_Page() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @When("^User Navigata to (\\d+) Mail LogIn Page$") public void user_Navigata_to_Mail_LogIn_Page(int arg1) throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @When("^User enters UserName and Password$") public void user_enters_UserName_and_Password() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @Then("^Message displayed Login Successfully$") public void message_displayed_Login_Successfully() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @When("^User LogOut from the Application$") public void user_LogOut_from_the_Application() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @Then("^Message displayed LogOut Successfully$") public void message_displayed_LogOut_Successfully() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); }
(7)在stepDefinition Package中新建一个类,命名为Login_Test.java,将步骤(6)中的部分代码复制到类中,去掉// Write code here that turns the phrase above into concrete actions和throw new PendingException();这两行代码,并添加3个注解的import代码行
package cn.stepDefintion; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class Login_Test { @Given("^User is on EmailTypeList Page$") public void user_is_on_EmailTypeList_Page() throws Throwable { } @When("^User Navigata to (\\d+) Mail LogIn Page$") public void user_Navigata_to_Mail_LogIn_Page(int arg1) throws Throwable { } @When("^User enters UserName and Password$") public void user_enters_UserName_and_Password() throws Throwable { } @Then("^Message displayed Login Successfully$") public void message_displayed_Login_Successfully() throws Throwable { } @When("^User LogOut from the Application$") public void user_LogOut_from_the_Application() throws Throwable { } @Then("^Message displayed LogOut Successfully$") public void message_displayed_LogOut_Successfully() throws Throwable { } }
(8)在Cucumber的Package下,新建一个测试类并自定义命名(例如TestCucumber),编写并调试完成所有测试场景的自动化测试代码。具体代码如下
package cn.cucumberTest; import static org.junit.Assert.*; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class TestCucumber { public static WebDriver driver; public static String baseUrl = "https://email.126.com/"; @Before public void setUp() throws Exception { System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void testLoginAndLogout() throws InterruptedException { driver.get(baseUrl); driver.get("https://email.163.com/"); //进入登录框所在的frame driver.switchTo().frame(0); WebElement userName = driver.findElement(By.xpath("//input[@placeholder=‘邮箱账号或手机号码‘]")); WebElement password = driver.findElement(By.xpath("//input[@placeholder=‘输入密码‘]")); WebElement loginButton = driver.findElement(By.id("dologin")); //清空用户和密码输入框 userName.clear(); password.clear(); //输入用户名密码 userName.sendKeys("m17805983076"); password.sendKeys("*****"); //单击按钮 loginButton.click(); //等待5秒完成登录 Thread.sleep(5000); //断言判断页面是否包含未读邮件 assertTrue(driver.getPageSource().contains("未读邮件")); System.out.println("登录成功"); Thread.sleep(5000); WebElement logoutLink = driver.findElement(By.xpath("//a[text()=‘退出‘]")); //单击退出 logoutLink.click(); Thread.sleep(5000); //断言页面是否包含您已成功退出网易邮箱。判断已退出邮箱 assertTrue(driver.getPageSource().contains("您已成功退出网易邮箱。")); System.out.println("成功退出"); driver.quit(); } }
(9)根据Login_test.feature文件中场景操作的步骤,将步骤(8)中的代码按照测试拆分到Login_Test测试类中,实现后的代码如下
package cn.stepDefintion; import static org.junit.Assert.assertTrue; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class Login_Test { public static WebDriver driver; public static String baseUrl ="https://email.126.com/"; @Given("^User is on EmailTypeList Page$") public void user_is_on_EmailTypeList_Page() throws Throwable { System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(baseUrl); } @When("^User Navigata to (\\d+) Mail LogIn Page$") public void user_Navigata_to_Mail_LogIn_Page(int arg1) throws Throwable { driver.get("https://email.163.com/"); Thread.sleep(5000); } @When("^User enters UserName and Password$") public void user_enters_UserName_and_Password() throws Throwable { /*WebDriverWait wait = new WebDriverWait(driver,15); wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@placeholder=‘邮箱账号或手机号码‘]")));*/ driver.switchTo().frame(0); WebElement userName = driver.findElement(By.xpath("//input[@placeholder=‘邮箱账号或手机号码‘]")); WebElement password = driver.findElement(By.xpath("//input[@placeholder=‘输入密码‘]")); WebElement loginButton = driver.findElement(By.id("dologin")); //清空用户和密码输入框 userName.clear(); password.clear(); //输入用户名密码 userName.sendKeys("m17805983076"); password.sendKeys("*****"); //单击按钮 loginButton.click(); } @Then("^Message displayed Login Successfully$") public void message_displayed_Login_Successfully() throws Throwable { //等待5秒完成登录 Thread.sleep(5000); //断言判断页面是否包含未读邮件 assertTrue(driver.getPageSource().contains("未读邮件")); System.out.println("登录成功"); } @When("^User LogOut from the Application$") public void user_LogOut_from_the_Application() throws Throwable { Thread.sleep(5000); WebElement logoutLink = driver.findElement(By.xpath("//a[text()=‘退出‘]")); //单击退出 logoutLink.click(); } @Then("^Message displayed LogOut Successfully$") public void message_displayed_LogOut_Successfully() throws Throwable { Thread.sleep(5000); //断言页面是否包含您已成功退出网易邮箱。判断已退出邮箱 assertTrue(driver.getPageSource().contains("您已成功退出网易邮箱。")); System.out.println("成功退出"); driver.quit(); } }
(10)运行TestRunner.java,可以看到BDD的自动化测试用例被成功执行,并显示出每个测试步骤的执行结果
(11)在工程的target\cucumber-html-report目录下可以看到BDD的HTML版本的测试报告,如图
原文:https://www.cnblogs.com/z-zzz/p/10592786.html