2016-09-01 2 views
0

ScalaTest를 사용하여 Spec 또는 Suite의 모든 실패 테스트에 대해 스크린 샷을 찍고 싶습니다. 스칼라 테스트 웹 사이트는이와 함께 실패 할 수 있습니다 모든 코드를 주변의 스크린 샷을하는 방법을 보여줍니다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") 
     } 
     } 
    } 
} 

, 오류를 차단하고 스크린 샷을 저장합니다 스크린 샷의 특성을 확장 모든 사양.

질문에서 언급 한 것처럼 화면 캡처()를 사용하여 주변 지역을 보완하는 것은 단언 할 때 실패 만 저장하지만 요소를 찾을 수 없어 (예 : 잘못된 선택기) 테스트가 실패하면 스크린 샷을 저장하지 않습니다.

위 코드를 사용하면 모든 실패가 스크린 샷을 저장합니다.