Intellij IDEA + Maven + Cucumber 項目 (二): 創建第一個Test


 

 

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的位置引起的。可自行百度如何解決。

若出現其他錯誤,注意查配置文件中依賴的庫是否是最新版本,有可能是兼容性引起的問題。更新庫到最新版本一般可解決問題。


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM