문제

wanted to ask you about the best way to take screenshot on fail in such project? Should I do it in the Selenium code, or in the Maven project via some command or with Xvfb? Im using Firefox headless via Xvfb.

I have seen a few classes on the internet that take screenshots, but Im missing the big picture here. How does this class know when to take the screenshot? How does Jenkins tell the java test code that it has failed, so it will take a picture? Where in the test code should I reference the take screenshot class? Should I use a try catch on the whole test? Isnt there a Jenkins plugin that will automatically save screenshot on fail?

도움이 되었습니까?

해결책

I just found an article that explains a much better way to do it: http://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html

How does it work?
Effectively, you pass in a custom WebDriverEventListener, which has functions that will be called on certain events. One of those events is onException(). So every time an exception is thrown by WebDriver, you can write the code to take a screenshot.

I have seen three different ways to do this:

  1. If you have a static driver, the easiest way is to set up a listener in TestNG (Overridding TestListenerAdapter), and then to take a screenshot in the onTestFailure() method.

  2. My personal method is to use the Selenium Page Object pattern, but modified a bit. I have created an EnhancedWebElement object that wraps and extends a normal WebElement, and has a reference to the driver. Then in each of the methods that WebElement has, I perform the call in a try/catch and in the catch, I then take a screenshot. I'm open to sharing the code, but I'd have to trim away quite a bit to post here, so please tell me if you want to see it.

  3. Alternatively, you can set up a proxy around the WebElement or the Driver itself and have it catch everything. I haven't done this, but I've seen it work on other projects.

다른 팁

just found an article that explains a much better way to do it: http://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html Blockquote

But this dicision have some problem. it will save screenshoot on any exception even when you try/catch some in your code. I use methods from that article. But in my testng.xml files i add

`

<listeners>
    <listener class-name="MyListener" />
  </listeners>

`

And than i create

`

public class MyListener implements ITestListener{
   //almost all methods i create blank
   //but implement only onTestFailure
   //
   onTestFailure(){
      //here i used methods from article ubove
      //
   }
}

`

And screenshots done only in case when my @Test fail.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top