可以在Cucumber(java)中创建“Master Step”吗?

问题描述 投票:0回答:2

可以在Cucumber(java)中创建“Master Step”吗?

例如,我创建了许多使用重复代码的步骤文件,重复的代码在每个步骤文件中初始化浏览器等。

甚至可以创建一个主步骤文件,它将容纳驱动程序设置等,因此在每个步骤之前使用'Cucumber Before'执行设置。

我的代码:

public class LoginStep {

WebDriver driver;
LoginPage loginPage;

@Before
public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\deltaUser\\Desktop\\CucumberFramework\\PimCucumberFramework\\src\\test\\java\\resources\\other\\chromedriver.exe");
    this.driver = new ChromeDriver();
    this.driver.manage().window().maximize();
    this.driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

    loginPage = PageFactory.initElements(driver, LoginPage.class);
}

@Given("^User is on the PIM login page$")
public void user_is_on_the_PIM_login_page() throws Throwable {
    loginPage.loginIntoAccount();
    // loginPage.test();
}

@And("^enters the correct username$")
public void enters_the_correct_username() throws Throwable {
    System.out.println("User neters the correct password inside the password textefield");
    // loginPage.test2();
}

@And("^enters the correct password$")
public void enters_the_correct_password() throws Throwable {
    System.out.println("Entered the correct password");
}

@When("^clicks on the login button$")
public void clicks_on_the_login_button() throws Throwable {
    System.out.println("Clicked on the login button");
}

@Then("^user should be taken to the successful login page$")
public void user_should_be_taken_to_the_successful_login_page() throws Throwable {
    System.out.println("Succesffully taken to the login page.");
}

}

我已经尝试了下面列出的以下代码,但代码dosnt工作似乎打开浏览器但其他步骤不起作用(好像它已经创建了一个单独的驱动程序实例):

public class MasterStep {
WebDriver driver;
LoginPage loginPage;

@Before
public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\gianni.bruno\\Desktop\\BuyAGiftCucumberFramework\\PimCucumberFramework\\src\\test\\java\\resources\\other\\chromedriver.exe");
    this.driver = new ChromeDriver();
    this.driver.manage().window().maximize();
    this.driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

    loginPage = PageFactory.initElements(driver, LoginPage.class);
}

}

java selenium selenium-webdriver junit cucumber
2个回答
0
投票

对的,这是可能的。使用继承的概念。

第1步:创建基类

package com.base;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestBase2 {

    public static WebDriver driver = null;

    public void initialize() {

        System.setProperty("webdriver.chrome.driver", "src/com/drivers/chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in");
    }
}

第2步:使用extends关键字继承此类,并在需要时调用initialize()方法。


0
投票

黄瓜提供了可用于此的前后挂钩。

钩子是可以在Cucumber执行周期中的各个点运行的代码块。它们通常用于在每个场景之前和之后设置和拆除环境。

钩子在每个场景的第一步之前运行之前。

注释方法风格:

@Before
public void doSomethingBefore() {
}

Lambda风格:

Before(() -> {
});

在每个方案的最后一步之后运行挂钩后,即使步骤失败,未定义,挂起或跳过。

注释方法风格:

@After
public void doSomethingAfter(Scenario scenario){
    // Do something after after scenario
}

Lambda风格:

After((Scenario scenario) -> {
});

scenario参数是可选的,但如果使用它,则可以检查方案的状态。

© www.soinside.com 2019 - 2024. All rights reserved.