Using soft assertion does not attach correct snapshot for failed test

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using soft assertion does not attach correct snapshot for failed test

garvitagirotraev
Hi,

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

Issue is :
As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

Example :
Class util
{
  // contains create_chart(String parameter) method

  // 3 assertions are done everytime create_chart() method is called.
}

Class PieChart()
{
    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.
    util.create_chart("year");
    assertAll();
}


Can i attach screenshot at time when assertion fails?

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using soft assertion does not attach correct snapshot for failed test

Krishnan Mahadevan
Here's how you do it.

  1. subclass org.testng.asserts.SoftAssert
  2. Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) and include your screenshot recording logic
  3. Have all of your tests use the subclassed variant of SoftAssert [ That you created in (1) ]

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Thu, Dec 7, 2017 at 2:37 PM, <[hidden email]> wrote:
Hi,

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

Issue is :
As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

Example :
Class util
{
  // contains create_chart(String parameter) method

  // 3 assertions are done everytime create_chart() method is called.
}

Class PieChart()
{
    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.
    util.create_chart("year");
    assertAll();
}


Can i attach screenshot at time when assertion fails?

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using soft assertion does not attach correct snapshot for failed test

garvitagirotraev
Hi Krishnan,

I tried above way, i am facing few concerns :

1. I overrided onAssertFailure(), here i called method from Base class to capture screenshot.
 Below is code in Base class to capture screenshot and add it to allure report :

Class Base()
{
// screenshot taken if test case fails

@AfterMethod
    protected void screenShotIfFail(ITestResult result) throws IOException {
        if (!result.isSuccess()) {
            takeScreenShot(result.getMethod());
        }
    }
  
 @Attachment(value = "Page Screenshot", type = "image/png")
    private byte[] takeScreenShot(String name) throws IOException {
        String path = getRelativePath(name);
        File screenShot = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenShot, new File(path));

        String filename = makeScreenShotFileName(name);
        System.out.println("Taking Screenshot! " + filename);
        // Allure.addAttachment(filename, screenShot);
        Reporter.log("<a href=" + path + " target='_blank' >" + filename
                + "</a>");
        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    }

    protected void takeScreenShot(ITestNGMethod testMethod) throws IOException {
        String nameScreenShot = testMethod.getTestClass().getRealClass()
                .getSimpleName()
                + "_" + testMethod.getMethodName();
        takeScreenShot(nameScreenShot);
    }
    
    private String makeScreenShotFileName(String name) {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");
        Date date = new Date();
        return dateFormat.format(date) + "_" + name + ".png";
    }

    private String getRelativePath(String name) throws IOException {
        Path path = Paths.get(".", "target", "surefire-reports", "screenShots",
                makeScreenShotFileName(name));
        File directory = new File(path.toString());
        return directory.getCanonicalPath();
    }
}

Solutions tried :
1. I tried passing method name by this in Assertions class that extends SoftAssert :

ITestNGMethod name;

    public void setUp(ITestResult result) {

        name = result.getMethod();
    }
public void onAssertFailure(org.testng.asserts.IAssert A,
            java.lang.AssertionError ax) {
                Base b = new Base();
                b.takeScreenShot(name);
     }

But it passed null.

2. I tried getting method name using Thread.currentThread.getStackTrace() and called takeScreenShot(ITestNGMethod testMethod) from base class.
it returned calling method not test method. But snapshots were taken correctly. If i use this, snapshots are not attached to corresponding test method in allure report. 

Now, snapshots are taken at point when assertion fails but as test completes, softassertall() declares test as failed. and due to this snapshot code is written in @After test, it takes a snapshot again of last screen.

Is there any way to solve above?

Thanks !!
On Thursday, December 7, 2017 at 2:48:31 PM UTC+5:30, Krishnan Mahadevan wrote:
Here's how you do it.

  1. subclass org.testng.asserts.SoftAssert
  2. Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) and include your screenshot recording logic
  3. Have all of your tests use the subclassed variant of SoftAssert [ That you created in (1) ]

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Thu, Dec 7, 2017 at 2:37 PM, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ITAvb9YUBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">garvitag...@...> wrote:
Hi,

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

Issue is :
As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

Example :
Class util
{
  // contains create_chart(String parameter) method

  // 3 assertions are done everytime create_chart() method is called.
}

Class PieChart()
{
    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.
    util.create_chart("year");
    assertAll();
}


Can i attach screenshot at time when assertion fails?

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ITAvb9YUBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ITAvb9YUBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using soft assertion does not attach correct snapshot for failed test

Krishnan Mahadevan

I didn’t quite understand what is the problem. But it looks like you are trying to grab hold of the current @Test method’s ITestResult object from within some method that is invoked via a @Test method.

 

For doing that, you can always access the current @Test method’s ITestResult via Reporter.getCurrentTestResult() method.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of <[hidden email]>
Reply-To: <[hidden email]>
Date: Thursday, January 11, 2018 at 5:38 PM
To: testng-users <[hidden email]>
Subject: Re: [testng-users] Using soft assertion does not attach correct snapshot for failed test

 

Hi Krishnan,

 

I tried above way, i am facing few concerns :

 

1. I overrided onAssertFailure(), here i called method from Base class to capture screenshot.

 Below is code in Base class to capture screenshot and add it to allure report :

 

Class Base()

{

// screenshot taken if test case fails

 

@AfterMethod

    protected void screenShotIfFail(ITestResult result) throws IOException {

        if (!result.isSuccess()) {

            takeScreenShot(result.getMethod());

        }

    }

  

 @Attachment(value = "Page Screenshot", type = "image/png")

    private byte[] takeScreenShot(String name) throws IOException {

        String path = getRelativePath(name);

        File screenShot = ((TakesScreenshot) driver)

                .getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(screenShot, new File(path));

 

        String filename = makeScreenShotFileName(name);

        System.out.println("Taking Screenshot! " + filename);

        // Allure.addAttachment(filename, screenShot);

        Reporter.log("<a href=" + path + " target='_blank' >" + filename

                + "</a>");

        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);

    }

 

    protected void takeScreenShot(ITestNGMethod testMethod) throws IOException {

        String nameScreenShot = testMethod.getTestClass().getRealClass()

                .getSimpleName()

                + "_" + testMethod.getMethodName();

        takeScreenShot(nameScreenShot);

    }

    

    private String makeScreenShotFileName(String name) {

        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");

        Date date = new Date();

        return dateFormat.format(date) + "_" + name + ".png";

    }

 

    private String getRelativePath(String name) throws IOException {

        Path path = Paths.get(".", "target", "surefire-reports", "screenShots",

                makeScreenShotFileName(name));

        File directory = new File(path.toString());

        return directory.getCanonicalPath();

    }

}

 

Solutions tried :

1. I tried passing method name by this in Assertions class that extends SoftAssert :

 

ITestNGMethod name;

 

    public void setUp(ITestResult result) {

 

        name = result.getMethod();

    }

public void onAssertFailure(org.testng.asserts.IAssert A,

            java.lang.AssertionError ax) {

                Base b = new Base();

                b.takeScreenShot(name);

     }

 

But it passed null.

 

2. I tried getting method name using Thread.currentThread.getStackTrace() and called takeScreenShot(ITestNGMethod testMethod) from base class.

it returned calling method not test method. But snapshots were taken correctly. If i use this, snapshots are not attached to corresponding test method in allure report. 

 

Now, snapshots are taken at point when assertion fails but as test completes, softassertall() declares test as failed. and due to this snapshot code is written in @After test, it takes a snapshot again of last screen.

 

Is there any way to solve above?

 

Thanks !!

On Thursday, December 7, 2017 at 2:48:31 PM UTC+5:30, Krishnan Mahadevan wrote:

Here's how you do it.

 

  1. subclass org.testng.asserts.SoftAssert
  2. Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) and include your screenshot recording logic
  3. Have all of your tests use the subclassed variant of SoftAssert [ That you created in (1) ]


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

On Thu, Dec 7, 2017 at 2:37 PM, <<a href="javascript:" target="_blank">garvitag...@...> wrote:

Hi,

 

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

 

Issue is :

As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

 

Example :

Class util

{

  // contains create_chart(String parameter) method

 

  // 3 assertions are done everytime create_chart() method is called.

}

 

Class PieChart()

{

    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.

    util.create_chart("year");

    assertAll();

}

 

 

Can i attach screenshot at time when assertion fails?

 

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank">testng-users...@....
To post to this group, send email to <a href="javascript:" target="_blank">testng...@....
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using soft assertion does not attach correct snapshot for failed test

garvitagirotraev
Reporter.getCurrentTestResult() helped .  Correct screenshots are attached to report.

Class chart()
{

softassert.assertTrue(expected, actual); // it fails, screenshot taken

// other functionalities

softassert.assertAll();  // @Test failed and screenshot taken again of last screen because of @After Test method
}

Is there a way to skip @AfterTest in case there is only assertion failure in test?

Thanks !1

On Thursday, January 11, 2018 at 10:16:40 PM UTC+5:30, Krishnan Mahadevan wrote:

I didn’t quite understand what is the problem. But it looks like you are trying to grab hold of the current @Test method’s ITestResult object from within some method that is invoked via a @Test method.

 

For doing that, you can always access the current @Test method’s ITestResult via Reporter.getCurrentTestResult() method.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

 

From: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="jKChUSz4DgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...> on behalf of <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="jKChUSz4DgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">garvitag...@...>
Reply-To: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="jKChUSz4DgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Date: Thursday, January 11, 2018 at 5:38 PM
To: testng-users <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="jKChUSz4DgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Subject: Re: [testng-users] Using soft assertion does not attach correct snapshot for failed test

 

Hi Krishnan,

 

I tried above way, i am facing few concerns :

 

1. I overrided onAssertFailure(), here i called method from Base class to capture screenshot.

 Below is code in Base class to capture screenshot and add it to allure report :

 

Class Base()

{

// screenshot taken if test case fails

 

@AfterMethod

    protected void screenShotIfFail(ITestResult result) throws IOException {

        if (!result.isSuccess()) {

            takeScreenShot(result.getMethod());

        }

    }

  

 @Attachment(value = "Page Screenshot", type = "image/png")

    private byte[] takeScreenShot(String name) throws IOException {

        String path = getRelativePath(name);

        File screenShot = ((TakesScreenshot) driver)

                .getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(screenShot, new File(path));

 

        String filename = makeScreenShotFileName(name);

        System.out.println("Taking Screenshot! " + filename);

        // Allure.addAttachment(filename, screenShot);

        Reporter.log("<a href=" + path + " target='_blank' >" + filename

                + "</a>");

        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);

    }

 

    protected void takeScreenShot(ITestNGMethod testMethod) throws IOException {

        String nameScreenShot = testMethod.getTestClass().getRealClass()

                .getSimpleName()

                + "_" + testMethod.getMethodName();

        takeScreenShot(nameScreenShot);

    }

    

    private String makeScreenShotFileName(String name) {

        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");

        Date date = new Date();

        return dateFormat.format(date) + "_" + name + ".png";

    }

 

    private String getRelativePath(String name) throws IOException {

        Path path = Paths.get(".", "target", "surefire-reports", "screenShots",

                makeScreenShotFileName(name));

        File directory = new File(path.toString());

        return directory.getCanonicalPath();

    }

}

 

Solutions tried :

1. I tried passing method name by this in Assertions class that extends SoftAssert :

 

ITestNGMethod name;

 

    public void setUp(ITestResult result) {

 

        name = result.getMethod();

    }

public void onAssertFailure(org.testng.asserts.IAssert A,

            java.lang.AssertionError ax) {

                Base b = new Base();

                b.takeScreenShot(name);

     }

 

But it passed null.

 

2. I tried getting method name using Thread.currentThread.getStackTrace() and called takeScreenShot(ITestNGMethod testMethod) from base class.

it returned calling method not test method. But snapshots were taken correctly. If i use this, snapshots are not attached to corresponding test method in allure report. 

 

Now, snapshots are taken at point when assertion fails but as test completes, softassertall() declares test as failed. and due to this snapshot code is written in @After test, it takes a snapshot again of last screen.

 

Is there any way to solve above?

 

Thanks !!

On Thursday, December 7, 2017 at 2:48:31 PM UTC+5:30, Krishnan Mahadevan wrote:

Here's how you do it.

 

  1. subclass org.testng.asserts.SoftAssert
  2. Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) and include your screenshot recording logic
  3. Have all of your tests use the subclassed variant of SoftAssert [ That you created in (1) ]


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

 

On Thu, Dec 7, 2017 at 2:37 PM, <[hidden email]> wrote:

Hi,

 

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

 

Issue is :

As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

 

Example :

Class util

{

  // contains create_chart(String parameter) method

 

  // 3 assertions are done everytime create_chart() method is called.

}

 

Class PieChart()

{

    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.

    util.create_chart("year");

    assertAll();

}

 

 

Can i attach screenshot at time when assertion fails?

 

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jKChUSz4DgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jKChUSz4DgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using soft assertion does not attach correct snapshot for failed test

garvitagirotraev
I found a workout, set a Boolean flag before assertAll() to true. It worked.

Thanks a lot !!!

On Friday, January 12, 2018 at 10:45:51 AM UTC+5:30, [hidden email] wrote:
Reporter.getCurrentTestResult() helped .  Correct screenshots are attached to report.

Class chart()
{

softassert.assertTrue(expected, actual); // it fails, screenshot taken

// other functionalities

softassert.assertAll();  // @Test failed and screenshot taken again of last screen because of @After Test method
}

Is there a way to skip @AfterTest in case there is only assertion failure in test?

Thanks !1

On Thursday, January 11, 2018 at 10:16:40 PM UTC+5:30, Krishnan Mahadevan wrote:

I didn’t quite understand what is the problem. But it looks like you are trying to grab hold of the current @Test method’s ITestResult object from within some method that is invoked via a @Test method.

 

For doing that, you can always access the current @Test method’s ITestResult via Reporter.getCurrentTestResult() method.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of <[hidden email]>
Reply-To: <[hidden email]>
Date: Thursday, January 11, 2018 at 5:38 PM
To: testng-users <[hidden email]>
Subject: Re: [testng-users] Using soft assertion does not attach correct snapshot for failed test

 

Hi Krishnan,

 

I tried above way, i am facing few concerns :

 

1. I overrided onAssertFailure(), here i called method from Base class to capture screenshot.

 Below is code in Base class to capture screenshot and add it to allure report :

 

Class Base()

{

// screenshot taken if test case fails

 

@AfterMethod

    protected void screenShotIfFail(ITestResult result) throws IOException {

        if (!result.isSuccess()) {

            takeScreenShot(result.getMethod());

        }

    }

  

 @Attachment(value = "Page Screenshot", type = "image/png")

    private byte[] takeScreenShot(String name) throws IOException {

        String path = getRelativePath(name);

        File screenShot = ((TakesScreenshot) driver)

                .getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(screenShot, new File(path));

 

        String filename = makeScreenShotFileName(name);

        System.out.println("Taking Screenshot! " + filename);

        // Allure.addAttachment(filename, screenShot);

        Reporter.log("<a href=" + path + " target='_blank' >" + filename

                + "</a>");

        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);

    }

 

    protected void takeScreenShot(ITestNGMethod testMethod) throws IOException {

        String nameScreenShot = testMethod.getTestClass().getRealClass()

                .getSimpleName()

                + "_" + testMethod.getMethodName();

        takeScreenShot(nameScreenShot);

    }

    

    private String makeScreenShotFileName(String name) {

        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");

        Date date = new Date();

        return dateFormat.format(date) + "_" + name + ".png";

    }

 

    private String getRelativePath(String name) throws IOException {

        Path path = Paths.get(".", "target", "surefire-reports", "screenShots",

                makeScreenShotFileName(name));

        File directory = new File(path.toString());

        return directory.getCanonicalPath();

    }

}

 

Solutions tried :

1. I tried passing method name by this in Assertions class that extends SoftAssert :

 

ITestNGMethod name;

 

    public void setUp(ITestResult result) {

 

        name = result.getMethod();

    }

public void onAssertFailure(org.testng.asserts.IAssert A,

            java.lang.AssertionError ax) {

                Base b = new Base();

                b.takeScreenShot(name);

     }

 

But it passed null.

 

2. I tried getting method name using Thread.currentThread.getStackTrace() and called takeScreenShot(ITestNGMethod testMethod) from base class.

it returned calling method not test method. But snapshots were taken correctly. If i use this, snapshots are not attached to corresponding test method in allure report. 

 

Now, snapshots are taken at point when assertion fails but as test completes, softassertall() declares test as failed. and due to this snapshot code is written in @After test, it takes a snapshot again of last screen.

 

Is there any way to solve above?

 

Thanks !!

On Thursday, December 7, 2017 at 2:48:31 PM UTC+5:30, Krishnan Mahadevan wrote:

Here's how you do it.

 

  1. subclass org.testng.asserts.SoftAssert
  2. Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) and include your screenshot recording logic
  3. Have all of your tests use the subclassed variant of SoftAssert [ That you created in (1) ]


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

 

On Thu, Dec 7, 2017 at 2:37 PM, <[hidden email]> wrote:

Hi,

 

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

 

Issue is :

As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

 

Example :

Class util

{

  // contains create_chart(String parameter) method

 

  // 3 assertions are done everytime create_chart() method is called.

}

 

Class PieChart()

{

    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.

    util.create_chart("year");

    assertAll();

}

 

 

Can i attach screenshot at time when assertion fails?

 

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Using soft assertion does not attach correct snapshot for failed test

Krishnan Mahadevan

No out of the box exists. You would need to build this on your own by retrieving the current ITestContext and then checking if it has any failures and if so then skip the execution of @AfterTest.


On Fri, Jan 12, 2018, 11:25 <[hidden email]> wrote:
I found a workout, set a Boolean flag before assertAll() to true. It worked.

Thanks a lot !!!


On Friday, January 12, 2018 at 10:45:51 AM UTC+5:30, [hidden email] wrote:
Reporter.getCurrentTestResult() helped .  Correct screenshots are attached to report.

Class chart()
{

softassert.assertTrue(expected, actual); // it fails, screenshot taken

// other functionalities

softassert.assertAll();  // @Test failed and screenshot taken again of last screen because of @After Test method
}

Is there a way to skip @AfterTest in case there is only assertion failure in test?

Thanks !1

On Thursday, January 11, 2018 at 10:16:40 PM UTC+5:30, Krishnan Mahadevan wrote:

I didn’t quite understand what is the problem. But it looks like you are trying to grab hold of the current @Test method’s ITestResult object from within some method that is invoked via a @Test method.

 

For doing that, you can always access the current @Test method’s ITestResult via Reporter.getCurrentTestResult() method.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of <[hidden email]>
Reply-To: <[hidden email]>
Date: Thursday, January 11, 2018 at 5:38 PM
To: testng-users <[hidden email]>
Subject: Re: [testng-users] Using soft assertion does not attach correct snapshot for failed test

 

Hi Krishnan,

 

I tried above way, i am facing few concerns :

 

1. I overrided onAssertFailure(), here i called method from Base class to capture screenshot.

 Below is code in Base class to capture screenshot and add it to allure report :

 

Class Base()

{

// screenshot taken if test case fails

 

@AfterMethod

    protected void screenShotIfFail(ITestResult result) throws IOException {

        if (!result.isSuccess()) {

            takeScreenShot(result.getMethod());

        }

    }

  

 @Attachment(value = "Page Screenshot", type = "image/png")

    private byte[] takeScreenShot(String name) throws IOException {

        String path = getRelativePath(name);

        File screenShot = ((TakesScreenshot) driver)

                .getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(screenShot, new File(path));

 

        String filename = makeScreenShotFileName(name);

        System.out.println("Taking Screenshot! " + filename);

        // Allure.addAttachment(filename, screenShot);

        Reporter.log("<a href=" + path + " target='_blank' >" + filename

                + "</a>");

        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);

    }

 

    protected void takeScreenShot(ITestNGMethod testMethod) throws IOException {

        String nameScreenShot = testMethod.getTestClass().getRealClass()

                .getSimpleName()

                + "_" + testMethod.getMethodName();

        takeScreenShot(nameScreenShot);

    }

    

    private String makeScreenShotFileName(String name) {

        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");

        Date date = new Date();

        return dateFormat.format(date) + "_" + name + ".png";

    }

 

    private String getRelativePath(String name) throws IOException {

        Path path = Paths.get(".", "target", "surefire-reports", "screenShots",

                makeScreenShotFileName(name));

        File directory = new File(path.toString());

        return directory.getCanonicalPath();

    }

}

 

Solutions tried :

1. I tried passing method name by this in Assertions class that extends SoftAssert :

 

ITestNGMethod name;

 

    public void setUp(ITestResult result) {

 

        name = result.getMethod();

    }

public void onAssertFailure(org.testng.asserts.IAssert A,

            java.lang.AssertionError ax) {

                Base b = new Base();

                b.takeScreenShot(name);

     }

 

But it passed null.

 

2. I tried getting method name using Thread.currentThread.getStackTrace() and called takeScreenShot(ITestNGMethod testMethod) from base class.

it returned calling method not test method. But snapshots were taken correctly. If i use this, snapshots are not attached to corresponding test method in allure report. 

 

Now, snapshots are taken at point when assertion fails but as test completes, softassertall() declares test as failed. and due to this snapshot code is written in @After test, it takes a snapshot again of last screen.

 

Is there any way to solve above?

 

Thanks !!

On Thursday, December 7, 2017 at 2:48:31 PM UTC+5:30, Krishnan Mahadevan wrote:

Here's how you do it.

 

  1. subclass org.testng.asserts.SoftAssert
  2. Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) and include your screenshot recording logic
  3. Have all of your tests use the subclassed variant of SoftAssert [ That you created in (1) ]


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

On Thu, Dec 7, 2017 at 2:37 PM, <[hidden email]> wrote:

Hi,

 

I am using soft assertion for validating charts. At end of test i write assertAll() because i am using multiple assertions in one test. Assertion failures are reported correctly. Screenshot is attached for every failed test case.

 

Issue is :

As assertAll() is last line in test(supposed to be), when it reports that test is failed- screenshot is taken for current open page. Can we add screenshot when assertion fails?

 

Example :

Class util

{

  // contains create_chart(String parameter) method

 

  // 3 assertions are done everytime create_chart() method is called.

}

 

Class PieChart()

{

    util.create_chart("companies");    // if one assertion fails here, test continues and as test is reported fail after executing last line in test, screenshot is                                                                take for that screen.

    util.create_chart("year");

    assertAll();

}

 

 

Can i attach screenshot at time when assertion fails?

 

Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
--

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.