8

背景:私の目標は、自己完結型(MavenまたはAntプラグインへの文字列はなく、Javaのみ)を実行するTestNG-Seleniumシステムをコーディングすることです。テストケースがブラウザやドメインURLなどのパラメータを受け入れることができるようにする必要があります。TestRunnerこれらのテストケースをインスタンス化するとき、ブラウザーとドメインを使用して、Seleniumオブジェクトを取得してテストを実行します。

@BeforeSuite問題: Seleniumオブジェクト(の)を取得しようとする前に、スイートごとに1つのテストクラスのみが(メソッドの)ドメインパラメーターの取得に成功します@BeforeTest。ドメインを受け取らないテストクラスには、nullインスタンス化できないセレンオブジェクトがあります。

コード:XmlClassesはそれぞれ独自のXmlTestに含まれており、3つすべてが単一のXmlSuiteに含まれています。スイートには、TestClass1、TestClass2、TestClass3の順に含まれています。テストクラス自体は、注入された変数を初期化し、その後Seleniumのインスタンスを取得する機能を含む、2層の抽象基本クラスのサブクラスです。これの目的は、コードの繰り返しをできるだけ少なくして(複数のドメインで)1つまたは複数のアプリケーションをテストすることです(つまり、Seleniumのインスタンス化は、すべてのテストに共通であるため、ルート基本クラスにあります)。詳細については、以下の方法を参照してください。

// 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)

この問題に関する情報をいただければ幸いです。

4

1 に答える 1

14

問題は、@BeforeSuiteメソッドがフィールドに値を割り当てていることだと思いますが、3つの異なるインスタンスがあるため、他の2つは初期化されません。

@BeforeSuite所属するクラスに関係なく、実行されるのは1回だけであることを忘れないでください。そのため、@Before/AfterSuiteメソッドは通常、テスト環境全体の外部にあるクラスで定義されます。これらの方法は実際にはそうあるべきですが、実際static的でない場合があるため、この要件を強制しないことにしました。

問題に取り組むためのより良い方法は、ドメインフィールドを、各テストがGuiceまたは他の依存性注入フレームワークから受け取る注入されたリソースとして見ることだと思います。

于 2010-08-05T03:45:17.003 に答える