0

我有一个基于 Java 的 Selenium 设置,它使用 Cucumber 来指定和运行测试。目前,它使用 Chrome 驱动程序运行,但理想情况下,我们将使用 Firefox 和 IE(如果可能的话并行)重复测试,因为本地用户同时使用这三个。我正在使用 Gradle 作为构建工具,并希望坚持下去。

为了尝试并朝着这种方法努力,我实现了一个基于 JUnit 的并行测试设置,该设置在此处进行了描述,它运行良好,并且完全符合我的要求。但是,这不是通过 Cucumber 而是直接在 JUnit 中运行,我看不到一种简单的方法来适应它。

我找到了Zucchini,它似乎完全符合我的要求。然而,这方面的文档确实非常稀少,而且我的经验有限,我无法设置它——我是测试、Selenium 和 Java 的新手,并且继承了我正在尝试扩展的这个框架。这里有一个几乎为我运行的示例(在 Firefox 上失败,但这可能是我的设置),但我无法实现它。基本上在我的实现中,代码运行,打开驱动程序并似乎立即再次关闭它。然后它在场景的第一步返回 NullPointerException(可能是因为它找不到驱动程序)。

我的功能文件:

Feature: Visit Google and do an example search

  Scenario: Navigate to the Google search homepage, search for "Cat", and check the first result is about cats
    Given I visit the Google search page
    When I enter the text "cat" into the search box
    And I click the search button
    Then I should see the top result mentioning cats

我的设置代码(类似于FirstZucchiniTest.java示例 repo):

package stepDefinitions;

import java.util.ArrayList;
import java.util.List;

import com.comcast.zucchini.AbstractZucchiniTest;
import com.comcast.zucchini.TestContext;
import net.dimaj.zucchini.utils.Constants;

import cucumber.api.CucumberOptions;
import cucumber.api.Scenario;
import cucumber.api.java.Before;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

@CucumberOptions(
        features = {"src/test/resources/features"},
        plugin = {"pretty", "json:target/cucumber-report/json/cucumber-report.json", "html:target/cucumber-report"},
        glue={"helpers","stepDefinitions"}
)

public class ZucchiniDefinitions extends AbstractZucchiniTest {
    @Override
    public List<TestContext> getTestContexts() {
        List<TestContext> rv = new ArrayList<TestContext>();

        TestContext chrome = new TestContext("chrome");
        chrome.set(Constants.CONTEXT_BROWSER, new ChromeDriver());
        rv.add(chrome);

        TestContext ff = new TestContext("firefox");
        ff.set(Constants.CONTEXT_BROWSER, new FirefoxDriver());
        rv.add(ff);

        return rv;
    }


    @Override
    public void cleanup(TestContext out) {
        WebDriver driver = out.get(Constants.CONTEXT_BROWSER);
        if (null != driver) {
            driver.quit();
        }
    }

}

访问 Google 的定义:

package stepDefinitions;

import com.comcast.zucchini.TestContext;
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.openqa.selenium.WebDriver;
import pageFactory.examplePages.GoogleSearchHomepage;
import pageFactory.examplePages.GoogleSearchResultsPage;

import static net.dimaj.zucchini.utils.Constants.CONTEXT_BROWSER;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class GoogleDefinitions {
    private WebDriver driver;

    public GoogleDefinitions (BaseDefinitions baseDefinitions) {

        this.driver = TestContext.getCurrent().get(CONTEXT_BROWSER);
        System.out.println("got driver");
    }

    @Given("^I visit the Google search page$")
    public void iVisitTheGoogleSearchPage() {
        GoogleSearchHomepage googleSearchHomepage = new GoogleSearchHomepage(driver);
        googleSearchHomepage.visit();
    }

    @When("^I enter the text \"([^\"]*)\" into the search box$")
    public void iEnterTheTextIntoTheSearchBox(String textToEnter) {
        GoogleSearchHomepage googleSearchHomepage = new GoogleSearchHomepage(driver);
        googleSearchHomepage.typeInSearchField(textToEnter);
    }

    @And("^I click the search button$")
    public void iClickTheSearchButton() {
        GoogleSearchHomepage googleSearchHomepage = new GoogleSearchHomepage(driver);
        googleSearchHomepage.clickSearchButton();
    }

    @Then("^I should see the top result mentioning cats$")
    public void iShouldSeeTheTopResultMentioningCats() {
        GoogleSearchResultsPage googleSearchResultsPage = new GoogleSearchResultsPage(driver);
        assertThat(googleSearchResultsPage.getTextOfResult(1).toLowerCase(), containsString("cat"));
    }
}

最后是 Google 主页的页面定义:

package pageFactory.examplePages;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import pageFactory.SuperPage;

public class GoogleSearchHomepage extends SuperPage {

    @FindBy(css = ".gsfi[name='q']")
    public WebElement searchTextField;

    @FindBy(css = "[value='Google Search'][name='btnK']")
    public WebElement searchButton;

    public GoogleSearchHomepage(WebDriver driver) {
        super(driver);
    }

    public void typeInSearchField(String text) {
        searchTextField.sendKeys(text);
    }

    public GoogleSearchResultsPage clickSearchButton() {
        searchTextField.sendKeys(Keys.RETURN);
        return new GoogleSearchResultsPage(driver);
    }

    public GoogleSearchHomepage visit() {
        driver.get("https://www.google.co.uk/");
        System.out.println("Google acquired");
        return new GoogleSearchHomepage(driver);
    }


}

和谷歌搜索结果页面:

package pageFactory.examplePages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import pageFactory.SuperPage;

import java.util.List;

public class GoogleSearchResultsPage extends SuperPage {

    @FindBy(className = "r:first-of-type")
    public List<WebElement> searchTextField;

    public GoogleSearchResultsPage(WebDriver driver) {
        super(driver);
    }

    public String getTextOfResult(Integer resultNumberToGet) {
        return searchTextField.get(resultNumberToGet).getText();
    }

}

常量由原始示例 repo 提供,如下所示:

package net.dimaj.zucchini.utils;

public class Constants {
    public static final String CONTEXT_BROWSER = "browser";
    public static final String CONTEXT_SCENARIO = "scenarior";


}

我的问题是为什么这个设置失败了?

4

0 回答 0