1. 定义第一个Feature
- 在目录test下新建一个目录 resources.
- 接着,在resources下,新建feature目录,新建文件 baiduSearch.feature.
- 并在该文档中写第一个feature:
2. 在目录test->java下新建一个目录com,之后在com目录下再建一个cucumber目录,并在该目录下新建一个类RunCukesTest.java
内容如下:
3. 之后在编辑器右侧找到Maven Projects插件,在Cucumber_First项目下的LifeCycle中点击 “test”,在日志中可看到记录:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.cucumber.RunCukesTest
Feature: baidu search
open baidu and search
Scenario: baidu search selenium # baiduSearch.feature:4
Given Go to baidu.com
When I find baidu logo
And Type the search text "selenium"
And Click the submit
Then wait the query result
1 Scenarios (1 undefined)
5 Steps (5 undefined)
0m0.000s
You can implement missing steps with the snippets below:
@Given("^Go to baidu\\.com$")
public void go_to_baidu_com() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^I find baidu logo$")
public void i_find_baidu_logo() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Type the search text \"([^\"]*)\"$")
public void type_the_search_text(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Click the submit$")
public void click_the_submit() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^wait the query result$")
public void wait_the_query_result() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Tests run: 7, Failures: 0, Errors: 0, Skipped: 6, Time elapsed: 0.512 sec
Results :
Tests run: 7, Failures: 0, Errors: 0, Skipped: 6
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.144 s
[INFO] Finished at: 2017-09-22T15:49:00+08:00
[INFO] Final Memory: 21M/170M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
从上图可看到,日志提示我们,有一个场景,五个步骤需要运行,但是我们都没有实现这些步骤。所以后面就需要实现这五个step
4. 具体实现step的代码
在cucumber下新建baidu目录,并在该目录下新建一个类BaiduSearchStepfs.java
该类就是上述 baiduSearch.feature 文件真正对应的step文件
在类BaiduSearchStepfs.java输入下面内容:
package com.cucumber.baidu;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;
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.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* Created by yuanjulie on 17/9/22.
*/
public class baiduSearchStepfs {
private WebDriver driver;
@Given("^Go to baidu\\.com$")
public void go_to_baidu_com() throws Exception {
// Write code here that turns the phrase above into concrete actionsT)
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.baidu.com/");
// throw new PendingException();
}
@When("^I find baidu logo$")
public WebElement i_find_baidu_logo() {
// Write code here that turns the phrase above into concrete actions
//throw new PendingException();
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement element = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id='lg']/img"))));
return element;
}
@And("^Type the search text \"([^\"]*)\"$")
public void type_the_search_text(String searchText) throws Throwable {
// Write code here that turns the phrase above into concrete actions
// throw new PendingException();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys(searchText);
}
@And("^Click the submit$")
public void click_the_submit() throws Throwable {
// Write code here that turns the phrase above into concrete actions
//throw new PendingException();
driver.findElement(By.id("su")).click();
}
@Then("^wait the query result$")
public void wait_the_query_result() throws InterruptedException {
// Write code here that turns the phrase above into concrete actions
// throw new PendingException();
Thread.sleep(10000);
Assert.assertEquals("selenium_百度搜索",driver.getTitle());
}
}
5. 之后在编辑器右侧找到Maven Projects插件,在Cucumber_First项目下的LifeCycle中点击 “test”,在日志中可看到记录
T E S T S
-------------------------------------------------------
Running com.cucumber.RunCukesTest
Feature: baidu search
open baidu and search
Starting ChromeDriver 2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061) on port 28566
Only local connections are allowed.
九月 22, 2017 5:28:49 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
Scenario: baidu search selenium # baiduSearch.feature:4
Given Go to baidu.com # baiduSearchStepfs.go_to_baidu_com()
When I find baidu logo # baiduSearchStepfs.i_find_baidu_logo()
And Type the search text "selenium" # baiduSearchStepfs.type_the_search_text(String)
And Click the submit # baiduSearchStepfs.click_the_submit()
Then wait the query result # baiduSearchStepfs.wait_the_query_result()
1 Scenarios (1 passed)
5 Steps (5 passed)
0m13.233s
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 13.841 sec
Results :
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.007 s
[INFO] Finished at: 2017-09-22T17:29:01+08:00
[INFO] Final Memory: 20M/198M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
备注:若无法正确启动chrome 报如下错误:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;
则是没有正确设置chromedriver的位置引起的。可自行百度如何解决。
若出现其他错误,注意查配置文件中依赖的库是否是最新版本,有可能是兼容性引起的问题。更新库到最新版本一般可解决问题。