黄瓜和WebDriver,在不需要的时候打开chrome



我从黄瓜开始,遵循一些教程等。 我有两个功能文件,一个包含您在Cucumber的"入门">中看到的基本内容,另一个来自WebDriver的"入门"。

测试运行成功,但 Cucumber 会打开浏览器,即使对于不要求它的功能/步骤也是如此。

project
|--src/test/java/packages
|--StepDefinitions.java
|--RunCucumberTest.java
|--GoogleSearchSteps.java
|--src/test/resources/packages
|--google_search.feature
|--is_it_friday_yet.feature

is_it_friday_yet.特点:

Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
| day            | answer |
| Monday         | Nope   |
| Tuesday        | Nope   |
| Wednesday      | Nope   |
| Thursday       | Nope   |
| Friday         | YES    |
| Saturday       | Nope   |
| Sunday         | Nope   |
| 12345                  | Nope   |
| Icecream       | Nope   |

google_search.特点:

Feature: Google Search Cheese
Example of how to test web pages with cucumber
Scenario: Finding some cheese
Given I am on the Google search page
When I search for "Cheese!"
Then the page title should start with "cheese"

步骤定义.java

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import static org.junit.Assert.*;

class IsItFriday {
static String isItFriday(String today) {
return "Friday".equals(today) ? "YES" : "Nope";
}
}
public class StepDefinitions {
private String today;
private String actualAnswer;
@Given("today is {string}")
public void today_is(String today) {
this.today = today;
}
@When("I ask whether it's Friday yet")
public void i_ask_whether_it_s_Friday_yet() {
actualAnswer = IsItFriday.isItFriday(today);
}
@Then("I should be told {string}")
public void i_should_be_told(String expectedAnswer) {
assertEquals(expectedAnswer, actualAnswer);
}

谷歌搜索步骤.java

import io.cucumber.java.After;
import io.cucumber.java.Before;
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.WebDriverWait;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class GoogleSearchSteps {
private WebDriver driver;
@Before
public void createDriver() {
System.setProperty("webdriver.chrome.driver","mypath\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
}
@Given("I am on the Google search page")
public void i_am_on_the_Google_search_page() {
driver.get("https:\www.google.com");
}
@When("I search for {string}")
public void i_search_for(String query) {
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys(query);
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
}
@Then("the page title should start with {string}")
public void the_page_title_should_start_with(String titleStartsWith) {
// Google's search is rendered dynamically with JavaScript
// Wait for the page to load timeout after ten seconds
new WebDriverWait(driver, 10L).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith(titleStartsWith);
}
});
}
@After()
public void closeBrowser() {
driver.quit();
}

测试运行正常,它们基本上是从教程中复制和粘贴的,并进行了一些修改,并且它们也成功运行,问题实际上是为每个测试打开了一个浏览器窗口。

那么,如何使WebDriver仅用于GoogleSearch功能呢?

提前谢谢。

要仅将WebDriver用于 Google 搜索功能场景,您应该使用条件挂钩。您可以将Before钩子与以下标签表达式相关联:

@Before("@browser")
public void createDriver() {
...
}

在功能文件中:

@browser
Feature: Google Search Cheese
Example of how to test web pages with cucumber
...

您的@Before注释函数将为每个测试生成一个新的 WebDriver 实例。

你可以跳过它,

比如
@Before
public void createDriver(Scenario scenario) {
if(!scenario.getName().equals("GoogleSearch")){
System.setProperty("webdriver.chrome.driver","mypath\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
}  
}

注意:您需要将scenario传递给createDriver()方法。

最新更新