Selenium WebDriverを使用したWebアプリケーション統合テスト

統合テスト(単体テストまたは単体テストとは対照的に)は、システムの個々のアトミックコンポーネント(クラス)ではなく、あらゆる環境での相互作用の結果をテストします。



運命の意志により、特定の企業のニーズに合わせて調整された一種のインターフェースフレームワークを開発しています。 フレームワークランタイムはブラウザであるため、言語はJavaScriptです。



JavaScriptの単体テストの方法については、前書いたが 、ここでチームで使用される統合テストプロセスについて説明します。



セレン



長い間、ブラウザでWebアプリケーション/ページをテストするための既知のツール-Selenium 。 その用途に関しては、主に2つの方法があります。

  1. SeleniumIDEでTestSuiteを作成し、SeleniumTestRunnerで実行する、または
  2. webriverを使用する


WebDriverは、製品の2番目のブランチに登場した新しいSeleniumの「機能」です。 その主な本質は、コード(C#、Java、Python、Ruby)で記述されたテストを、さまざまなブラウザーや仮想ランタイムで実行できることです。



Webriver



Selenium WebDriverはさまざまな言語(C#、Java)の「バインダー」のセットであり、さまざまなコマンドを「下位」ブラウザーに提供できます。



各ブラウザには、WebDriverの独自の実装(FireFoxDriver、InternetExplorerDriver、ChromeDriver-現在含まれ、OperaSoftwareが開発したOperaDriver )があります。 「仮想」HtmlUnitDriverもあります。 「ブラウザ」実装とは異なり、インストールされたブラウザを必要としないため、より高速に動作し、プラットフォームに依存しませんが、欠点もあります-HtmlUnitDriverには「独自の」JavaScript実装があるため、「リッチ」Webアプリケーションの動作が異なる場合があります。 タスクには「ブラウザ」実装を使用します。これにより、後で実行される正確な環境でアプリケーションをテストできます。



簡単に言うと、WebDriverで作業する一般的な本質は次のように説明できます。



WebDriverでできること



以下は「ブラウザ」実装です。拡張の本質はRemoteWebDriverクラスです(WebDriverインターフェイスを実装します)。



「found」要素(WebElementインターフェイス)



テストランタイム



テストを作成するための言語としてJavaが選択されました。 ランタイムはJUnit4です。



免責事項 :私はクールなジャビストのふりをしていないので、上級の同僚が欠陥やその他のあらゆる「アンチパターン」を見つけたら、コメントを喜んで聞きます。



基本的な抽象Webテストクラス。

@Ignore abstract public class AbstractWebTest { protected static RemoteWebDriver _driver; //    private String testPageLocation = String.format( "http://%s:%s/test.html", System.getProperty("test.httproot"), // Web- ... System.getProperty("test.httpport", "80") //   ); //   WebDriver private static String driverName = System.getProperty( "test.driver", "org.openqa.selenium.firefox.FirefoxDriver"); /** *     -   . *     */ @BeforeClass public static void setUpDriver() throws ClassNotFoundException, IllegalAccessException, InstantiationException { _driver = (RemoteWebDriver) Class.forName(driverName).newInstance(); } /** *    -    */ @Before public void setUp() { _driver.get(testPageLocation); } /** *     -    ( ) */ @AfterClass public static void tearDown() { _driver.close(); } }
      
      





テストのセットを持つ特定のクラス(簡単にするために、たとえば、CSSセレクターによる要素が実際に見つかってページ上で利用可能かどうかなど、いくつかのチェックが削除されました)

 public class TestMoneyField extends AbstractWebTest { /** *         0.00 */ @Test public void testRendering() { WebElement content = _driver.findElementByCssSelector("#FieldMoney .input-text-field"); Assert.assertEquals("0.00", content.getValue()); } /** *   "" */ @Test public void testInputWithoutDot() { WebElement content = _driver.findElementByCssSelector("#FieldMoney .input-text-field"); content.sendKeys("999999"); Assert.assertEquals("999 999.00", content.getValue()); } }
      
      





すべてのテストは、個別のAntビルドを使用して実行されます。

 <target name="integrationtest" depends="init, buildtests, deploytests"> <junit haltonfailure="false"> <sysproperty key="test.driver" value="org.openqa.selenium.firefox.FirefoxDriver" /> <classpath> <pathelement location="${path.to.tests.jar}"/> </classpath> <batchtest> <fileset dir="${path.to.compiled.test.classes}"> <include name="**/tests/Test*.class" /> </fileset> </batchtest> </junit> <junit haltonfailure="false"> <sysproperty key="test.driver" value="org.openqa.selenium.ie.InternetExplorerDriver" /> <classpath> <pathelement location="${path.to.tests.jar}"/> </classpath> <batchtest> <fileset dir="${path.to.compiled.test.classes}"> <include name="**/tests/Test*.class" /> </fileset> </batchtest> </junit> </target>
      
      





このタスクは、FirefoxおよびInternetExplorerで名前がTestで始まるクラスからすべての既知のテストを実行します。 依存関係には、基本的な初期化、コンパイル、およびコンパイルされたテストのテストサイトへのアンロードを伴うタスクがあります。



お団子チップス



一部の「ブラウザ」実装(Firefox、Opera、Chrome)は、スクリーンショットの取得をサポートしています。 これは、テストが失敗した時点でテストページがあった視覚状態を修正するのに役立ちます。 機能JUnit4- TestWatchmanはこれに適しています。

 @Ignore abstract public class AbstractWebTest { //    private String screenshotDir = System.getProperty("test.screenshotDir", ""); @Rule public MethodRule watchman = new TestWatchman() { /** *     ""  * @param e    * @param method - */ @Override public void failed(Throwable e, FrameworkMethod method) { if(_driver instanceof TakesScreenshot && !screenshotDir.equals("")) { String browserName = _driver.getClass().getName(); String testSuiteName = method.getMethod().getDeclaringClass().getName(); browserName = browserName.substring(browserName.lastIndexOf('.') + 1); testSuiteName = testSuiteName.substring(testSuiteName.lastIndexOf('.') + 1); byte[] screenshot = ((TakesScreenshot)_driver).getScreenshotAs(OutputType.BYTES); try { FileOutputStream stream = new FileOutputStream( new File( String.format("%s/screenshot_%s_%s_%s.png", screenshotDir, browserName, testSuiteName, method.getName()))); stream.write(screenshot); stream.close(); } catch (IOException e1) { e1.printStackTrace(System.out); } } } }; //  ... }
      
      





Antビルドのスクリーンショットを含むフォルダーへのパスを持つ変数を追加します

  <junit haltonfailure="false"> <sysproperty key="test.driver" value="org.openqa.selenium.firefox.FirefoxDriver" /> <sysproperty key="test.screenshotDir" value="${screenshotsDir}" /> <classpath> <pathelement location="${path.to.tests.jar}"/> </classpath> <batchtest> <fileset dir="${path.to.compiled.test.classes}"> <include name="**/tests/Test*.class" /> </fileset> </batchtest> </junit>
      
      





統合



現在の実装では、AntビルドはJetbrains TeamCityを介して追跡されます。 ビルドの起動は、SVNのコードをリセットするように構成されています。 統合テストは、全体的なテスト手順の一部です。 統合テストのいずれかが失敗した場合、スクリーンショットが作成され、ビルドの「アーティファクト」として公開されます。機能をトランクにリセットした後に「左」のテストだけでなく、「左」の「方法」も確認できます。



現在、IEおよびFirefoxでテストが使用されていますが、Chromeは統合の難しさのために接続されていません(明らかに、ChromeDriverにはいくつかのエラーがあり、場合によっては通常ページ上の要素を検索できません-2.0b1の時点で2.0b2が利用可能になりましたただし、まだ試していない)



All Articles