问题
I am using selenium to perform integration tests on a Scala web app. I would like to either screenshot or print the html of a page into the console whenever a test fails. My current set up is Scalatest using Selenium 2.0, with Spec.
Is there anyway to intercept a failure or determine the state of a test from a AfterEach override method?
回答1:
To do that you'll want to override withFixture instead of using BeforeAndAfterEach. BeforeAndAfterEach's beforeEach method happens before the test, and afterEach happens after the test. Whereas withFixture can do something at the start and/or end of the test. So when withFixture is invoked, the test has already started. After the test function returns to withFixture, the test has not yet completed. So you can catch an exception in withFixture to determine if the test has failed, and if so, create a screen shot.
来源:https://stackoverflow.com/questions/9464030/perform-specialised-functionality-on-failure-of-a-scalatest