Takescreenshot CODE - Error | Selenium C# Forum
S
Shreekanth Posted on 16/01/2019


ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();


string screenshotFile = DateTime.Now.ToString().Replace("/", "_").Replace(":", "_").Replace(" ", "_") + ".png";

string filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
filePath = Directory.GetParent(Directory.GetParent(filePath).FullName).FullName;

string screenshotPath = filePath + "\\Screenshots\\" + screenshotFile;
screenshot.SaveAsFile(filePath + "\\Screenshots\\" + screenshotFile, ScreenshotImageFormat.Png);


test.Log(Status.Info, "Screenshot - ", MediaEntityBuilder.CreateScreenCaptureFromPath(screenshotPath).Build());  ----> FAILS HERE AND THROWS BELOW MESSAGE??


test.AddScreenCaptureFromPath(screenshotPath);

 

ERROR Message:

Message: System.NullReferenceException : Object reference not set to an instance of an object.


G
gunjan Replied on 17/01/2019

Can you share the zip file of the entire code.


S
Shreekanth Replied on 17/01/2019

Hi Gunjan,

I have attached my BASETEST code.

Line 125 - Error message - test. (Null)

I have DEBUG the code and until line 120 - Path is available.

Thanks in advance,

Shreekanth

 

 

 


G
gunjan Replied on 17/01/2019

At which line are you getting the error then?


S
Shreekanth Replied on 17/01/2019

Below Line:

test.Log(Status.Info, "Screenshot - ", MediaEntityBuilder.CreateScreenCaptureFromPath(screenshotPath).Build()); ----> FAILS HERE AND THROWS BELOW MESSAGE??

 

Message: System.NullReferenceException : Object reference not set to an instance of an object.


G
gunjan Replied on 17/01/2019

Did you initialize the object of ExtentTest class in the TestCase from which you are calling the takeScreenshot() ?

like for example.,

test = rep.CreateTest("CreateLeadsTest", "This test will describe my CreateLeadsTest");


S
Shreekanth Replied on 17/01/2019

Thank you! I was looking at above specific line and I made a mistake..

Error --> test = rep.CreateTest("Reports Execution successful");

Corrected --> test = rep.CreateTest("ReportsTest", "Reports Execution successful");

Executed my test and screenshots are now attached in Extent Reports..

Thank you and you can close this ticket.