FAQ
Hello Group :)

I am running selenium tests written in Java as a JUnit task. On a headless
Linux machine with the use of FirefoxDriver and Xvfb. I would like to take
a screenshot when the test fails. There is the following piece of code to
take the screenshots:

@Rule
public TestName name = new TestName();


/**
* TestWatchmen used to take screenshots of browser window if a test
fails.
*/

@Rule
public MethodRule watchman = new TestWatchman() {
@Override
public void failed(Throwable e, FrameworkMethod method) {
System.err.println("TAKING SCREENSHOT");
File scrFile = ((TakesScreenshot)
driver).getScreenshotAs(OutputType.FILE);
Calendar now = new GregorianCalendar();
File outDir = new File(OUT_DIR);
outDir.mkdir();
System.err.println("Output directory: " +
outDir.getAbsolutePath());
//String scrFilename = e.getStackTrace()[0].toString() + ".png";
String scrFilename = name.getMethodName() + ".png";

System.err.println("Taking screenshot to " + scrFilename);


File outputFile = new File(outDir, scrFilename);

try {
FileUtils.copyFile(scrFile, outputFile);
} catch (IOException ioe) {
System.err.println("Failed.");
}

}
};

And here there is a part of the testing function that generates an error:

help.clickDefault("Subnet mask:");
assertTrue(help.isParameterEnabled("Subnet mask:"));
* assertEquals("255.255.255.0", help.getText("Subnet mask:"));*

help.getText("Subnet mask:") returns "255.255.255.255" so the assert fails.
In the stderr of the test I see the following:

INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText {"id":"{65097ec4-ae14-4cff-b99f-d76f2281f608}"}]
Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText {"id":"{dbc96864-ba6e-42de-a8d9-e5a479e0c370}"}]
Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, quit {}]
TAKING SCREENSHOT
Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
INFO: Executing: [null, screenshot {}]

And in the XML report of the junit test there's this message:

Error MessageError communicating with the remote browser. It may have died.
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
09:57:28' System info: os.name: 'Linux', os.arch: 'amd64', os.version:
'2.6.26-2-openvz-amd64', java.version: '1.6.0_18' Driver info:
driver.version: FirefoxDriverStacktraceorg.openqa.selenium.remote.UnreachableBrowserException:
Error communicating with the remote browser. It may have died.
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
09:57:28'
System info: os.name: 'Linux', os.arch: 'amd64', os.version:
'2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
Driver info: driver.version: FirefoxDriver
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:421)

at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:428)

at
org.openqa.selenium.firefox.FirefoxDriver.getScreenshotAs(FirefoxDriver.java:204)
at validation$1.failed(validation.java:60)
Caused by: org.openqa.selenium.WebDriverException: The FirefoxDriver cannot
be used after quit() was called.
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
09:57:28' System info: os.name: 'Linux', os.arch: 'amd64', os.version:
'2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
Driver info: driver.version: FirefoxDriver at
org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:235)

at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:401)

It looks like the @After function was called before the screenshot and
there is no firefox running. Is there a way to make Junit execute the
TestWatchman before the @After function?

Best regards
Bogdan Katyński




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/QvBq8o1VZbkJ.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Search Discussions

  • Thomas Sundberg at Jul 12, 2012 at 7:35 am
    Hi!

    I just wrote a blog post about this. The issue you are facing is due
    to the fact that the browser is closed when you are about to take the
    screen shot.
    My solution is to close the browser in a finished() method in the rule.

    Read all about it here:

    http://thomassundberg.wordpress.com/2012/07/08/performing-an-action-when-a-test-fails/

    HTH
    Thomas
    On 12 July 2012 09:24, Bogdan Katyński wrote:
    Hello Group :)

    I am running selenium tests written in Java as a JUnit task. On a headless
    Linux machine with the use of FirefoxDriver and Xvfb. I would like to take a
    screenshot when the test fails. There is the following piece of code to take
    the screenshots:

    @Rule
    public TestName name = new TestName();


    /**
    * TestWatchmen used to take screenshots of browser window if a test
    fails.
    */

    @Rule
    public MethodRule watchman = new TestWatchman() {
    @Override
    public void failed(Throwable e, FrameworkMethod method) {
    System.err.println("TAKING SCREENSHOT");
    File scrFile = ((TakesScreenshot)
    driver).getScreenshotAs(OutputType.FILE);
    Calendar now = new GregorianCalendar();
    File outDir = new File(OUT_DIR);
    outDir.mkdir();
    System.err.println("Output directory: " +
    outDir.getAbsolutePath());
    //String scrFilename = e.getStackTrace()[0].toString() + ".png";
    String scrFilename = name.getMethodName() + ".png";

    System.err.println("Taking screenshot to " + scrFilename);


    File outputFile = new File(outDir, scrFilename);

    try {
    FileUtils.copyFile(scrFile, outputFile);
    } catch (IOException ioe) {
    System.err.println("Failed.");
    }

    }
    };

    And here there is a part of the testing function that generates an error:

    help.clickDefault("Subnet mask:");
    assertTrue(help.isParameterEnabled("Subnet mask:"));
    assertEquals("255.255.255.0", help.getText("Subnet mask:"));

    help.getText("Subnet mask:") returns "255.255.255.255" so the assert fails.
    In the stderr of the test I see the following:

    INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText
    {"id":"{65097ec4-ae14-4cff-b99f-d76f2281f608}"}]
    Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
    INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText
    {"id":"{dbc96864-ba6e-42de-a8d9-e5a479e0c370}"}]
    Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
    INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, quit {}]
    TAKING SCREENSHOT
    Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
    INFO: Executing: [null, screenshot {}]

    And in the XML report of the junit test there's this message:

    Error Message

    Error communicating with the remote browser. It may have died. Build info:
    version: '2.15.0', revision: '15105', time: '2011-12-08 09:57:28' System
    info: os.name: 'Linux', os.arch: 'amd64', os.version:
    '2.6.26-2-openvz-amd64', java.version: '1.6.0_18' Driver info:
    driver.version: FirefoxDriver

    Stacktrace

    org.openqa.selenium.remote.UnreachableBrowserException: Error communicating
    with the remote browser. It may have died.
    Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
    09:57:28'
    System info: os.name: 'Linux', os.arch: 'amd64', os.version:
    '2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
    Driver info: driver.version: FirefoxDriver
    at
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:421)
    at
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:428)
    at
    org.openqa.selenium.firefox.FirefoxDriver.getScreenshotAs(FirefoxDriver.java:204)
    at validation$1.failed(validation.java:60)
    Caused by: org.openqa.selenium.WebDriverException: The FirefoxDriver cannot
    be used after quit() was called.
    Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
    09:57:28' System info: os.name: 'Linux', os.arch: 'amd64', os.version:
    '2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
    Driver info: driver.version: FirefoxDriver at
    org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:235)
    at
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:401)

    It looks like the @After function was called before the screenshot and there
    is no firefox running. Is there a way to make Junit execute the TestWatchman
    before the @After function?

    Best regards
    Bogdan Katyński




    --
    You received this message because you are subscribed to the Google Groups
    "Selenium Users" group.
    To view this discussion on the web visit
    https://groups.google.com/d/msg/selenium-users/-/QvBq8o1VZbkJ.
    To post to this group, send email to selenium-users@googlegroups.com.
    To unsubscribe from this group, send email to
    selenium-users+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/selenium-users?hl=en-US.


    --
    Thomas Sundberg
    M. Sc. in Computer Science

    Mobile: +46 70 767 33 15
    Blog: http://thomassundberg.wordpress.com/
    Twitter: @thomassundberg

    Better software through faster feedback

    --
    You received this message because you are subscribed to the Google Groups "Selenium Users" group.
    To post to this group, send email to selenium-users@googlegroups.com.
    To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.
  • Bogdan Katyński at Jul 12, 2012 at 12:18 pm
    Hi Thomas

    Thank you very much for your help. I read your blog post and it works this
    way.

    Best regards
    Bogdan

    W dniu czwartek, 12 lipca 2012 09:34:39 UTC+2 użytkownik Thomas Sundberg
    napisał:
    Hi!

    I just wrote a blog post about this. The issue you are facing is due
    to the fact that the browser is closed when you are about to take the
    screen shot.
    My solution is to close the browser in a finished() method in the rule.

    Read all about it here:


    http://thomassundberg.wordpress.com/2012/07/08/performing-an-action-when-a-test-fails/

    HTH
    Thomas
    Hello Group :)

    I am running selenium tests written in Java as a JUnit task. On a headless
    Linux machine with the use of FirefoxDriver and Xvfb. I would like to take a
    screenshot when the test fails. There is the following piece of code to take
    the screenshots:

    @Rule
    public TestName name = new TestName();


    /**
    * TestWatchmen used to take screenshots of browser window if a test
    fails.
    */

    @Rule
    public MethodRule watchman = new TestWatchman() {
    @Override
    public void failed(Throwable e, FrameworkMethod method) {
    System.err.println("TAKING SCREENSHOT");
    File scrFile = ((TakesScreenshot)
    driver).getScreenshotAs(OutputType.FILE);
    Calendar now = new GregorianCalendar();
    File outDir = new File(OUT_DIR);
    outDir.mkdir();
    System.err.println("Output directory: " +
    outDir.getAbsolutePath());
    //String scrFilename = e.getStackTrace()[0].toString() + ".png";
    String scrFilename = name.getMethodName() + ".png";

    System.err.println("Taking screenshot to " + scrFilename);


    File outputFile = new File(outDir, scrFilename);

    try {
    FileUtils.copyFile(scrFile, outputFile);
    } catch (IOException ioe) {
    System.err.println("Failed.");
    }

    }
    };

    And here there is a part of the testing function that generates an error:
    help.clickDefault("Subnet mask:");
    assertTrue(help.isParameterEnabled("Subnet mask:"));
    assertEquals("255.255.255.0", help.getText("Subnet mask:"));

    help.getText("Subnet mask:") returns "255.255.255.255" so the assert fails.
    In the stderr of the test I see the following:

    INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText
    {"id":"{65097ec4-ae14-4cff-b99f-d76f2281f608}"}]
    Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
    INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText
    {"id":"{dbc96864-ba6e-42de-a8d9-e5a479e0c370}"}]
    Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
    INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, quit {}]
    TAKING SCREENSHOT
    Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
    INFO: Executing: [null, screenshot {}]

    And in the XML report of the junit test there's this message:

    Error Message

    Error communicating with the remote browser. It may have died. Build info:
    version: '2.15.0', revision: '15105', time: '2011-12-08 09:57:28' System
    info: os.name: 'Linux', os.arch: 'amd64', os.version:
    '2.6.26-2-openvz-amd64', java.version: '1.6.0_18' Driver info:
    driver.version: FirefoxDriver

    Stacktrace

    org.openqa.selenium.remote.UnreachableBrowserException: Error
    communicating
    with the remote browser. It may have died.
    Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
    09:57:28'
    System info: os.name: 'Linux', os.arch: 'amd64', os.version:
    '2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
    Driver info: driver.version: FirefoxDriver
    at
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:421)
    at
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:428)
    at
    org.openqa.selenium.firefox.FirefoxDriver.getScreenshotAs(FirefoxDriver.java:204)
    at validation$1.failed(validation.java:60)
    Caused by: org.openqa.selenium.WebDriverException: The FirefoxDriver cannot
    be used after quit() was called.
    Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
    09:57:28' System info: os.name: 'Linux', os.arch: 'amd64', os.version:
    '2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
    Driver info: driver.version: FirefoxDriver at
    org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:235)
    at
    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:401)
    It looks like the @After function was called before the screenshot and there
    is no firefox running. Is there a way to make Junit execute the
    TestWatchman
    before the @After function?

    Best regards
    Bogdan Katyński




    --
    You received this message because you are subscribed to the Google Groups
    "Selenium Users" group.
    To view this discussion on the web visit
    https://groups.google.com/d/msg/selenium-users/-/QvBq8o1VZbkJ.
    To post to this group, send email to selenium-users@googlegroups.com.
    To unsubscribe from this group, send email to
    selenium-users+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/selenium-users?hl=en-US.


    --
    Thomas Sundberg
    M. Sc. in Computer Science

    Mobile: +46 70 767 33 15
    Blog: http://thomassundberg.wordpress.com/
    Twitter: @thomassundberg

    Better software through faster feedback
    --
    You received this message because you are subscribed to the Google Groups "Selenium Users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/KljAoh9ftJ4J.
    To post to this group, send email to selenium-users@googlegroups.com.
    To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupselenium-users @
categoriesselenium
postedJul 12, '12 at 7:25a
activeJul 12, '12 at 12:18p
posts3
users2
websiteseleniumhq.org

People

Translate

site design / logo © 2022 Grokbase