Cucumber行為驅動
Cucumber行為驅動,
簡稱BDD,
其核心思想是把自然語言轉換成代碼;
但在敏捷開發的過程中,
這種東西極大的束縛了測試人員的手腳,
感覺它像封建時代的八股文,
要遵守嚴格的韻律,
反正我個人十分反感;
就像在做功能測試的時候,
那種基於Excel文檔的測試。
用Maven構建Cucumber依賴:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
baiduSearch.feature配置文件:
# language: zh-CN
功能: 百度搜索的測試用例
場景大綱: 分別搜索<word>
假如我打開火狐瀏覽器
當輸入百度的網址後,頁面跳轉到"https://www.baidu.com/"
當輸入<word>,點擊搜索按鈕之後
那麽頁面標題會變為<result>
同時關閉火狐瀏覽器
例子:
|word |result |
|Selenium |Selenium_百度搜索 |
|JMeter |JMeter_百度搜索 |
|Appium |Appium_百度搜索 |
這個文件是可以直接運行的,會在控制臺輸出:
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.假如;
import cucumber.api.java.zh_cn.同時;
import cucumber.api.java.zh_cn.當;
import cucumber.api.java.zh_cn.那麽;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
public class CucumberBaidu {
public static WebDriver driver;
@假如("^我打開火狐瀏覽器$")
public void openFirefox() throws Throwable{
System.setProperty("webdriver.firefox.marionette",
"src/main/resourcec/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@當("^輸入百度的網址後,頁面跳轉到\"(.*)\"$")
public void openBaiduHomePage(String url) throws Throwable{
driver.get(url);
}
@當("^輸入(.*),點擊搜索按鈕之後$")
public void searchChina(String searchWord) throws Throwable{
driver.findElement(By.xpath(".//*[@id=‘kw‘]"))
.sendKeys(searchWord);
driver.findElement(By.xpath(".//*[@id=‘su‘]"))
.click();
Thread.sleep(2000);
}
@那麽("^頁面標題會變為(.*)$")
public void keyword(String searchResult) throws Throwable{
Assert.assertEquals(driver.getTitle(), searchResult);
Thread.sleep(2000);
}
@同時("^關閉火狐瀏覽器$")
public void quit(){
driver.close();
driver.quit();
}
}
驅動類CucumberDriver.java:
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(
features = "baiduSearch.feature",
format = {"pretty",
"html:target/cucumber-html-report",
"json:target/cucumber-json-report.json"}
)
/*指定cucumber.feature文件,在工程的根目錄下
命令行/控制臺輸出日誌
生成html測試報告
生成json測試報告*/
public class CucumberDriver extends AbstractTestNGCucumberTests {
}
運行一把,查看測試報告:
Cucumber行為驅動