The type java.io.File cannot be resolved. It is indirectly referenced from required .class files | Selenium Forum
C
Cheong Kah Seng 5 days go

There are 4 error points bolded. Full error code also provided when running LeadTest.java

package com.qtpselenium.zoho.project.base//Error #1
import java.io.File;//Error #2
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.asserts.SoftAssert;

import com.qtpselenium.zoho.project.util.ExtentManager;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class BaseTest {
public WebDriver driver;
public Properties prop;
public ExtentReports rep = ExtentManager.getInstance();
public ExtentTest test;

public void init() throws IOException {
//init the prop file
if(prop==null) {
prop=new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir") + "\\src\\test\\resources\\projectconfig.properties");
prop.load(fs);
}
}

public void openBrowser(String bType) throws IOException {

//--System.out.println(prop.getProperty("appurl"));

//System.out.println(System.getProperty("user.dir"));
test.log(LogStatus.INFO, "Opening " + bType + " browser ");
if(bType.equals("Mozilla")) {
System.setProperty("webdriver.gecko.driver", "C:\\webdrivers\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(bType.equals("Chrome")) {
System.setProperty("webdriver.chrome.driver", prop.getProperty("chromedriver_exe"));
driver = new ChromeDriver();
}
else if(bType.equals("IE")) {
System.setProperty("webdriver.ie.driver", prop.getProperty("iedriver_exe"));
driver = new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//driver.manage().window().maximize(); //Unusable
test.log(LogStatus.INFO, bType + " browser opened successfully");

}

public void navigate(String urlKey) {
test.log(LogStatus.INFO, "Navigating to " + prop.getProperty(urlKey));
driver.get(prop.getProperty(urlKey));
}

/**
public void click(String xpathEleKey) {
driver.findElement(By.xpath(prop.getProperty(xpathEleKey))).click();
}

public void type(String xpathEleKey, String dataKey) {
driver.findElement(By.xpath(prop.getProperty(xpathEleKey))).sendKeys(prop.getProperty(dataKey));
}
* @throws IOException
**/

public void click(String locatorKey) throws IOException {
test.log(LogStatus.INFO, "Clicking on " + locatorKey);
getElement(locatorKey).click();
test.log(LogStatus.INFO, "Clicked successfully on " + locatorKey);
}

public void type(String locatorKey, String data) throws IOException {
test.log(LogStatus.INFO, "Typing in " + locatorKey + ". Data - " + data);
getElement(locatorKey).sendKeys(data);
test.log(LogStatus.INFO, "Typed successfully on " + locatorKey);
}

//finding element and returning it
public WebElement getElement(String locatorKey) throws IOException {
WebElement e = null;
try {
if(locatorKey.endsWith("_id")) {
e = driver.findElement(By.id(prop.getProperty(locatorKey)));
}
else if(locatorKey.endsWith("_name")) {
e = driver.findElement(By.name(prop.getProperty(locatorKey)));
}
else if(locatorKey.endsWith("_xpath")) {
e = driver.findElement(By.xpath(prop.getProperty(locatorKey)));
}
else if(locatorKey.endsWith("_css")) {
e = driver.findElement(By.cssSelector(prop.getProperty(locatorKey)));
}
else {
reportFailure("Locator not correct - " + locatorKey);
Assert.fail("Locator not correct - " + locatorKey);
}
}catch(Exception ex) {
//fail the test and report the error
reportFailure(ex.getMessage());
ex.printStackTrace();
Assert.fail("Failed the test - " + ex.getMessage());
}
return e;
}

/********************Validations************************/
public boolean verifyTitle() {
return false;
}

public boolean isElementPresent(String locatorKey) throws IOException {
List<WebElement> elementList = null;
try {
if(locatorKey.endsWith("_id")) {
elementList = driver.findElements(By.id(prop.getProperty(locatorKey))); //use findElements
}
else if(locatorKey.endsWith("_name")) {
elementList = driver.findElements(By.name(prop.getProperty(locatorKey))); //use findElements
}
else if(locatorKey.endsWith("_xpath")) {
elementList = driver.findElements(By.xpath(prop.getProperty(locatorKey))); //use findElements
}
else {
reportFailure("Locator not correct - " + locatorKey);
Assert.fail("Locator not correct - " + locatorKey);
}
}catch(Exception ex) {
//fail the test and report the error
reportFailure("Locator not correct - " + locatorKey);
Assert.fail("Locator not correct - - " + locatorKey);
}
if(elementList.size()==0)
return false;
else
return true;
}

public boolean verifyText(String locatorKey, String expectedTextKey) throws IOException {
String actualText = getElement(locatorKey).getText().trim();
String expectedText=prop.getProperty(expectedTextKey);
if(actualText.equals(expectedText))
return true;
else
return false;
}

public void clickAndWait(String locator_clicked, String locator_pres) throws IOException {
test.log(LogStatus.INFO, "Clicking and waiting on - " + locator_clicked);
int count=5;
for(int i=0; i<count;i++) {
getElement(locator_clicked).click();
wait(2);
if(isElementPresent(locator_pres))
break;
}
}

/********************Reporting**************************/
public void reportPass(String msg) {
test.log(LogStatus.PASS, msg);
}

public void reportFailure(String msg) throws IOException {
test.log(LogStatus.FAIL, msg);
takeScreenshot();
Assert.fail(msg);
}

public void takeScreenshot() throws IOException{
Date d=new Date();
String filename=d.toString().replace(":", "_").replace(" ", "_");
//store screenshots in that file
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Error #3
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir")+"\\screenshots\\"+filename+".jpg")); //Error #4

//put screenshot file in reports
test.log(LogStatus.INFO, "Screenshot-> " + test.addScreenCapture(System.getProperty("user.dir")+"\\screenshots\\"+filename+".jpg"));
}

public void acceptAlert() {
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.alertIsPresent());
test.log(LogStatus.INFO, "Accepting alert");
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
}

//Reusable, but only for applications

public void waitForPageToLoad() {
JavascriptExecutor js=(JavascriptExecutor)driver;
String state = (String)js.executeScript("return document.readyState");

while(!state.equals("complete")) {
wait(2);
state = (String)js.executeScript("return document.readyState");

}
}

public void wait(int timeToWaitInSec) {
try {
Thread.sleep((timeToWaitInSec * 1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/*******************App functions
* @throws InterruptedException *********************/
public boolean doLogin(String username, String password) throws IOException, InterruptedException{
test.log(LogStatus.INFO, "Trying to login with " + username + ", " + password);
click("loginLink_xpath");


type("loginid_xpath", username);
wait(2); //Thread.sleep (wait() method) is important here to wait for the next button ready
click("loginIdNext_btn_xpath");
type("password_xpath", password);
click("pwSignIn_btn_xpath");




if(isElementPresent("crmlink_xpath")) {
test.log(LogStatus.INFO, "Login Success");

return true;
}
else {
test.log(LogStatus.INFO, "Login Failed");
return false;
}
}

public int getLeadRowNum(String leadName) {
test.log(LogStatus.INFO, "Finding the lead " + leadName);
List<WebElement> leadNames=driver.findElements(By.xpath(prop.getProperty("leadNamesCol_xpath")));
for(int i=0;i<leadNames.size();i++) {
System.out.println(leadNames.get(i).getText());
if(leadNames.get(i).getText().trim().equals(leadName)) {
test.log(LogStatus.INFO, "Lead found in row num " + (i+1));
return (i+1);
}
}
test.log(LogStatus.INFO, "Lead not found");
return -1;
}

public void clickOnLead(String leadName) {
test.log(LogStatus.INFO, "Clicking the lead " + leadName);
int rNum = getLeadRowNum(leadName);
driver.findElement(By.xpath(prop.getProperty("leadpart1_xpath")+rNum+prop.getProperty("leadpart2_xpath"))).click();
}


}

Full error code: 

[RemoteTestNG] detected TestNG version 7.0.0
org.testng.TestNGException:
Cannot instantiate class com.qtpselenium.zoho.project.testcases.LeadTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193)
at org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113)
at org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:114)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:172)
at org.testng.TestClass.getInstances(TestClass.java:102)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82)
at org.testng.TestClass.init(TestClass.java:74)
at org.testng.TestClass.<init>(TestClass.java:39)
at org.testng.TestRunner.initMethods(TestRunner.java:463)
at org.testng.TestRunner.init(TestRunner.java:342)
at org.testng.TestRunner.init(TestRunner.java:295)
at org.testng.TestRunner.<init>(TestRunner.java:226)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:679)
at org.testng.SuiteRunner.init(SuiteRunner.java:196)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:127)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1265)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1093)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 26 more
Caused by: java.lang.Error: Unresolved compilation problems:
The type java.io.File cannot be resolved. It is indirectly referenced from required .class files
Syntax error on token "base", ; expected after this token
The import java.io.File cannot be resolved
File cannot be resolved to a type
OutputType<File> cannot be resolved to a type
File cannot be resolved to a type

at com.qtpselenium.zoho.project.base.BaseTest.<init>(BaseTest.java:1)
at com.qtpselenium.zoho.project.testcases.LeadTest.<init>(LeadTest.java:21)
... 32 more

 

LeadTest.java

package com.qtpselenium.zoho.project.testcases;

import java.io.IOException;
import java.util.Hashtable;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import com.qtpselenium.zoho.project.base.BaseTest;
import com.qtpselenium.zoho.project.util.DataUtil;
import com.qtpselenium.zoho.project.util.Xls_Reader;
import com.relevantcodes.extentreports.LogStatus;

public class LeadTest extends BaseTest{
//Can read data from Excel file
Xls_Reader xls;
SoftAssert softAssert;

@Test(priority=1, dataProvider="getData")
public void createLeadTest(Hashtable<String, String> data) throws IOException, InterruptedException {
test = rep.startTest("CreateLeadTest");
if(!DataUtil.isRunnable("CreateLeadTest", xls) || data.get("Runmode").equals("N")) {
test.log(LogStatus.SKIP, "Skipping the test as runmode is N");
throw new SkipException("Skipping the test as run is N");
}



init();
openBrowser(data.get("Browser"));
navigate("appurl");

doLogin(prop.getProperty("username"), prop.getProperty("password"));
click("crmlink_xpath");
wait(1);
click("leadsTab_xpath");
wait(1);
click("plus_btn_xpath");
wait(1);
//click("newLead_dropDown_xpath");
//wait(3);
type("leadCompany_xpath", data.get("LeadCompany"));
type("leadLastName_xpath", data.get("LeadLastName"));
wait(2);
click("leadSaveButton_xpath");
wait(1);
clickAndWait("leadsTab_xpath", "plus_btn_xpath");

//validate
int rNum = getLeadRowNum(data.get("LeadLastName"));
if(rNum==-1)
reportFailure("Lead not found in lead table " + data.get("LeadLastName"));

reportPass("Lead found in lead table " + data.get("LeadLastName"));
takeScreenshot();
}

@Test(priority=2,dataProvider="getData")
public void convertLeadTest(Hashtable<String, String> data) throws IOException, InterruptedException {
//Assert.fail();
test = rep.startTest("ConvertLeadTest");
if(!DataUtil.isRunnable("ConvertLeadTest", xls) || data.get("Runmode").equals("N")) {
test.log(LogStatus.SKIP, "Skipping the test as runmode is N");
throw new SkipException("Skipping the test as run is N");
}

openBrowser(data.get("Browser"));
navigate("appurl");
doLogin(prop.getProperty("username"), prop.getProperty("password"));
click("crmlink_xpath");
wait(1);
click("leadsTab_xpath");
clickOnLead(data.get("LeadLastName"));
click("convertLead_xpath");
click("convertLeadandSave_xpath");
//validate
/**
click("clickDropDownForAcc_xpath");
wait(3);
clickAndWait("droppedAccount_xpath", "accPageImportbtn_xpath");
wait(1);
int rNum = getLeadRowNum(data.get("LeadLastName"));
**/
if(!isElementPresent("goToLeadAfterConvert_xpath"))
reportFailure("Lead is not converted to account table " + data.get("LeadLastName"));

reportPass("Lead is converted to account table " + data.get("LeadLastName"));
takeScreenshot();

}

@Test(priority=3, dataProvider="getDataDeleteLead") //Double check here, continue here tmr
public void deleteLeadAccountTest(Hashtable<String, String> data) throws IOException, InterruptedException {
test = rep.startTest("DeleteLeadAccountTest");
if(!DataUtil.isRunnable("DeleteLeadAccountTest", xls) || data.get("Runmode").equals("N")) {
test.log(LogStatus.SKIP, "Skipping the test as runmode is N");
throw new SkipException("Skipping the test as run is N");
}
openBrowser(data.get("Browser"));
navigate("appurl");
doLogin(prop.getProperty("username"), prop.getProperty("password"));
click("crmlink_xpath");
wait(1);
click("leadsTab_xpath");
clickOnLead(data.get("LeadLastName"));
waitForPageToLoad();
//click("clickDropDownDeleteLead");


click("dropDownWithDelete_css");
click("deleteButton_css");
wait(1);
click("confirmDelete_css");
//acceptAlert();
//wait(3);
click("backButtonLead_xpath");
int rNum = getLeadRowNum(data.get("LeadLastName"));
if(rNum==0)
reportFailure("Could not delete the Lead");
reportPass("Lead deleted successfully");
takeScreenshot();

}

@DataProvider
public Object[][] getData() throws IOException{
super.init();
xls = new Xls_Reader(prop.getProperty("xls_path"));
return DataUtil.getTestData(xls, "CreateLeadTest");

}

@DataProvider
public Object[][] getDataDeleteLead() throws IOException{
super.init();
xls = new Xls_Reader(prop.getProperty("xls_path"));
return DataUtil.getTestData(xls, "DeleteLeadAccountTest");
}

@BeforeMethod
public void init() {
softAssert = new SoftAssert();

}

@AfterMethod
public void quit() {

try {
softAssert.assertAll();
}catch(Error e) {
test.log(LogStatus.FAIL, e.getMessage());
}

if(rep!=null) {
rep.endTest(test);
rep.flush();
}
// if(driver!=null)
// driver.quit();
}

}

 

}