serenity bdd:如何在黄瓜BDD中重复使用测试步骤(@steps)(即使用pico容器)

时间:2018-05-10 06:56:04

标签: bdd serenity-bdd

使用Serenity BDD + selenium + Junit进行自动化。我有黄瓜BDD的经验,我们可以很容易地分享步骤。我想在平静中实现同样的目标。 为简单起见,我们假设我们有登录页面,后续页面说名称检查页。我为这两个页面创建了单独的POM,然后创建了相应的步骤定义(@steps),并希望在单个脚本中使用在这两个步骤定义类中定义的@steps。可以这样做吗?

  1. 我已尝试使用此功能但浏览器会在特定步骤类完成后立即关闭,以便其他步骤不会执行并提供异常

    public class AA_NameCheck{
    
        @Managed(driver="edge",clearCookies=ClearCookiesPolicy.BeforeEachTest)
        WebDriver driver;
    
        @Steps
        StepsLoginPage stepL;
        StepsNameCheckPage stepN;
    
        @Test
        public void NameCheck_ValidUser() throws InterruptedException{
            stepL.Launch();
            stepL.Login_ValidCredential();
            stepN.Enter_NameDetails();
    
            }
    }
    
  2. stepL.Login_validCredentials()完成后浏览器关闭,因此Name_EnterDetails()上的方法发生异常

    1. 我尝试过而不是创建单独的步骤定义来保持POM中的@steps,它起作用但是在平静报告中这些步骤是不可见的。
    2. 只是想知道其他方法可以分享步骤吗?

      分享POM和分步定义,请看看

      POM for Name Check Page:

      public class NameCheckPage extends PageObject {
      
          public NameCheckPage(WebDriver driver) {
              super(driver);
          }
      
          Actions action=new Actions(getDriver());
      
      
          @FindBy(id="xx")
          public WebElementFacade EngName;
      
          @FindBy(id="xx")
          public WebElementFacade SecondEngName;
      
          @FindBy(id="xx")
          public WebElementFacade ThirdEngName;
      
      
          @FindBy(xpath="xx")
          public List<WebElementFacade> NameCheckBtns;
      
          @FindBy(id="xx")
          public WebElementFacade PreliminaryStatusChk;
      
      
          public void Enter_FirstEngName() {
              EngName.isVisible();
              EngName.sendKeys("xylon");
          }
      
          public void Enter_EnglishName(String name) {
              EngName.isVisible();
              EngName.sendKeys(name);
          }
      
          public void Enter_SecEngName() {
              SecondEngName.isVisible();
              SecondEngName.sendKeys("xylonone");
          }
      
          public void Enter_ThirdEngName() {
              ThirdEngName.isVisible();
              ThirdEngName.sendKeys("xylontwo");
          }
      
          public void Enter_FirstEngNameone() {
              EngName.isVisible();
              EngName.sendKeys("xylo");
          }
      
          public void Enter_SecEngNameone() {
              SecondEngName.isVisible();
              SecondEngName.sendKeys("xyloone");
          }
      
          public void Enter_ThirdEngNameone() {
              ThirdEngName.isVisible();
              ThirdEngName.sendKeys("xylotwo");
          }
      
          public void AddCompany() {
      
              action.click(NameCheckBtns.get(1)).perform();
          }
      
          public void AddSecondCompany() {
              action.click(NameCheckBtns.get(2)).perform();
              }
      
          public void PrelimCheck() throws InterruptedException {
      
              for(WebElement btns:NameCheckBtns) {
                  System.out.println("Buttons :"+btns.getText());
              }
      
              System.out.println(NameCheckBtns.get(4).getText());
      
      
              JavascriptExecutor js=(JavascriptExecutor)getDriver();
              js.executeScript("arguments[0].scrollIntoView();", NameCheckBtns.get(4));
      
              action.click(NameCheckBtns.get(4)).perform();
              PreliminaryStatusChk.isVisible();
              System.out.println(PreliminaryStatusChk.getText());
          }
      

      登录页面的步骤定义:

      public class StepsLoginPage extends ScenarioSteps  {
          private static final long serialVersionUID = 1L;
          LoginPage login;
      
          @Step
          public void Launch() {
          login.Launch();
          }
          @Step
          public void Login_ValidCredential() {
              login.Launch();
              login.Enter_Username();
              login.Enter_Password();
              login.Click_LoginBtn();
               //tried using return but of no help the browser still closes
              //return new NameCheckPage(login.getDriver());
      }
      

      名称检查页面的步骤定义:

      public class StepsNameCheckPage extends ScenarioSteps{
          private static final long serialVersionUID = 1L;
          NameCheckPage namechk;
          @Step
          public void Enter_NameDetails() throws InterruptedException {
              namechk.Enter_FirstEngName();
              namechk.AddCompany();
              namechk.Enter_SecEngName();
              namechk.AddCompany();
              namechk.Enter_ThirdEngName();
              namechk.PrelimCheck();
      }
      

      测试:

      public class AA_NameCheck{
      
          @Managed(driver="edge",clearCookies=ClearCookiesPolicy.BeforeEachTest)
          WebDriver driver;
      
          @Steps
          StepsLoginPage stepL;
          StepsNameCheckPage stepN;
      
          @Test
          public void NameCheck_ValidUser() throws InterruptedException{
              stepL.Launch();
              stepL.Login_ValidCredential();
              stepN.Enter_NameDetails();
      
              }
      }
      

1 个答案:

答案 0 :(得分:-1)

当您在注释@Steps中使用带有步骤的两个类时,您应该具有:

@Steps
StepsLoginPage stepL; 
@Steps 
StepsNameCheckPage stepN;

在您的情况下,第二堂课为空。