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();
}