DOES TestNGの保証@BeforeSuite方法は@BeforeTest方法の前に実行されますか?
-
25-09-2019 - |
質問
背景:私の目標は、コードに自己完結型で実行さTestNGの-セレンシステムであり、 (Mavenのか蟻のプラグインへの文字列ません。ただのjava)。テストケースを受け入れることができるようにする必要があります ブラウザおよびドメインURLを含むパラメータ。ときにテストランナーをインスタンス化します これらのテストケースは、ブラウザおよびドメインが実行するためにSeleniumのオブジェクトを取得するために使用されています これはテストです。
問題:スイートごとに1つのテストクラスは中(ドメインパラメータを得ることに成功しました ()@BeforeTestにセレンオブジェクトを取得しようとする前に@BeforeSuite方法)。 それはカントcのドメインを受けていないテストクラスは/ヌルセレンオブジェクトbを持っています インスタンス化されます。
CODE:XmlClassesは、それぞれ独自た[XMLTestと3つのすべての中に含まれています 単一XmlSuiteです。スイートはTestClass1の順に含まれ、 TestClass2、その後、TestClass3。テストクラス自体は、2層のサブクラスです 注入された変数を初期化する機能が含ま抽象基底クラスと その後、セレンのインスタンスを取得します。このため目的は、テスト1または複数にあります セレン:で可能な(すなわち、わずか繰り返しコードとして(複数のドメイン上の)アプリケーション それはすべてのテストに共通だからインスタンス化)は、ルートベースクラスです。メソッドを参照してください。 詳細については、以下ます。
// 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...
}
}
OUTPUT: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)
私はあなたがこの問題に持つかもしれない情報を感謝しています。
解決
私は問題は他の2つは初期化されることはありません飽きないので、あなたの@BeforeSuite方法は、フィールドに値を代入されていますが、三つの異なるインスタンスを持っているということだと思います。
は、それが属する関係なく、どのようなクラスの、@BeforeSuiteは一度だけ実行されることに注意してください。そのため、前/ AfterSuite @メソッドは、通常は全体のテスト環境の外にあるクラスに定義されています。これらのメソッドは、実際に静的である必要がありますが、それは時々非現実的なので、私はこの要件を強制しないことを決定します。
私はあなたの問題にアプローチするより良い方法は、あなたのテストのそれぞれが、Guiceのか、他の依存性注入フレームワークから受け取ることを注入し、リソースとしてあなたのドメインフィールドを見ていると思います。