Paulo Roberto
Paulo Roberto

Reputation: 1548

TakesScreenshot in Selenium with Java does not work

I'm trying to create a function to get Screenshot in selenium, at the end of the tests. I am passing 3 parameters "Test result", "Message", "True / False" for the user to decide whether or not to take a print.

However, when executing the function call, it does not take the print off the screen, it generates the HTML with the executed steps, but without the print.

Function

public void escreveRelatorio(boolean status, String msg, boolean printScreen) {

        scenario.write(msg);

        if(printScreen) {
            scenario.embed(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES), "image/png");
        }

        if(status == false) {
            Assert.fail(msg);
        }

    }

Function call in the middle of the tests.

generic.escreveRelatorio(false, "O número de confirmação foi gerado: " + num, true);

Or

public void validaNumeroConfirmacao() {

    String num = generic.obterTexto(lblConfirmationNumber).substring(23);

    if(!num.equals("1")){
        generic.escreveRelatorio(false, "O número de confirmação foi gerado: " + num, true);
    } else {
        generic.escreveRelatorio(true, "O número de confirmação foi gerado: " + num, true);
    }

}

What am I doing wrong?

Upvotes: 0

Views: 183

Answers (1)

Sumit2500
Sumit2500

Reputation: 183

Problem with above example is you are not specifying where your screenshot should save.

Your getScreenshotAs() will return byte[] but you are not doing anything with it.

File scrfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);    
FileUtils.copyFile(scrfile, new File("/screenshot/SomeUniqueName.png"));

Upvotes: 1

Related Questions