0

I have written a script to login to a webpage. I deliberately give incorrect credentials and click on submit. The test should fail here. When I run from terminal in Visual Studio, I see it fails rightly. But I also have a piece of code to capture screenshot and generate log when the test fails and extract all that in html file. When I open the file after the test execution stops, it shows as the test passed. Can anyone please help ?

I call the login function from another class:

public shopping validlogin(string user, string pwd)
{
    username.SendKeys(user);
    password.SendKeys(pwd);
    chkbox.Click();
    sbmt.Click();
    return new shopping(driver);
}

The code for one time setup

public ExtentReports reporter;
public ExtentTest test;
String browsername;

//generate report file
[OneTimeSetUp]
public void setup()
{
    string Curdirectory = Environment.CurrentDirectory;
    string Projdirectory = Directory.GetParent(Curdirectory).Parent.Parent.FullName;
    String reportpath = Projdirectory + "//index.html";
    var htmlreporter = new ExtentHtmlReporter(reportpath);
    reporter = new ExtentReports();
    reporter.AttachReporter(htmlreporter);
}

Here is the code that generates screenshot:

[TearDown]
public void AfterTest()
{
    var status = TestContext.CurrentContext.Result.Outcome.Status;
    var stackTrace = TestContext.CurrentContext.Result.StackTrace;

    DateTime time = DateTime.Now;
    String fileName = "Screenshot_" + time.ToString("h_mm_ss") + ".png";

    if (status == TestStatus.Failed)
    {
        TestContext.Progress.WriteLine(status);
        test.Fail("Test failed", captureScreenShot(driver.Value, fileName));
        test.Log(Status.Fail, "test failed with logtrace" + stackTrace);

    }
    else if (status == TestStatus.Passed)
    {

    }

    reporter.Flush();
    driver.Value.Quit();
}

public MediaEntityModelProvider captureScreenShot(IWebDriver driver, String screenShotName)
{
    ITakesScreenshot ts = (ITakesScreenshot)driver;
    var screenshot = ts.GetScreenshot().AsBase64EncodedString;

    return MediaEntityBuilder.CreateScreenCaptureFromBase64String(screenshot, screenShotName).Build();
}
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Megatron
  • 13
  • 1
  • 4

1 Answers1

0

test was null since only declaration was done so null exception was seen on the terminal log. This problem was fixed when I added below line for initialization: test = reporter.CreateTest(TestContext.CurrentContext.Test.Name);

Megatron
  • 13
  • 1
  • 4