玩转PPT 2018-01-21
Cucumber行为驱动,
简称BDD,
其核心思想是把自然语言转换成代码;
但在敏捷开发的过程中,
这种东西极大的束缚了测试人员的手脚,
感觉它像封建时代的八股文,
要遵守严格的韵律,
反正我个人十分反感;
就像在做功能测试的时候,
那种基于Excel文档的测试。
用Maven构建Cucumber依赖:
<dependency><br /> <groupId>info.cukes</groupId><br /> <artifactId>cucumber-java</artifactId><br /> <scope>test</scope><br /> <version>1.2.5</version><br /> </dependency><br /><br /> <dependency><br /> <groupId>info.cukes</groupId><br /> <artifactId>cucumber-jvm</artifactId><br /> <version>1.2.5</version><br /> <type>pom</type><br /> </dependency><br /><br /> <dependency><br /> <groupId>info.cukes</groupId><br /> <artifactId>cucumber-testng</artifactId><br /> <version>1.2.4</version><br /> </dependency><br /></dependencies><br /><br /><br />baiduSearch.feature配置文件:
# language: zh-CN<br /><br />功能: 百度搜索的测试用例<br /> 场景大纲: 分别搜索<word><br /> 假如我打开火狐浏览器<br /> 当输入百度的网址后,页面跳转到"https://www.baidu.com/"<br /> 当输入<word>,点击搜索按钮之后<br /> 那么页面标题会变为<result><br /> 同时关闭火狐浏览器<br /> 例子:<br /> |word |result |<br /> |Selenium |Selenium_百度搜索 |<br /> |JMeter |JMeter_百度搜索 |<br /> |Appium |Appium_百度搜索 |
<br /><br />这个文件是可以直接运行的,会在控制台输出:
Undefined step: 假如 我打开火狐浏览器
Undefined step: 当 输入百度的网址后,页面跳转到"https://www.baidu.com/"
Undefined step: 当 输入Selenium,点击搜索按钮之后
Undefined step: 那么 页面标题会变为Selenium_百度搜索
Undefined step: 同时 关闭火狐浏览器
Undefined step: 假如 我打开火狐浏览器
Undefined step: 当 输入百度的网址后,页面跳转到"https://www.baidu.com/"
Undefined step: 当 输入JMeter,点击搜索按钮之后
Undefined step: 那么 页面标题会变为JMeter_百度搜索
Undefined step: 同时 关闭火狐浏览器
Undefined step: 假如 我打开火狐浏览器
Undefined step: 当 输入百度的网址后,页面跳转到"https://www.baidu.com/"
Undefined step: 当 输入Appium,点击搜索按钮之后
3 Scenarios (3 undefined)
15 Steps (15 undefined)
0m0.000s
You can implement missing steps with the snippets below:
@假如("^我打开火狐浏览器$")
public void 我打开火狐浏览器() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@当("^输入百度的网址后,页面跳转到\"([^\"]*)\"$")
public void 输入百度的网址后_页面跳转到(String arg1) throws Throwable {
Undefined step: 那么 页面标题会变为Appium_百度搜索
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@当("^输入Selenium,点击搜索按钮之后$")
public void 输入selenium_点击搜索按钮之后() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@那么("^页面标题会变为Selenium_百度搜索$")
public void 页面标题会变为selenium_百度搜索() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@那么("^关闭火狐浏览器$")
public void 关闭火狐浏览器() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@当("^输入JMeter,点击搜索按钮之后$")
public void 输入jmeter_点击搜索按钮之后() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@那么("^页面标题会变为JMeter_百度搜索$")
public void 页面标题会变为jmeter_百度搜索() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@当("^输入Appium,点击搜索按钮之后$")
public void 输入appium_点击搜索按钮之后() throws Throwable {
Undefined step: 同时 关闭火狐浏览器
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@那么("^页面标题会变为Appium_百度搜索$")
public void 页面标题会变为appium_百度搜索() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();}
测试用例类CucumberBaidu.java:
import cucumber.api.java.zh_cn.假如;<br />import cucumber.api.java.zh_cn.同时;<br />import cucumber.api.java.zh_cn.当;<br />import cucumber.api.java.zh_cn.那么;<br />import org.openqa.selenium.WebDriver;<br />import org.openqa.selenium.WebElement;<br />import org.openqa.selenium.firefox.FirefoxDriver;<br />import org.openqa.selenium.support.FindBy;<br />import org.openqa.selenium.support.PageFactory;<br />import org.testng.Assert;<br /><br />public class CucumberBaidu {<br /><br /> private static WebDriver driver;<br /><br /> @FindBy(xpath = ".//*[@id='kw']")<br /> private WebElement inputBox;<br /> //输入框<br /><br /> @FindBy(xpath = ".//*[@id='su']")<br /> private WebElement searchButton;<br /> //搜索按钮<br /><br /> @假如("^我打开火狐浏览器$")<br /> public void openFirefox() throws Throwable{<br /> System.setProperty("webdriver.firefox.marionette",<br /> "src/main/resourcec/geckodriver.exe");<br /> driver = new FirefoxDriver();<br /> PageFactory.initElements(driver, this);<br /> driver.manage().window().maximize();<br /> }<br /><br /> @当("^输入百度的网址后,页面跳转到\"(.*)\"$")<br /> public void openBaiduHomePage(String url) throws Throwable{<br /> driver.get(url);<br /> //不需要去声明百度首页的地址,因为它会从配置文件里面读取<br /> }<br /><br /> @当("^输入(.*),点击搜索按钮之后$")<br /> public void searchChina(String searchWord) throws Throwable{<br /> inputBox.sendKeys(searchWord);<br /> searchButton.click();<br /> Thread.sleep(2000);<br /> }<br /><br /> @那么("^页面标题会变为(.*)$")<br /> public void keyword(String searchResult) throws Throwable{<br /> Assert.assertEquals(driver.getTitle(), searchResult);<br /> Thread.sleep(2000);<br /> }<br /><br /> @同时("^关闭火狐浏览器$")<br /> public void quit(){<br /> driver.close();<br /> driver.quit();<br /> }<br />}
<br /><br />驱动类CucumberDriver.java:
import cucumber.api.CucumberOptions;<br />import cucumber.api.testng.AbstractTestNGCucumberTests;<br /><br />@CucumberOptions(<br /> features = "baiduSearch.feature",<br /> format = {"pretty",<br /> "html:target/cucumber-html-report",<br /> "json:target/cucumber-json-report.json"}<br /> )<br />/*指定cucumber.feature文件,在工程的根目录下<br /> 命令行/控制台输出日志<br /> 生成html测试报告<br /> 生成json测试报告*/<br /><br />public class CucumberDriver extends AbstractTestNGCucumberTests {<br /><br />}<br /><br /><br />运行一把,查看测试报告: