2017-08-10 3 views
1

Ich versuche zu verstehen, wie Reflexion funktioniert, kann ich nicht herausfinden, das Problem. In der zweiten for-Schleife method.length nicht Methode aus einem anderen Programm abrufen.Java-Reflexion in Selen

Es folgt der Code:

public class DriverScript 
{ 

    public static ActionKeywords actionKeywords; 
    public static String sActionKeyword; 
    public static Method method[]; 

    public DriverScript() throws NoSuchMethodException, SecurityException 
    { 
     actionKeywords = new ActionKeywords(); 
     method = actionKeywords.getClass().getMethods(); 
    } 

    public static void main(String[] args) throws Exception 
    { 
     System.out.println(method); 
     String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx"; 
     ExcelUtils.setExcelFile(sPath, "Test Steps"); 

     for (int iRow = 1; iRow <= 9; iRow++) 
     { 
      sActionKeyword = ExcelUtils.getCellData(iRow, 3); 
      execute_Actions(); 
     } 
    } 

    private static void execute_Actions() throws Exception 
    { 
     for (int i = 0; i < 11; i++) 
     { 
      if (method[i].getName().equals(sActionKeyword)) 
      { 
       method[i].invoke(actionKeywords); 
       break; 
      } 
     } 
    } 
} 
+0

https://stackoverflow.com/questions/29250410/get-methods-using-java-reflection-class ** diese beziehen ** – selvakumar

+0

nicht fett sollte da sein. –

Antwort

0

Sie den Konstruktor für DriverScript, so dass die Felder nicht initialisiert nicht aufrufen.

Versuchen Sie, Ihre Methoden von statisch auf nicht statisch zu ändern und reparieren Sie von dort aus.

+0

Ich hatte den Konstruktor DriverScript aufgerufen. method = actionKeywords.getClass(). getMethods(); zeigt Null-Wert .. – selvakumar

+0

Nun, ich habe keine Ahnung, welche Methoden auf der ActionKeywords-Klasse verfügbar sind, weil Sie diesen Code nicht veröffentlicht haben. Ich habe das Problem behoben, dass Sie veröffentlicht, bitte als akzeptiert –

+0

öffentlichen statischen WebDriver-Treiber, öffentliche statische void openBrowser() meine eine der Methode – selvakumar

0
public class Test2 { 
     //Reflection in Keyword driven Framework 
    /** 
    * @authors Venkadesh,Selvakumar work name : Test 
    **/ 
    static Class Class1 = ActionKeywords.class; 
    static ActionKeywords Keywords = new ActionKeywords(); 
    public static void main(String[] args) throws Exception { 

     String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx"; 
     ExcelUtils.setExcelFile(sPath, "Test Steps"); 
     Method[] methods = Class1.getDeclaredMethods(); 

     for (int i = 1; i <= 9; i++) { 
      // This to get the value of column Action Keyword from the excel 
      String sActionKeyword = ExcelUtils.getCellData(i, 3); 
      for (int j = 1; j <= 9; j++) { 
       if (methods[j].getName().equals(sActionKeyword)) { 
        try { 

         methods[j].invoke(Keywords); 
        } catch (Exception e) { 
        } 

        System.out.println(methods[j]); 
       } 

      } 

     } 
    } 
} 

Versuch mit dieser funktioniert es