我的功能文件是search.feature。并且有两个名为step的步骤定义文件:Backgroundstepdef和Searchstepdef。我必须将Backgroundstepdef的状态使用到Searchstepdef中。如何通过DI注入使用Backgroundstepdef中的步骤状态?
我写了扩展picofactory的SharedContext类。
search.feature如下:
Feature: Test Search page using search textbox also using filter and sort
@login
Background: User is Logged In
Given User navigate to the login page
When User submit username "tahashin.egp@outlook.com" and password "123456"
Then User will click Sign In button
And User will see Account menu
Then User will enter text "Honey"
Scenario: Searching with name of honey
Given User is Suceesfully loged in
Given User will enter text "honey"
And User will see press enter or click search button
Then User will see the list of honey
Scenario: Search with invalid data
Given User will enter invalid text
And User will press enter or click serach icon
Then User will see the message invalid search
================================================ ========================
Backgroundstepdef如下:
public class BackgroundStepDef {
WebDriver driver;
LoginPage loginPage;
String afterSignIn;
private SharedContext sharedContext;
//HelperClass helper;
//constructor for picocontainer DI Injection
public BackgroundStepDef(SharedContext sharedContext){
this.sharedContext=sharedContext;
}
@Given("^User navigate to the login page$")
public void userNavigateToTheLoginPage() throws Throwable {
// Write code here that turns the phrase above into concrete actions
//driver=HelperClass.initializeDriver();
//loginPage=new LoginPage();
driver=sharedContext.driver;
loginPage= PageGenerator.GetInstance(LoginPage.class,driver);
}
@When("^User submit username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void userSubmitUsernameAndPassword(String username, String
password) throws Throwable {
// Write code here that turns the phrase above into concrete actions
loginPage.enterUserName(username);
loginPage.enterPassword(password);
}
@Then("^User will click Sign In button$")
public void userWillClickSignInButton() throws Throwable {
// Write code here that turns the phrase above into concrete actions
loginPage.enterSignIn();
Thread.sleep(3000);
}
@And("^User will see Account menu$")
public void userWillSeeAccountMenu() throws Throwable {
// Write code here that turns the phrase above into concrete actions
afterSignIn=loginPage.verifylinkText();
}
}
================================================ ================== searchstepdef如下所示:
public class SearchSteps {
WebDriver driver;
SearchPage serachPage;
String afterSignIn;
SharedContext sharedContext;
public SearchSteps(SharedContext sharedContext){
this.sharedContext=sharedContext;
}
@Given("^User is Suceesfully loged in$")
public void userIsSuceesfullyLogedIn() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Given("^User will enter text \"([^\"]*)\"$")
public void enterSearchText(String text){
//driver=HelperClass.initializeDriver();
//serachPage=new SearchPage();
serachPage=PageGenerator.GetInstance(SearchPage.class,driver);
serachPage.enterSearchtext("prubita");
}
@And("^User will see press enter or click search button$")
public void userWillSeePressEnterOrClickSearchButton() throws Throwable {
// Write code here that turns the phrase above into concrete actions
serachPage.pressEnter();
}
@Then("^User will see the list of honey$")
public void userWillSeeTheListOfHoney() throws Throwable {
// Write code here that turns the phrase above into concrete actions
}
@Given("^User will enter invalid text$")
public void userWillEnterInvalidText() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@And("^User will press enter or click serach icon$")
public void userWillPressEnterOrClickSerachIcon() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^User will see the message invalid search$")
public void userWillSeeTheMessageInvalidSearch() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
SharedContext如下:
package hgtest.utils;
public class SharedContext extends PicoFactory {
public WebDriver driver;
public Scenario scenario;
public String stepInfo;
public String scenarioName;
public String tagname;
public String getScenarioname(){
scenarioName=scenario.getName();
return scenarioName;
}
public SharedContext(){
addClass(BackgroundStepDef.class);
addClass(SearchSteps.class);
}
}
cuumber.properties cumul.api.java.ObjectFactory = hgtest.utils.SharedContext
据我了解,使用SharedContext类可以将Backgroundstepsdef的状态添加到Searchstepsdef