Take screenshot in selenium (java) only in after method

I have created a automation suite in java selenium using testng frame work.Basically in my suite the scenarios are placed in between before and after method. I want to take the screen shot for each failed and passed test scenario.For that i have created a separate class and calling it in each script to capture the screenshot before the after Method. The issue that i am facing here is if the scenario is getting failed the script stopped executing and it is not moving to the take screenshot line of code and so it is not capturing the screenshot for the failed ones. So i want the take screenshot program to be only placed in after method so before the driver quit it will take the screenshot inspite of the scenario result of pass/fail. I have written code for nearly 20 scenario using testng. Can some one tel me the code in the after method only to take the screenshot without so much effecting the code which i have written.

The screenshot program should only be in the after method so it will capture teh screen before driver quit.


Make sure you catch exceptions so you won't exit the code prematurely.

For example, this is how I take screenshots after every test run (passed or failed). I use the exception value (if any) to dynamically name the screenshot files.

First, an example test method:

    [DataSource("System.Data.SqlClient", "Data Source=DEV338X2B;Initial Catalog=SeleniumDb;Integrated Security=True",
        "dbo.F_single_ErkenningAannemerKlasse1", DataAccessMethod.Sequential), TestMethod]
    public void erkenningAannemerKlasse1()
    {
        try
        {
            frontUser.erkenningAannemerKlasse1(data);
        }
        catch (Exception e)
        {
            exception = e.Message + " " + e.StackTrace;
        }
    }

Then this method executes after every test. Default behaviour in Microsoft UnitTest framework, in this case.

    [TestCleanup()]
    public void Cleanup()
    {
        frontUser.takeScreenshot(data.get("testnaam"), data.get("datarow"), exception);
        frontUser.closeBrowser();
    }
链接地址: http://www.djcxy.com/p/67398.html

上一篇: Selenium:无法截取失败的登录页面

下一篇: 仅在after方法中使用selenium(java)截图