不TestNG的保证@BeforeSuite方法之前@BeforeTest方法执行?
-
25-09-2019 - |
题
背景:我的目标是代码运行包含自一个TestNG的铁蛋白 - 硒系统 (无串到Maven或蚂蚁插件;不仅仅是Java)。它必须允许测试用例接受 参数,包括浏览器和域网址。当TestRunner的实例 这些测试用例,浏览器和域用来获得硒对象执行 它的测试。
问题:每一套只有一个测试类中获得域参数成功(在 试图获得硒对象(在@BeforeTest前@BeforeSuite法))。 不接收域的测试类具有空硒对象B / C它不能 被实例化。
代码:XmlClasses各自包含自己的XMLTest并且所有三个内 在单一XmlSuite。该套件包含TestClass1的顺序, TestClass2,然后TestClass3。测试类本身的2层的子类 抽象基类,其包括功能初始化注入变量和 随后得到硒的一个实例。这样做的目的是为了测试一个或多个 应用程序(在多个域上)尽可能少的重复的码尽可能(即:硒 实例是根基类,因为它是通用于所有测试)。查看方法 下面的详细资料。
// Top-most custom base class
abstract public class WebAppTestBase extends SeleneseTestBase
{
private static Logger logger = Logger.getLogger(WebAppTestBase.class);
protected static Selenium selenium = null;
protected String domain = null;
protected String browser = null;
@BeforeTest(alwaysRun = true)
@Parameters({ "selenium.browser" })
public void setupTest(String browser)
{
this.browser = browser;
logger.debug(this.getClass().getName()
+ " acquiring Selenium instance ('" + this.browser + " : " + domain + "').");
selenium = new DefaultSelenium("localhost", 4444, browser, domain);
selenium.start();
}
}
// Second level base class.
public abstract class App1TestBase extends WebAppTestBase
{
@BeforeSuite(alwaysRun = true)
@Parameters({"app1.domain" })
public void setupSelenium(String domain)
{
// This should execute for each test case prior to instantiating any Selenium objects in @BeforeTest
logger.debug(this.getClass().getName() + " starting selenium on domain '" + domain+ "'.");
this.domain = domain;
}
}
// Leaf level test class
public class TestClass1 extends App1TestBase
{
@Test
public void validateFunctionality() throws Exception
{
// Code for tests go here...
}
}
// Leaf level test class
public class TestClass2 extends App1TestBase
{
@Test
public void validateFunctionality() throws Exception
{
selenium.isElementPresent( ...
// Rest of code for tests go here...
// ....
}
}
// Leaf level test class
public class TestClass3 extends App1TestBase
{
@Test
public void validateFunctionality() throws Exception
{
// Code for tests go here...
}
}
输出:TestCase3正常运行。 TestCase1和TestCase2失败。堆栈跟踪获取生成...
10:08:23 [DEBUG RunTestCommand.java:63] - Running Tests.
10:08:23 [Parser] Running:
Command line suite
Command line suite
[DEBUG App1TestBase.java:49] - TestClass3 starting selenium on domain 'http://localhost:8080'.
10:08:24 [DEBUG WebAppTestBase.java:46] - TestClass2 acquiring Selenium instance ('*firefox : null').
10:08:24 [ERROR SeleniumCoreCommand.java:40] - Exception running 'isElementPresent 'command on session null
10:08:24 java.lang.NullPointerException: sessionId should not be null; has this session been started yet?
at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:216)
at org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(SeleniumDriverResourceHandler.java:562)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleCommandRequest(SeleniumDriverResourceHandler.java:370)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(SeleniumDriverResourceHandler.java:129)
at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1530)
at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1482)
at org.openqa.jetty.http.HttpServer.service(HttpServer.java:909)
at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820)
at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986)
at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837)
at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:245)
at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
我很欣赏你可能在这个问题上的任何信息。
解决方案
我认为问题是,你的@BeforeSuite方法是确定一个值的字段,但是你有三种不同的情况,所以其他两个永远不会被初始化。
请记住,@BeforeSuite只运行一次,无论什么类的它属于。这样,@之前/ AfterSuite方法上是外部的整个测试环境类通常定义。这些方法真的应该是静态的,但我决定不执行这项要求,因为它有时是不切实际的。
我想一个更好的方式来处理你的问题是在您的域名场看作是一种注射资源,每个测试都将收到来自吉斯或其他依赖注入框架。
不隶属于 StackOverflow