Access to the Context in a Listener

44 views
Skip to first unread message

Felipe Knorr Kuhn

unread,
Feb 23, 2010, 1:11:52 PM2/23/10
to testng...@googlegroups.com
Hello all,

I'm trying to implement a IInvokedMethodListener to capture screenshots of failed tests as posted in [1]

The code is working as is, but I want to tweak it a bit, to use same directory set in the Context (context.getOutputDirectory()) to save the files.

Unfortunately, there's no way to access that through the Listener interface.

Any ideas?

Thanks and regards,

FK

[1] http://seleniumexamples.com/blog/examples/capturing-screenshots-from-remote-selenium-rc

Cédric Beust ♔

unread,
Feb 23, 2010, 1:57:06 PM2/23/10
to testng...@googlegroups.com
Hi Felipe,

This is possible in 5.12 (currently in beta):  there is a new interface called IInvokedMethodListener2:

/**
 * Implement this interface if you need a handle to {@link ITestContext}.
 *
 * @author karthik....@gmail.com (Karthik Krishnan)
 */
public interface IInvokedMethodListener2 extends IInvokedMethodListener {

  /**
   * To be implemented if the method needs a handle to contextual information.
   */
  void beforeInvocation(IInvokedMethod method, ITestResult testResult,
      ITestContext context);

  /**
   * To be implemented if the method needs a handle to contextual information.
   */
  void afterInvocation(IInvokedMethod method, ITestResult testResult,
      ITestContext context);
}

-- 
Cedric



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.



--
Cédric


Cédric Beust ♔

unread,
Feb 23, 2010, 1:57:21 PM2/23/10
to testng...@googlegroups.com
Sorry, forgot the link:  http://testng.org/beta


2010/2/23 Cédric Beust ♔ <cbe...@google.com>



--
Cédric


Felipe Knorr Kuhn

unread,
Feb 23, 2010, 2:33:04 PM2/23/10
to testng...@googlegroups.com
Thanks Cédric :)

I didn't update to 5.12beta because it broke my Interceptor and I didn't have time to fix it...

Everything is working now, except for an error that started showing up after I run the following line:

String base64Screenshot = ((BaseTest) result.getTestClass().getInstances(true)[0]).getSelenium().captureEntirePageScreenshotToString("");

I'm sure there is a better way to get the Selenium instance in the Listener, but I have no idea what it would be :)

16:28:09.256 INFO - Stopped HttpContext[/,/]
16:28:09.256 INFO - Stopped org.mortbay.jetty.Server@e6f7d2
com.thoughtworks.selenium.SeleniumException: Connection refused
    at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:107)
    at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:86)
    at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:262)
    at com.thoughtworks.selenium.DefaultSelenium.captureEntirePageScreenshotToString(DefaultSelenium.java:723)
    at common.InvocationListener.afterInvocation(InvocationListener.java:37)
    at org.testng.internal.Invoker.runInvokedMethodListeners(Invoker.java:474)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:162)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:92)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:307)
    at org.testng.SuiteRunner.run(SuiteRunner.java:195)
    at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:904)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:871)
    at org.testng.TestNG.run(TestNG.java:779)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:75)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:127)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:335)
    at sun.net.www.protocol.http.HttpURLConnection.setNewClient(HttpURLConnection.java:540)
    at sun.net.www.protocol.http.HttpURLConnection.setNewClient(HttpURLConnection.java:528)
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:474)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1062)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
    at com.thoughtworks.selenium.HttpCommandProcessor.getResponseCode(HttpCommandProcessor.java:144)
    at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:164)
    at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:104)
    ... 15 more

Thanks again,

FK

2010/2/23 Cédric Beust ♔ <cbe...@google.com>

Felipe Knorr Kuhn

unread,
Feb 24, 2010, 2:58:59 PM2/24/10
to testng...@googlegroups.com
It seems like I was getting this error because the setupBeforeSuite and setupAfterSuite methods are passed to the listener.

At the point setupBeforeSuite was called, my Selenium Server object did not exist anymore, thus causing the "Connection Refused" error.

Regards,
FK

Felipe Knorr Kuhn

unread,
Feb 24, 2010, 3:54:44 PM2/24/10
to testng...@googlegroups.com
I forgot to mention that my "workaround" for this was ignoring the setupBeforeSuite and setupAfterSuite methods in afterInvocation():

if ((method.getTestMethod().getMethodName() != "setupBeforeSuite") && (method.getTestMethod().getMethodName() != "setupAfterSuite"))
{
if (!result.isSuccess()) {
...

Cédric Beust ♔

unread,
Feb 24, 2010, 3:56:14 PM2/24/10
to testng...@googlegroups.com
!= and not equals()  ?

Felipe Knorr Kuhn

unread,
Feb 24, 2010, 4:08:20 PM2/24/10
to testng...@googlegroups.com
Changed to ("setupBeforeSuite").equals(method.getTestMethod().getMethodName())

Thanks for the tip :)

FK

2010/2/24 Cédric Beust ♔ <cbe...@google.com>
Reply all
Reply to author
Forward
0 new messages