【问题标题】:How to instantiate objects and pass to methods using testng如何使用 testng 实例化对象并传递给方法
【发布时间】:2013-01-23 03:19:17
【问题描述】:

我正在尝试将对象正确实例化并传递给其他方法。这是我的代码:

public class CreateAndDeleteCollector {

private static WebDriver driver = new FirefoxDriver();
private LoginPage login;
private AdminRtcpCollectorPage rtcp;
private AdminFunctions admin;

//DesiredCapabilities capability = DesiredCapabilities.firefox();
//String hubUrl = "http://localhost:4444/wd/hub";
//WebDriver d = new RemoteWebDriver(new URL(hubUrl), capability);


@BeforeMethod
public void setup() throws Exception {
    LoginPage login = new LoginPage(driver);
    AdminRtcpCollectorPage rtcp = new AdminRtcpCollectorPage(driver);
    AdminFunctions admin = new AdminFunctions();
}

@Test
public void newCollector() throws Exception {
    login.login("192.168.1.100", "admin", "admin");
    rtcp.goToRtcpCollectorPage();
    rtcp.newRtcpCollector("test-1", "test", "test", "192.168.1.100");
}

@Test(dependsOnMethods = { "newCollector" })
public void deleteCollector() throws Exception {
    admin.initialize(driver);
    rtcp.goToRtcpCollectorPage();
    admin.delete("test-1");
}

@AfterTest
public void logoutAndClose() {
    Util.logout(driver);
    driver.close();
}
}     

当我使用 TestNG 运行它时,一旦我进入第一个测试方法,它就会出现 NullPointerException 错误,因为登录、rtcp 和管理员。我确定我误解了 @BeforeMethod 注释应该做什么。

我可以把我的所有声明和实例化放在一个设置方法之外,顺便说一句,这很好用,但是把它们放在一个方法中的原因是因为当我在 RemoteWebDriver 行中注释时,我得到一个“默认构造函数无法处理异常类型...”错误。

有人有什么建议吗?

我认为使用 TestNG 的工厂和数据提供者可能是一个答案,但我不确定如何将其应用于我的情况。

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:

    你得到一个空指针异常,因为你在你的方法中重新声明了变量。只需从

    中删除“登录页面”

    登录页面登录 = ...

    使用登录 =...

    【讨论】:

      猜你喜欢
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      • 1970-01-01
      • 2012-07-12
      • 2011-12-13
      相关资源
      最近更新 更多