我有一个基于Java的Selenium设置,它使用Cucumber来指定和运行测试。目前,这是使用Chrome驱动程序运行的,但是理想情况下,我们会使用Firefox和IE(如果可能的话,并行进行)重复测试,因为本地用户会同时使用这三个。我正在使用Gradle作为构建工具,并希望坚持使用。
为了尝试采用这种方法,我实现了一个基于JUnit的并行测试设置,该设置描述为here,该设置工作出色,完全可以满足我的要求。但是,这不是通过Cucumber运行的,而是直接在JUnit中运行的,我看不到一种简单的方法来适应它。
我找到了Zucchini,它似乎完全可以实现我想要的功能。但是,关于此的文档确实确实很稀疏,并且由于我的经验有限,我无法对其进行设置-我对Selenium和Java还是陌生的,并且继承了我试图扩展的这个框架。有一个示例here几乎可以为我运行(在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
):
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);
}
}
和Google搜索结果页:
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();
}
}
常量由原始示例存储库提供,外观如下:
package net.dimaj.zucchini.utils;
public class Constants {
public static final String CONTEXT_BROWSER = "browser";
public static final String CONTEXT_SCENARIO = "scenarior";
}
我的问题是为什么此设置失败?