在硒java中获取错误“ java.lang.reflect.InvocationTargetException”。我已经阅读了导致此错误的原因,但不知道代码中哪一行会导致这些错误。我是自动化的新手,所以不知道需要更正哪条线。这是我的代码:
///////This is the class where my test case is being executed.
public class LoginTestCase extends ExtentReportBaseClass {
static WebDriver driver;
//Homepage login = new Homepage();
UtilityMethods util = new UtilityMethods();
configFileReader configReaderObj=new configFileReader();
String locUsernameElem;
String locPasswordelem;
String Sign_in;
String insertEmail;
String insertFirstName;
String createAccountButton;
// @Parameters("browser")
@BeforeClass
public void launchBrowser() {
driver= UtilityMethods.openBrowser(ConstantsValues.BROWSER_NAME);
UtilityMethods.launchWebsite(Utility.ConstantsValues.URL);
driver.manage().window().maximize();
//report = ExtentReportBaseClass.setUp();
}
@Test
public void registration() throws InterruptedException, IOException {
test = report.createTest("registration", "This will check status for registration of the user.");
Sign_in = Utility.UtilityLocatorz.SignUp.SIGN_IN;
reportLog("User will create a new account");
createAccountButton = Utility.UtilityLocatorz.SignUp.CREATE_ACOUNT;
util.clickElement(Sign_in);
Thread.sleep(2000);
reportLog("User will put his personal details");
driver.findElement(By.name("email_create")).sendKeys("vivek443333111@gmail.com");
//util.sendData(insertEmail);
util.clickElement(UtilityLocatorz.SignUp.CREATE_ACCOUNT);
util.clickElement(UtilityLocatorz.SignUp.USER_TITLE);
reportLog("User will add personal details for his new account");
util.sendDataById("customer_firstname", UtilityLocatorz.SignUp.FIRST_NAME);
util.sendDataById("customer_lastname", UtilityLocatorz.SignUp.LAST_NAME);
util.sendData(UtilityLocatorz.SignUp.USER_PASSWORD);
util.clickElement(UtilityLocatorz.SignUp.USER_NEWSLETTER);
Select day = new Select(driver.findElement(By.id("days")));
day.selectByValue("1");
Select month = new Select(driver.findElement(By.id("months")));
month.selectByValue("2");
Select year = new Select(driver.findElement(By.id("years")));
year.selectByValue("2014");
util.sendData(SignUp.USER_COMPANY);
util.sendData(SignUp.USER_STATE);
util.sendData(SignUp.USER_ADDRESS1);
util.sendData(SignUp.USER_MOBILENUMBER);
util.sendData(SignUp.USER_ZIPCODE);
util.sendData(SignUp.USER_ALIAS);
util.sendData(SignUp.USER_CITY);
util.clickElement(SignUp.SUBMITACCOUNT);
reportLog("After creating a new account, User will login to his new account");
boolean chkElement=driver.findElement(By.className("View my customer account")).isDisplayed();
Assert.assertTrue(chkElement);
util.clickElement(SignUp.USER_SIGN_OUT);
//test.log(Status.PASS, MarkupHelper.createLabel("PASS", ExtentColor.GREEN));
}
............................................... ............................
This is the class where my utility methods are being called.
public class UtilityMethods extends ExtentReportBaseClass{
static WebDriver driver;
public static WebDriver openBrowser(String browsers) {
System.out.println("initiated browser is " + browsers);
if (browsers.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.firefox.marionette", "");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
driver = new FirefoxDriver(opt);
}
else if (browsers.equalsIgnoreCase("Chrome")) {
// String driverPath = System.getProperty("user.dir") +
// "\\src\\Drivers\\chromedriver";
// System.setProperty("webdriver.chrome.driver", driverPath);
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
return driver;
}
public static void launchWebsite(String URL) {
driver.get(URL);
}
public static By locateByElement(String locator, String locatorValue) {
By by = null;
switch (locator) {
case "id":
by = By.id(locatorValue);
break;
case "name":
by = By.name(locatorValue);
break;
case "linktext":
by = By.linkText(locatorValue);
break;
case "partialLinkText":
by = By.partialLinkText(locatorValue);
break;
case "className":
by = By.className(locatorValue);
break;
case "tagName":
by = By.tagName(locatorValue);
break;
case "cssSelector":
by = By.cssSelector(locatorValue);
break;
case "xpath":
by = By.xpath(locatorValue);
break;
}
System.out.println("value of by is" + by);
return by;
}
public void sendDataById(String locatorValue, String userData) {
driver.findElement(By.id(locatorValue)).sendKeys(userData);
}
public void sendData(String element) {
String[] var = element.split("###");
String loctype = var[0];
String locval = var[1];
// System.out.println(locval);
String uservalue = var[2];
// System.out.println(locval);
// System.out.println(uservalue);
By locator = locateByElement(loctype, locval);
System.out.println(loctype);
System.out.println(locval);
driver.findElement(locator).sendKeys(uservalue);
}
public WebDriver getdriver() {
if (driver == null) {
driver = new FirefoxDriver();
return driver;
} else {
return driver;
}
}
public void clickElement(String locElem) throws IOException {
try {
System.out.println(locElem);
String[] var = locElem.split("###");
String loctype = var[0];
String locval = var[1];
System.out.println(loctype);
System.out.println(locval);
By locator = locateByElement(loctype, locval);
driver.findElement(locator).click();
}
catch (Exception e) {
System.out.println("Catched Exception, Element not clicked is" + locElem);
}
}
////////堆栈跟踪...
Cannot instantiate class TestCases.LoginTestCase
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190)
at org.testng.TestClass.getInstances(TestClass.java:95)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81)
at org.testng.TestClass.init(TestClass.java:73)
at org.testng.TestClass.<init>(TestClass.java:38)
at org.testng.TestRunner.initMethods(TestRunner.java:389)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
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:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
... 25 more
Caused by: java.lang.StackOverflowError
at Utility.UtilityMethods.<init>(UtilityMethods.java:19)
at Utility.ExtentReportBaseClass.<init>(ExtentReportBaseClass.java:25)
at Utility.UtilityMethods.<init>(UtilityMethods.java:19)
at Utility.ExtentReportBaseClass.<init>(ExtentReportBaseClass.java:25)
at Utility.UtilityMethods.<init>(UtilityMethods.java:19)
at Utility.ExtentReportBaseClass.<init>(ExtentReportBaseClass.java:25)
at Utility.UtilityMethods.<init>(UtilityMethods.java:19)
at Utility.ExtentReportBaseClass.<init>(ExtentReportBaseClass.java:25)
at Utility.UtilityMethods.<init>(UtilityMethods.java:19)
at Utility.ExtentReportBaseClass.<init>(ExtentReportBaseClass.java:25)