Cucumber java + Webdriver(一)

时间:2023-03-09 04:04:45
Cucumber java + Webdriver(一)

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

 <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</groupId>
<artifactId>Cucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Cucumber</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies> </project>

二、新建一个RunCukesTest运行类

 package mypackage;

 import org.junit.runner.RunWith;

 import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class)
@CucumberOptions(
format = {"pretty", "html:target/cucumber"})
public class RunCukesTest { }

三、新建一个文件baiduSearch.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

四、新建一个业务类BaiduSearchStepfs

 package mypackage;

 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 java.util.concurrent.TimeUnit; 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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; 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();
}
}