在多个步骤定义之间传递场景上下文 类
Passing scenario context between multiple step definition classes
所以,我开始使用 Cucumber/TestNG/Java/selenium 开发一个框架
我有一个 Context class,它在枚举的帮助下以键值对的形式保存场景上下文
引用自 here
我的问题是:
对于功能中的特定场景,步骤定义定义在多个 classes:
Sample feature
Feature: A feature
Scenario: Scenario
Given Statement 1
Then Statement 2
Class1
Class firstDef{
TestRunner test;
public firstDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement1")
void method1(){
}
}
Class 2
Class secondDef{
TestRunner test;
public secondDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement2")
void method1(){
}
}
TestRunner class
Class TestRunner{
//some code
@Test
public method1(){
//some code
}
}
所以,
每个步骤定义的大脑 class 对象都会不同,这无济于事,因为我希望上下文在整个场景中都相同
即使我在 Runner 中实例化 Brain class,该实例对于测试的每个实例都是新的 class
为了克服这个问题,我想到的一种可能的解决方案是序列化和反序列化
在@BeforeClass 方法中,我将有:
File f = new File(path);
if(!f.exists()){
Brain context = new Brain();
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(context);
}
然后我可以在任何需要上下文的地方反序列化,并在更改相同的引用变量后再次序列化
上面的方法是否正确或者是否有更好的方法来克服同样的问题
使用 Cucumber PicoContainer 的构造函数注入对上述问题非常有效
只需添加依赖项:
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
到您的 pom.xml(对于 Maven 项目)并通过多步定义 classes 传递 HashMap class 的引用变量,它在一个场景中将保持不变。
请仔细阅读this文章以获得详细解释
所以,我开始使用 Cucumber/TestNG/Java/selenium 开发一个框架 我有一个 Context class,它在枚举的帮助下以键值对的形式保存场景上下文 引用自 here
我的问题是: 对于功能中的特定场景,步骤定义定义在多个 classes:
Sample feature
Feature: A feature
Scenario: Scenario
Given Statement 1
Then Statement 2
Class1
Class firstDef{
TestRunner test;
public firstDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement1")
void method1(){
}
}
Class 2
Class secondDef{
TestRunner test;
public secondDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement2")
void method1(){
}
}
TestRunner class
Class TestRunner{
//some code
@Test
public method1(){
//some code
}
}
所以,
每个步骤定义的大脑 class 对象都会不同,这无济于事,因为我希望上下文在整个场景中都相同
即使我在 Runner 中实例化 Brain class,该实例对于测试的每个实例都是新的 class
为了克服这个问题,我想到的一种可能的解决方案是序列化和反序列化
在@BeforeClass 方法中,我将有:
File f = new File(path);
if(!f.exists()){
Brain context = new Brain();
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(context);
}
然后我可以在任何需要上下文的地方反序列化,并在更改相同的引用变量后再次序列化
上面的方法是否正确或者是否有更好的方法来克服同样的问题
使用 Cucumber PicoContainer 的构造函数注入对上述问题非常有效
只需添加依赖项:
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
到您的 pom.xml(对于 Maven 项目)并通过多步定义 classes 传递 HashMap class 的引用变量,它在一个场景中将保持不变。
请仔细阅读this文章以获得详细解释