1

我的功能文件是 search.feature。并且有两个 stepdefinitions 文件名为:Backgroundstepdef 和 Searchstepdef。我必须在 Searchstepdef 中使用 Backgroundstepdef 的状态。如何使用 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 cucumber.api.java.ObjectFactory = hgtest.utils.SharedContext

据我了解,使用 SharedContext 类我可能会将 Backgroundstepsdef 的状态转换为 Searchstepsdef

4

0 回答 0