结构..
src/test/java
config
TestConfiguration.java
hooks
WebDriverHooks.java
nicebank
RunSteps.java
OtherSteps..
support
ATMUserInterface.java
KnowsTheDomain.java
在@Autowired
包中放置步骤时, KnowsTheDomain
正在正确注入nicebank
。但是,当我将@Autowired
和KnowsTheDomain
WebDriverHooks
ATMUserInterface
在自动装配到不同的软件包时是否需要配置注释?我正在运行Cucumber跑步者..
从WebDriverHook.java和ATMUserInterface.java,字段private KnowsTheDomain helper;
返回null而不是单例实例。当我运行nicebank
包中的步骤时,我需要它们返回它返回的内容。
任何人都知道为什么这个helper
字段为空?
TestConfiguration.java
@Configuration
@ComponentScan(basePackages = { "support"})
public class TestConfiguration {
@Bean
public static KnowsTheDomain knowsTheDomain() {
return new KnowsTheDomain();
}
}
WebDriverHooks.java
@ContextConfiguration(classes = TestConfiguration.class, loader=AnnotationConfigContextLoader.class)
@Configurable(autowire = Autowire.BY_TYPE)
public class WebDriverHooks {
@Autowired
private KnowsTheDomain helper;
@After
public void finish(Scenario scenario) {
try {
byte[] screenshot =
helper.getWebDriver().getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
} catch (WebDriverException somePlatformsDontSupportScreenshots) {
System.err.println(somePlatformsDontSupportScreenshots.getMessage());
}
finally {
helper.getWebDriver().close();
}
}
}
RunSteps.java - 这会运行Cucumber跑步者..
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "html:out"},
snippets = SnippetType.CAMELCASE,
features = "classpath:cucumber",
dryRun = false)
@ContextConfiguration(classes = TestConfiguration.class)
public class RunSteps {
}
KnowsTheDomain.java
@Component
public class KnowsTheDomain {
private Account myAccount;
private CashSlot cashSlot;
private Teller teller;
private EventFiringWebDriver webDriver;
public Account getMyAccount() {
if (myAccount == null) {
myAccount = new Account();
}
return myAccount;
}
public CashSlot getCashSlot() {
if (cashSlot == null) {
cashSlot = new CashSlot();
}
return cashSlot;
}
public Teller getTeller() {
if (teller == null) {
teller = new ATMUserInterface();
}
return teller;
}
public EventFiringWebDriver getWebDriver() {
if (webDriver == null) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver_win32/chromedriver.exe");
webDriver = new EventFiringWebDriver(new ChromeDriver());
}
return webDriver;
}
}
ATMUserInterface.java
@ContextConfiguration(classes = TestConfiguration.class, loader=AnnotationConfigContextLoader.class)
@Configurable(autowire = Autowire.BY_TYPE)
public class ATMUserInterface implements Teller {
@Autowired
private KnowsTheDomain helper;
@Override
public void withdrawFrom(Account account, int dollars) {
try {
helper.getWebDriver().get("http://localhost:" + ServerHooks.PORT);
helper.getWebDriver().findElement(By.id("Amount"))
.sendKeys(String.valueOf(dollars));
helper.getWebDriver().findElement(By.id("Withdraw")).click();
} catch (Exception e) {
System.err.println("err" + e);
}
}
}
答案 0 :(得分:0)
@Autowired仅适用于bean。
因此,请确保您在哪里使用@Autowired注释。
将钩子包添加到@ComponentScan,如下所示
@ComponentScan(basePackages = {" support"," hooks"})