jeudi 3 septembre 2015

How to invoke methods in a required / correct sequence via Java reflections

I am new to java and selenium and trying to execute methods (tests) in class A from class B by using java reflections. The methods are being called and are also being executed. But the issue is that the methods are not being executed in the sequence they are written in class A or in a required sequence.

For eg. class A has following methods in sequence:

1. Login
2. Go to Page 1 --> do some activity
3. Go to Page 2 --> do some activity
4. Go to Page 4 --> do some activity
5. Logout

When these are called via reflection in class B, it changes the sequence to:

4. Go to Page 4 --> do some activity
2. Go to Page 1 --> do some activity
5. Logout
1. Login
2. Go to Page 1 --> do some activity

Due to this, the method fails since first step is to login and since the user is not logged in, the Go to Page 4 --> do some activity cannot be preformed. 

How can I make the methods to be executed in the correct format. Is there are any naming format.


        enter code here
Class A:

    public void Login()  
        { 
            //Some action;
        }

        public void GoToHomepage() 
       {
          //Some action;
        }

        public void GotoPageOne(){
            //Some action;
        }


        public void GotoPageTow(){
            //Some action;
        }

        public void GotoPageThree(){
            //Some action;
        }

        public void GotoPagefour(){
            //Some action
        }

    Class B (calling the methods from class A via reflections)

    public static void main (String args[]){
    public static Method[] method;
    ClassA wa = new ClassA(driver);
            method=wa.getClass().getDeclaredMethods();
             for (int i=0;i<=method.length-1;i++){
             System.out.println(method[i].getName());
             method[i].invoke(wa);

    The methods are printing in the same sequence in which they are executed.

    Kindly suggest and also let me know i am doing it incorrectly.

    Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire