【问题标题】:How to access a variable from a @Test method into another @Test method located in another class and another package?如何将变量从@Test 方法访问到位于另一个类和另一个包中的另一个@Test 方法?
【发布时间】:2021-09-22 03:35:01
【问题描述】:

我正在使用 Java、TestNG 和 Eclipse 开发 Selenium POM 项目。我有testAtestBtestA 有 2 个 @Test 方法,此测试提交一个生成 uniqueNumber 的 Web 表单。 testB 只有 1 个 @Test 方法。现在 testB 具有从 testA 运行这两种方法的先决条件,并且我能够从 testB 运行这两种方法(使用 TestNG 的DependsOn)。问题是在testA 中生成的uniqueNumber 没有结转到testB,因为我尝试使用Sysout 打印它并且我得到Null

  1. 我已将BaseClass 中的uniqueNumber 定义为public String uniqueNumber 变量,因为它是字母数字。 (我认为我应该能够在项目中的任何地方使用uniqueNumber,但似乎情况并非如此——我也不确定如何在 Java 中声明和使用能够保持其更新值的全局变量所有包中的类......应该在整个项目中都可以访问 - 声明 Static 不起作用,因为它在另一个包中访问时会打印 Null。)

  2. testA 和 testB 都是 BaseClass 的子类(例如,我将它们声明为“public class testA extends BaseClass”)

  3. testA 中的 2 个 @Test 方法被声明为 public void testAmethodOne()public void testAmethodTwo()。同样,testB 中的 1 个 @Test 方法声明为 public void testBmethod()

  4. testA 和 testB 位于不同的包中,所以我将两者放在一个包中,但仍然无法在 testB 中获得 uniqueNumber 的值。还尝试通过在 testB 中创建 testA 的对象来访问 testA 的方法,但这也不起作用。

  5. 为了在 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"。

  6. 也尝试过这种方式:将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..

  7. 不确定 TestNG 是否有办法将变量从一个包/类中的方法传递到位于不同包/类中的另一个类。由于这是一个非常简单的功能,我猜它应该包含在 TestNG 中 - 但我不知道。

  8. 我被困在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


【解决方案1】:

既然你提到了这个uniqueNumber in BaseClass as a String variable

如果你只是在你的基类中把它作为一个静态的:-

protected static String uniqueNumber;

并在执行testA 期间对其进行初始化,然后值也应该可用于testB 方法。

在 Java 中,如果一个字段被声明为静态的,那么正好是一个 该字段是在该类的所有实例之间创建和共享的。它 不管我们初始化一个类多少次;总会有 只是属于它的静态字段的一份副本。这个的价值 静态字段将在任何相同的所有对象之间共享 不同的班级。

Reference link

or click here to get to know more about static

【讨论】:

  • 已尝试但将 TestNG Exception 设为 org.testng.TestNGException package.testB.testBmethodOne() depends on nonexistent method testAmethodOne,即使该方法存在于 testA 中并且我复制、粘贴了它,所以我仔细检查了是否存在拼写错误..
  • @Curious : 我在你的代码中没有看到dependsOnMethod
  • 我编辑了我的问题以包含“dependsOnMethod”。我没有复制,粘贴真实代码,因为由于隐私问题我无法公开代码,但两个类的结构与问题。
  • 你了解 Java 中的静态是什么吗?可能你错过了静态字段的优势。
  • 是的-谢谢你的文章。由于我们要发布,所以我的工作量很大。这个周末的某个时候会试试 -
【解决方案2】:

即使两个类都扩展了相同的基类,但在执行测试时会初始化 testAtestB 的不同对象,因此为 testA 生成的 uniqueNumber 对象将无法用于 testB对象。

要解决这个问题,要么将uniqueNumber 设为static(已经回答),要么在testAtestB 类的测试方法中,使用ITestContext 作为参数。

在testA中

@Test(priority = 1)
public void testAmethodOne(ITestContext ctx) {
    //code
    ctx.setAttribute("uniqueNumber", uniqueNumber);
}

在testB中

public void testBmethod(ITestContext ctx) {
    //code
    String num = (String) ctx.getAttribute("uniqueNumber");
}

【讨论】:

  • 因为我们要发布了,所以我的工作量很大。这个周末的某个时候会试试 -
  • 尝试在 testB 中获取 Null。 testA 完成的那一刻,uniqueNumber(使用 ITestContext)失去了它的范围,并且在 testB 中不可用。我们不应该忘记 testB 位于另一个类中,位于另一个包中。
  • @Curious 你是如何运行测试的?使用 xml 吗?
  • 我正在单独运行测试 - 尚未使用 xml。顺便说一句,我在类名之前有下面的语句,以便获得单独测试的范围报告。 @Listeners(MyListeners.class) 如果您能发布适合您的代码,将不胜感激。例如,声明了 uniqueNumber 的 BaseClass,创建 TestA 并扩展到 BaseClass,在 TestA > @Test 方法中分配它的值,创建 TestA 并扩展到 BaseClass,在 TestA > @Test 方法中为其赋值,
  • @Curious 如果您单独运行每个测试,那么您无法以编程方式执行此操作 -> 使用 staticITestContext 方法。一旦执行完成,细节就会丢失。正如您提到的,您只能使用诸如 excel 之类的存储选项来做到这一点。
猜你喜欢
  • 1970-01-01
  • 2014-10-10
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2015-09-16
  • 1970-01-01
  • 2014-07-06
相关资源
最近更新 更多