Kindly help as m Getting InvocationTargetException when using Reflection API in Hybrid Framework kindly have a look at the attachment where i have mentioned DriverScript and GenericKeywords code and Exception error details | Selenium Training|Java|WebDriver|TestNG|Frameworks|Videos
10% April Month Discount Avail now

Selenium Training Forum

Kindly help as m Getting InvocationTargetException when using Reflection API in Hybrid Framework kindly have a look at the attachment where i have mentioned DriverScript and GenericKeywords code and Exception error details

H
Harshdeep Post on 03/04/2019

Query: On using Reflection API in Hybrid Framework, getting exception as ‘java.lang.reflect.InvocationTargetException’

 

DriverScript Code:

 

package com.dhagrp.hybrid.driver;

 

import java.lang.reflect.Method;

import java.util.Hashtable;

import java.util.Properties;

 

import com.dhagrp.hybrid.keywords.AppKeywords;

import com.dhagrp.hybrid.util.Xls_Reader;

 

public class DriverScript {

      public Properties envProp; // represents environments like sit or prod

      public Properties prop; // env.properties

      public AppKeywords app;

     

public void executeKeywords(String testName, Xls_Reader xls, Hashtable<String,String> testData){

      int rows = xls.getRowCount("Keywords");

      System.out.println("Rows "+rows);

      app= new AppKeywords();

      // send properties to keyword class

      app.setEnvProp(envProp);

      app.setProp(prop);

      // send the data

      app.setData(testData);

     

for(int rNum=2;rNum<=rows;rNum++){

     

      String tcid = xls.getCellData("Keywords", "TCID", rNum);

      if(tcid.equals(testName)){

      String keyword=xls.getCellData("Keywords", "Keyword", rNum);

      String objectKey=xls.getCellData("Keywords", "Object", rNum);

      String dataKey=xls.getCellData("Keywords", "Data", rNum);

      String data = testData.get(dataKey);

//    System.out.println(tcid + "---"+keyword+"-----"+prop.getProperty(objectKey)+"-----"+data);

     

      app.setDataKey(dataKey);

      app.setObjectKey(objectKey);

     

      /*

      if (keyword.equals("openBrowser"))

            app.openBrowser();

      else if (keyword.equals("navigate"))

            app.navigate();

      else if (keyword.equals("click"))

            app.click();

      else if (keyword.equals("type"))

            app.type();

      else if (keyword.equals("validateLogin"))

            app.validateLogin();

      }

}

}

*/

      //Reflections API in JAVA

     

      Method method;

      try{

            method=app.getClass().getMethod(keyword);

            method.invoke(app);

      }catch (Exception e){

            e.printStackTrace();

      }

      }

}

}

 

public Properties getEnvProp(){

      return envProp;

}

public void setEnvProp(Properties envProp){

this.envProp=envProp;

}

public Properties getProp(){

      return prop;

}

public void setProp(Properties Prop){

this.prop=Prop;

}

}

 

GenericKeywords Code:

package com.dhagrp.hybrid.keywords;

 

import java.util.Hashtable;

import java.util.Properties;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.edge.EdgeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

 

public class GenericKeywords {

              public Properties envProp; // represents environments like sit or prod

              public Properties prop; // env.properties

              public String objectKey;

              public String dataKey;

              public Hashtable<String,String> data;

              public WebDriver driver;

             

              /******************Setter Functions********************************/

              public void setEnvProp(Properties envProp) {

                            this.envProp = envProp;

              }

              public void setProp(Properties prop) {

                             this.prop = prop;

              }

              public void setObjectKey(String objectKey) {

                             this.objectKey = objectKey;

              }

              public void setDataKey(String dataKey) {

                             this.dataKey = dataKey;

              }

              public void setData(Hashtable<String, String> data) {

                             this.data = data;

              }

             

/*********************************************************/

              public void openBrowser(){

                             String browser=data.get(dataKey);

                             System.out.println("Opening Browser " +browser );

                             if (browser.equals("Mozila")){

                             //           System.setProperty("webdriver.geko.driver", "C:\\Users\\hbhardwaj4\\OneDrive - DXC Production\\Final Consolidated\\Profesional\\Professional_Notes\\Selenium\\downloads\\drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");

                             //                         FirefoxDriver driver = new FirefoxDriver();

                                           //options

                                           System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "null");

                                           driver = new FirefoxDriver();

                             }else if (browser.equals("Chrome")){

                                           driver = new ChromeDriver();

              }else if (browser.equals("IE")){

                             driver = new InternetExplorerDriver();

              }else if (browser.equals("Edge")){

                             driver = new EdgeDriver();

              }

              }

              public void navigate(){

                             System.out.println("Navigating to website"+envProp.getProperty(objectKey));

              }

             

              public void click(){

                             System.out.println("Clicking"+prop.getProperty(objectKey));

              }

              public void type(){

                             System.out.println("Typing in "+prop.getProperty(objectKey)+" . Data - "+data.get(dataKey));

                            

              }

              public void validateTitle(){

                             System.out.println("Validate title "+prop.getProperty(objectKey));

              }

}

 

Exception Error:

[TestNG] Running:

  C:\Users\hbhardwaj4\AppData\Local\Temp\testng-eclipse--675472751\testng-customsuite.xml

 

****LoginTest

Row number of the test is 1

Total Cols - 5

Total Rows 3

{Username=U1, ExpectedResult=LoginSuccess, Password=P1, Runmode=Y, Browser=Mozila}

-----------------------------

{Username=U2, ExpectedResult=LoginSuccess, Password=P2, Runmode=N, Browser=Chrome}

-----------------------------

{Username=U3, ExpectedResult=LoginFailure, Password=P3, Runmode=N, Browser=Edge}

-----------------------------

Rows 13

Opening Browser Mozila

java.lang.reflect.InvocationTargetException

      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 com.dhagrp.hybrid.driver.DriverScript.executeKeywords(DriverScript.java:59)

      at com.dhagrp.hybrid.suitea.LoginTest.loginTest(LoginTest.java:28)

      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:643)

      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)

      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)

      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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)

      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)

      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

      at com.google.common.base.Preconditions.checkState(Preconditions.java:750)

      at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)

      at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:44)

      at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:167)

      at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)

      at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:190)

      at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)

      at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)

      at com.dhagrp.hybrid.keywords.GenericKeywords.openBrowser(GenericKeywords.java:46)

      ... 30 more

Navigating to websitehttps://207.37.203.26/DHA_VAF_PIISF/Login.aspx

Validate title Home Page

Clicking//*[@id="txtEmail"]

Typing in //*[@id="txtEmail"] . Data - U1

Typing in //*[@id="txtPassword1"] . Data - P1

Clicking//*[@id="form1"]/div[3]/div[3]/div/div/div/div[1]/table/tbody/tr[4]/td[2]/input

Validating Login

PASSED: loginTest({Username=U1, ExpectedResult=LoginSuccess, Password=P1, Runmode=Y, Browser=Mozila})

PASSED: loginTest

SKIPPED: loginTest({Username=U2, ExpectedResult=LoginSuccess, Password=P2, Runmode=N, Browser=Chrome})

org.testng.SkipException: Runmode is set to No

      at com.dhagrp.hybrid.suitea.LoginTest.loginTest(LoginTest.java:25)

      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:643)

      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)

      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)

      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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)

      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)

      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

 

SKIPPED: loginTest({Username=U3, ExpectedResult=LoginFailure, Password=P3, Runmode=N, Browser=Edge})

org.testng.SkipException: Runmode is set to No

      at com.dhagrp.hybrid.suitea.LoginTest.loginTest(LoginTest.java:25)

      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:643)

      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)

      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)

      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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)

      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)

      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

 

 

===============================================

    Default test

    Tests run: 4, Failures: 0, Skips: 2

===============================================

 

 

===============================================

Default suite

Total tests run: 4, Failures: 0, Skips: 2

===============================================

 

[TestNG] Time taken by org.testng.reporters.XMLReporter@edf4efb: 124 ms

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6cd8737: 45 ms

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6aaa5eb0: 66 ms

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@71be98f5: 231 ms

[TestNG] Time taken by org.testng.reporters.jq.Main@2d6e8792: 213 ms

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 35 ms

 

D

Instructor

Demo Reply on 09/04/2019

The error is at GenericKeywords.java line number 46

H
Harshdeep Reply on 12/04/2019

Got the issue : As Path of the browser was not specified in environment variable. Put the path and sciprt is working now. Thanks.