您可能无法实现继承,但您可以在步骤定义类中使用构造函数将驱动程序对象引用从一个类传递到另一个类。
- 创建一个初始化驱动程序对象/页面对象类的基类/基础类。使用@Before 注释来定义设置,使用@After 来定义拆卸方法。
public class Step_Def_Base {
public static webDriverCreator test;
@Before
public void printScenario(Scenario scenario) {
test = new webDriverCreator(this.getClass().getSimpleName());
String className = this.getClass().getCanonicalName();
System.out.println("********************************************************");
System.out.println("Scenario: " + scenario.getName());
System.out.println("********************************************************");
}
@After
public void screenShotAndConsoleLog(Scenario result) {
test.takescreenshot.takeScreenShotOnException(result);
if (!(result.getStatus().contains("pass"))) {
throw new RuntimeException(result.getName() + " got failed");
}
test.closeBrowserSession();
}
}
- 在您的步骤定义类中,创建一个构造函数并使用上下文实例化 baseFoundation 对象。这就是 pico-container 发挥神奇作用的地方,它将步骤定义类的驱动程序对象与另一个类联系起来。
public class StepDefs_AltoroMutualLoginPage {
private Step_Def_Base contextStep;
private webDriverCreator test;
public StepDefs_AltoroMutualLoginPage(Step_Def_Base contextStep) {
// TODO Auto-generated constructor stub
this.contextStep = contextStep; // <-- This is where pico-containers starts working
test = contextStep.test; // <-- Linking your driver object reference from the point where it is instantiated , i.e the base foundation class
}
@Given("^I am on test fire login page \"([^\"]*)\"$")
public void alotoroMutualLoginPage(String url) {
test.launchApplication(url);
test.altoroMutual.launchLoginLink();
test.altoroMutual.verifyUserIsOnLoginPage();
}
现在您可以发挥创意,并相应地组织您的页面对象。我在返回 Web 驱动程序对象的包装类中聚合并实例化了我的所有页面对象类。您可以在代码中看到,我正在从对象访问altoroMutual
pageObject 类。test
确保您使用maven来管理所有 dev-dependencies 。以下依赖项应在您的项目中添加 pico 容器。
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.14.3</version>
</dependency>