【发布时间】:2021-09-22 03:35:01
【问题描述】:
我正在使用 Java、TestNG 和 Eclipse 开发 Selenium POM 项目。我有testA 和testB。 testA 有 2 个 @Test 方法,此测试提交一个生成 uniqueNumber 的 Web 表单。 testB 只有 1 个 @Test 方法。现在 testB 具有从 testA 运行这两种方法的先决条件,并且我能够从 testB 运行这两种方法(使用 TestNG 的DependsOn)。问题是在testA 中生成的uniqueNumber 没有结转到testB,因为我尝试使用Sysout 打印它并且我得到Null。
-
我已将
BaseClass中的uniqueNumber定义为public String uniqueNumber变量,因为它是字母数字。 (我认为我应该能够在项目中的任何地方使用uniqueNumber,但似乎情况并非如此——我也不确定如何在 Java 中声明和使用能够保持其更新值的全局变量所有包中的类......应该在整个项目中都可以访问 - 声明Static不起作用,因为它在另一个包中访问时会打印Null。) -
testA 和 testB 都是 BaseClass 的子类(例如,我将它们声明为“
public class testA extends BaseClass”) -
testA 中的 2 个 @Test 方法被声明为
public void testAmethodOne()和public void testAmethodTwo()。同样,testB 中的 1 个 @Test 方法声明为 public voidtestBmethod()。 -
testA 和 testB 位于不同的包中,所以我将两者放在一个包中,但仍然无法在 testB 中获得
uniqueNumber的值。还尝试通过在 testB 中创建 testA 的对象来访问 testA 的方法,但这也不起作用。 -
为了在 testA 中使用 2 个 @Test 方法,我尝试将 testB 类扩展为“
public class testB extends testA”,但得到Null Pointer Exception错误(我正在使用 POM,因此错误与页面类有关)为“Cannot read field <another_page_class_here> because "this.pages" is null"。 -
也尝试过这种方式:将
testAmethodOne的内容复制到另一个常规方法中,例如public void regularMethod()(不是@Test方法),然后创建testA的对象(例如objA),尝试通过objA.regularMethod()访问regularMethod(),但得到此NullPointerException错误:cannot read field <another_page_class> (NOT test class, please note that) because this.pages is null.. -
不确定 TestNG 是否有办法将变量从一个包/类中的方法传递到位于不同包/类中的另一个类。由于这是一个非常简单的功能,我猜它应该包含在 TestNG 中 - 但我不知道。
-
我被困在
uniqueNumber的值在 testB 中打印为 Null 并且它的值没有在 testB 中结转的点。 (uniqueNumber可以在整个 testA 中访问)// Class testA (located in package A) public class testA extends BaseClass { @BeforeMethod public void startUp() { // Code } @AfterMethod public void tearDown() { // Code } @Test(priority = 1) public void testAmethodOne() { // Code } @Test(priority = 2) public void testAmethodTwo() { // Code } } // End of testA class// 类testB(位于包B中)
public class testB extends BaseClass { @BeforeMethod public void startUp() { // Code } @AfterMethod public void tearDown() { // Code } @Test(priority = 1) public void testBmethodOne(dependsOnMethods = { "testAmethodOne", "testAmethodTwo" }) { **// HERE uniqueNumber's value printed as Null** **// Tried with TestNG's ITestContext, but getting Null here** **// Tried by declaring uniqueNumber as "public static" and as "protected static", but getting Null here** } } // End of testB class
【问题讨论】:
-
可以试试TestNG的ITestContext接口吗
标签: java selenium selenium-webdriver testng browser-automation