2016-09-01 28 views
0

ScalaTestを使用してSpecまたはSuite内のすべての失敗テストにつきましてスクリーンショットを取得したいと考えています。 Scalaのテストのウェブサイトがこれに失敗する可能性があるすべてのコードを囲むスクリーンショットを取る方法を示しています。Scalatestを使用しているすべての失敗のスクリーンショット

withScreenshot { 
    drive.findElement(By.id("login")).getAttribute("value") should be ("Login") 
} 

あり説明しようとthis postがあるが、私は正確に何をすべきか理解できませんでした。 私はクラスScreenshotOnFailure.scalaも見つけましたが、プライベートでパッケージの制限があると使用できませんでした。

何か障害を傍受してからスクリーンショットを撮る方法があれば教えてください。

答えて

1

ちょうど最終的な答えを私は質問に記載されたthis postからのアプローチに基づいて問題を解決できる方法を書いています。

要するに、解決策はこのようになりました(疑似コード)。

trait Screenshots extends FunSpec { 
    ... 

    override def withFixture(test: NoArgTest): Outcome = { 
     val outcome = test() 

     // If the test fails, it will hold an exception. 
     // You can get the message with outcome.asInstanceOf[Failure].exception 
     if (outcome.isExceptional) { 
     // Implement Selenium code to save the image using a random name 
     // Check: https://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver 
     } 
     outcome 
    } 
} 

class MySpec extends Screenshots { 
    ... 

    describe("Scenario A") { 
     describe("when this") { 
     it("the field must have value 'A'") { 
      // It will save a screenshot either if the selector is wrong or the assertion fails 
      driver.findElement(By.id("elementA")).getAttribute("value") should be ("A") 
     } 
     } 
    } 
} 

この時点から、Screenshot特性を拡張するすべての仕様はエラーを傍受し、スクリーンショットを保存します。

質問に記載されているように、周囲の領域をwithScreenshot()で補うだけで、アサーションでの失敗のみを保存しますが、要素が見つからないためにテストが失敗したときにスクリーンショットを保存しません。

上記のコードでは、すべての失敗がスクリーンショットを保存します。

関連する問題