Cucumber java + Webdriver(一)

一、打开Eclipse,新建一个maven项目,打开pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5 
 6     <groupId>Cucumber</groupId>
 7     <artifactId>Cucumber</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>Cucumber</name>
12     <url>http://maven.apache.org</url>
13     <repositories>
14         <repository>
15             <id>sonatype-snapshots</id>
16             <url>https://oss.sonatype.org/content/repositories/snapshots</url>
17             <snapshots>
18                 <enabled>true</enabled>
19             </snapshots>
20         </repository>
21     </repositories>
22 
23     <properties>
24         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>io.cucumber</groupId>
30             <artifactId>cucumber-junit</artifactId>
31             <version>2.3.1</version>
32             <scope>test</scope>
33         </dependency>
34         <dependency>
35             <groupId>junit</groupId>
36             <artifactId>junit</artifactId>
37             <version>4.12</version>
38             <scope>test</scope>
39         </dependency>
40         <dependency>
41             <groupId>io.cucumber</groupId>
42             <artifactId>cucumber-java</artifactId>
43             <version>2.3.1</version>
44             <scope>test</scope>
45         </dependency>
46         <dependency>
47             <groupId>org.seleniumhq.selenium</groupId>
48             <artifactId>selenium-java</artifactId>
49             <version>3.5.3</version>
50         </dependency>
51     </dependencies>
52 
53 </project>
View Code

二、新建一个RunCukesTest运行类

 1 package mypackage;
 2 
 3 import org.junit.runner.RunWith;
 4 
 5 import cucumber.api.CucumberOptions;
 6 import cucumber.api.junit.Cucumber;
 7 
 8 @RunWith(Cucumber.class)
 9 @CucumberOptions( 
10         format = {"pretty", "html:target/cucumber"})
11 public class RunCukesTest {
12 
13 }
View Code

三、新建一个文件baiduSearch.feature

1 Feature: 百度搜索  
2       打开百度进行搜索  
3   
4   Scenario: 百度搜索selenium  
5     Given Go to baidu home  
6     When  I find baidu logo  
7     And   Type the search text "selenium"  
8     And   Click the submit  
9     Then  Wait the query result
View Code

四、新建一个业务类BaiduSearchStepfs

 1 package mypackage;
 2 
 3 import cucumber.api.java.en.And;
 4 import cucumber.api.java.en.Given;
 5 import cucumber.api.java.en.Then;
 6 import cucumber.api.java.en.When;
 7 
 8 import java.util.concurrent.TimeUnit;
 9 
10 import org.junit.Assert;
11 import org.openqa.selenium.By;
12 import org.openqa.selenium.WebDriver;
13 import org.openqa.selenium.WebElement;
14 import org.openqa.selenium.chrome.ChromeDriver;
15 import org.openqa.selenium.support.ui.ExpectedConditions;
16 import org.openqa.selenium.support.ui.WebDriverWait;
17 
18 public class BaiduSearchStepfs {
19     private WebDriver driver;
20 
21     @Given("^Go to baidu home$")
22     public void go_to_baidu_home() throws Exception {
23         driver = new ChromeDriver();
24         driver.manage().window().maximize();
25         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
26         driver.get("http://www.baidu.com/");
27     }
28 
29     @When("^I find baidu logo")
30     public WebElement i_find_baidu_logo() {
31         WebDriverWait wait = new WebDriverWait(driver, 10);
32         WebElement element = wait
33                 .until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id='lg']/img"))));
34         return element;
35     }
36 
37     @And("^Type the search text \"([^\"]*)\"$")
38     public void type_the_search_text(String searchText) throws Throwable {
39         driver.findElement(By.id("kw")).clear();
40         driver.findElement(By.id("kw")).sendKeys(searchText);
41     }
42 
43     @And("^Click the submit$")
44     public void click_the_submit() {
45         driver.findElement(By.id("su")).click();
46     }
47 
48     @Then("^Wait the query result")
49     public void wait_the_query_result() throws InterruptedException {
50         Thread.sleep(10000);
51         Assert.assertEquals("selenium_百度搜索", driver.getTitle());
52         driver.close();
53         driver.quit();
54     }
55 }
View Code

 

posted @ 2018-03-08 17:22  Sunny*  阅读(242)  评论(0编辑  收藏  举报