您的位置:首页 > 编程语言 > Java开发

Cucumber java + Webdriver (2) 开始编写第一个Test

2015-10-21 10:46 513 查看
第一个Test,我们写个百度搜索的功能,下面是具体的实现过程

1、在intellij idea创建一个Maven项目Cucumber_FirstTest,在pom.xml添加如下依赖



具体内容是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>Cucumber_FirstTest</groupId>
<artifactId>Cucumber_FirstTest</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</build>

</project>


之后在编辑器右侧找到Maven Projects插件,在Cucumber_First项目下的LifeCycle中点击“test”,如下图所示标识1。待项目构建完成后,再点击Maven Projects中的Reimports按钮,如下图所示标识的 2。该操作是让maven把我们需要用到的Lib自动下载下来。如果之前已经下载过了,则只要单击标识2就可以 :



下图表示上述标识1构建完成后的日志



2、定义第一个feature

Cucumber_FirstTest项目中,在目录test下新建一个目录 resources.



接着,在resources下,新建feature目录,新建文件 baiduSearch.feature. 并在该文档中写第一个feature:



文档内容如下所示:
Feature: 百度搜索
打开百度进行搜索

Scenario: 百度搜索selenium
Given Go to baidu home
When  I find baidu logo
And   Type the search text "selenium"
And   Click the submit
Then  Wait the query result


3、上述feature具体step的实现

3.1 建RunCukesTest.java类

Cucumber_FirstTest项目中,在目录test->java下新建一个目录com,之后在com目录下再建一个cucumber目录,并在该目录下新建一个类RunCukesTest.java



类RunCukesTest.java的内容如下

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/feature/"},
format = {"pretty", "html:target/cucumber", "json:target/cucumber.json"},
glue = {"com.cucumber"}
)
public class RunCukesTest {
}
之后在编辑器右侧找到Maven Projects插件,在Cucumber_First项目下的LifeCycle中点击 “test”,在日志中可看到记录



从上图可看到,日志提示我们,有一个场景,五个步骤需要运行,但是我们都没有实现这些步骤。所以后面就需要实现这五个step

3.2 具体实现step的代码

在cucumber下新建baidu目录,并在该目录下新建一个类BaiduSearchStepfs.java

该类就是上述 baiduSearch.feature 文件真正对应的step文件

在类BaiduSearchStepfs.java输入下面内容:

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.WebDriver;
import org.openqa.selenium.WebElement;

public class BaiduSearchStepfs {
private WebDriver driver;

@Given("^Go to baidu home$")
public void go_to_baidu_home() throws Exception {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.baidu.com/");
}

@When("^I find baidu logo")
public WebElement i_find_baidu_logo() {
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 {
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys(searchText);
}

@And("^Click the submit$")
public void click_the_submit() {
driver.findElement(By.id("su")).click();
}

@Then("^Wait the query result")
public void wait_the_query_result() throws InterruptedException {
Thread.sleep(10000);
Assert.assertEquals("selenium_百度搜索", driver.getTitle());
driver.close();
driver.quit();
}
}
之后在编辑器右侧找到Maven Projects插件,在Cucumber_First项目下的LifeCycle中点击 “test”,在日志中可看到记录



则我们第一个Test完成......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: