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

问题描述 投票:0回答:2

我从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:

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:

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"

StepsDefinition.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);
    }

GoogleSearchSteps.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功能?

提前感谢。

java selenium-chromedriver cucumber-java
2个回答
0
投票
您可能会用类似的方法跳过它>

@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()方法。


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.