Geb Allure screenshot integration

528 views
Skip to first unread message

Mateusz Pluta

unread,
Nov 27, 2015, 4:53:13 AM11/27/15
to geb-...@googlegroups.com
Hi All,

I am using Geb with TestNG and Gradle. What is the easiest way to enable allure screenshot capabilities in my setup https://github.com/allure-framework/allure-core/wiki/Attachments

Tried by implementing method in custom TestNGListener and failed. Maybe there is another simpler way?

Regards

 
-------------------------------------------
Mateusz Pluta
Sr. QA Specialist
 



Marcin Erdmann

unread,
Dec 3, 2015, 6:50:05 PM12/3/15
to geb-...@googlegroups.com
I know nothing about allure and very little about TestNG but did you try following the instructions at https://github.com/allure-framework/allure-core/wiki/TestNG#gradle?

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/D8F75F5C-4E77-4DA4-A679-56E33C8A15F7%40smartrecruiters.com.
For more options, visit https://groups.google.com/d/optout.

Mateusz Pluta

unread,
Dec 9, 2015, 2:20:12 AM12/9/15
to Marcin Erdmann, geb-...@googlegroups.com
I have run another check and managed to create a custom TestNG Listener:

import geb.testng.GebReportingTest;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import ru.yandex.qatools.allure.annotations.Attachment;

 
public class ScreenshotListener extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult failingTest) {
        try {
       
            GebReportingTest test = (GebReportingTest) failingTest.getInstance();
            createAttachment(test.getBrowser().getDriver());
        } catch (Exception ex) {
            System.err.println("Unable to capture screenshot...");
            ex.printStackTrace();
        }
    }

    @Attachment(type = "image/png")
    private byte[] createAttachment(WebDriver driver) throws Exception {
        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    }
}

Now it seems it runs only of I annotate a test class with @Listeners(ScreenshotListener.class)

To make it use globally I should probably annotate GebReportingTest class but I do not have access to it and extending it now seems really troubling. This is the only way I managed onTestFailure method to run correctly. Is there any other?

Regards

Mateusz


Brian Kotek

unread,
Dec 9, 2015, 12:26:12 PM12/9/15
to Geb User Mailing List, Marcin Erdmann
Sorry, but what's the problem with just creating a base class that extends GebReportingTest and adding your annotation to that base class?

Marcin Erdmann

unread,
Dec 9, 2015, 3:18:06 PM12/9/15
to Brian Kotek, Geb User Mailing List
Brian's suggestion is what I would do as well.

Mateusz Pluta

unread,
Dec 28, 2015, 3:25:03 AM12/28/15
to geb-...@googlegroups.com
Hello All,

We manage to go different route and I am happy to say that it worked for us. Here is an excerpt from the listener we created:

@Slf4j
public class GebTestNGTestListener implements ITestListener, IConfigurationListener {
    int defaultTestNumber = 1

@Override
    void onTestFailure(ITestResult result) {
        if (result != null) {
            log.error("FAILURE: " + getTestDescription(result));
            grabScreenshots(result)
        }
    }

    private void grabScreenshots(ITestResult result) {
        Set<String> windows = getOpenWindows()
        for (String window : windows) {
            grabWindowScreenshot(window, result)
        }
    }

    private synchronized void grabWindowScreenshot(String window, ITestResult result) {
        Browser.drive {
            withWindow(window) {
                CreateScreenshot createScreenshot = new CreateScreenshot()

                def screenshotName = getScreenshotName(result) + "-" + defaultTestNumber
                createScreenshot.createScreenShot(screenshotName)
                defaultTestNumber++;

                Allure.LIFECYCLE.fire(new MakeAttachmentEvent(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES), screenshotName, "image/png"));

            }
        }
    }
}


And build gradle portion of solution:

  useTestNG() {
             
            options.useDefaultListeners = true
            options.outputDirectory = file("${buildDir}/test-results")
            options {
                listeners << 'com.smartrecruiters.geb.GebTestNGTestListener'
            }
         
        }

Hope this will help someone facing similar issue

Regards!

Mateusz

Reply all
Reply to author
Forward
0 new messages