Throwing error for taking screenshot method | Selenium Forum
A
Avijeet Posted on 07/05/2019

I have written below code for taking screenshot.

 

File src= ((TakesScreenshot)cd).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new File("C:/Selenium_Project/PPDS_Weekly_Job/screenshots/abc.jpg"));

 

When  i'm writting below code in the test its working fine but when i'm creating a function as takescreenshot in base class and calling the function in the script then its throwing error.

 

basetest.java:17  code is- File src= ((TakesScreenshot)cd).getScreenshotAs(OutputType.FILE);

at PPDS_Weekly_Job.weeklyjob(PPDS_Weekly_Job.java:60)   is - calling the function in the script i.e takeScreenshot();

 

 

java.lang.NullPointerException
at basetest.takeScreenshot(basetest.java:17)
at PPDS_Weekly_Job.weeklyjob(PPDS_Weekly_Job.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)


===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@39ed3c8d: 16 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 16 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@606d8acf: 16 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@73a28541: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7de26db8: 78 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@76ccd017: 393 ms


A
Ashish Thakur Replied on 07/05/2019

at basetest.takeScreenshot(basetest.java:17)

 

Error is coming on line 17

What is line 17


A
Avijeet Replied on 08/05/2019

error line are :

basetest.java:17  code is- File src= ((TakesScreenshot)cd).getScreenshotAs(OutputType.FILE);

at PPDS_Weekly_Job.weeklyjob(PPDS_Weekly_Job.java:60)   is - calling the function in the script i.e takeScreenshot();

 

 

 


A
Ashish Thakur Replied on 08/05/2019

i am sure cd is null


A
Avijeet Replied on 15/05/2019

I haven't declare CD= null. I just declare as public WebDriver cd . Kindly advise if i need to declare anything else. Below given the complete code.

 

 

code for test:

package test;

import java.io.File;
import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import basetest.Base_Test;

public class PPDS_Script extends Base_Test{



@Test

public void weeklyjob() throws Exception{

System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\Driver\\ChromeDriver.exe");

WebDriver cd=new ChromeDriver();




cd.get("http://ppds.int.thomsonreuters.com/PPDSWeb/Login.aspx ");

cd.manage().window().maximize();

cd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);



cd.findElement(By.xpath("//*[@id=\"ctl00_loginControls_txtUserName\"]")).sendKeys("anvesh.r");
cd.findElement(By.xpath("//*[@id=\"ctl00_loginControls_txtPassword\"]")).sendKeys("Password456");


cd.findElement(By.xpath("//*[@id=\"ctl00_loginControls_btnSubmit\"]")).click();

takeScreenShot();

 

}

}

 

 

Code for baseTest:

package basetest;

import java.io.File;
import java.io.IOException;
import java.util.Date;

 

import org.apache.commons.io.FileUtils;
//import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class Base_Test {


public WebDriver cd;

// ######################### Take Screenshot ###############################################



public void takeScreenShot(){
Date d=new Date();
String screenshotFile=d.toString().replace(":", "_").replace(" ", "_")+".png";

File srcFile=((TakesScreenshot)cd).getScreenshotAs(OutputType.FILE);
try{


FileUtils.copyFile(srcFile, new File(System.getProperty("user.dir")+"//screenshots//"+screenshotFile));

}catch(IOException e){

e.printStackTrace();
}
//test.log(LogStatus.INFO, "Screenshot---> "+test.addScreenCapture(System.getProperty("user.dir")+"//screenshots//"+screenshotFile));
}


}

 

 


A
Avijeet Replied on 15/05/2019

In the base test cd is null so its throwing error. If i'm removing that statement then getting error in line no 17 as cd isn't defined. Please advise.


A
Avinash Vijaykumar Mahamuni Replied on 16/05/2019

if you are using extent report then you have to write 

 

start testcase and end test case in PPDS_Script file.


A
Avijeet Replied on 16/05/2019

m not using extent report. Just need to capture the screenshot.

if m writting WebDriver cd= new WebDriver(); in both base and test package then its working find but multiple browser is getting opened with this statement.


A
Avinash Vijaykumar Mahamuni Replied on 16/05/2019

Can you please put only cd= new WebDriver(); instead of WebDriver cd= new WebDriver(); in test case file PPDS_Script and let me know.


A
Ashish Thakur Replied on 17/05/2019

Yes please try this


A
Avijeet Replied on 20/05/2019

Thanks a lot.  its working fine now. if you could tell me the exact reason of the error , it ll be very much helpful for me?? Thanks .


A
Ashish Thakur Replied on 22/05/2019

When ever we write WebDriver cd = new ____________();

It creates a new local cd variable ignoring the originally created global variable


A
Avijeet Replied on 22/05/2019

thanks. Query can be close now.