method Inspector

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

method Inspector

rajeshraj536
This post has NOT been accepted by the mailing list yet.
Hi All,

I am using testng framework for writing my test cases.

I have written few selenium test cases in different class with different priority list in groups

Ex:
Class A
@Test(groups = "p1")
private void AtestCase(){
}

Class B
@Test(groups = "p2")
private void BtestCase(){
}

I have implemented method Inspector Interface(IMethodInterceptor) in a class and written a code given below.
P1 - Highest priority
P2 - Second Highest priority
P3 - lowest priority

Observed Issues:
1.) Method Inspector was able to return a priority list, testng started executing test method based on the priority list given. Before the instance(browser) of the first test method(AtestCase) is closed, testng called Method Inspector once again and second test method(BtestCase). This process continued untill last priority is reached  

I am not sure what went wrong, can some one help me to solve this problem and provide me code to slove the problem.

Note: I have used Aftermethod() to close browser in each class

I am using testng.xml file to execute the test scripts

<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="listenerPackage.listenerClassAll"/>
</listeners> 

public class methodInsc implements IMethodInterceptor {
       
        private List<IMethodInstance> allMethodsSorted = null;
        private List<IMethodInstance> afterMethodsSorted = new ArrayList<IMethodInstance>();
        private int inc = 0;
        private String[] arrayListPriority = {"p1","p2","p3","p4",null};

        @Override
        public List<IMethodInstance> intercept(List<IMethodInstance> methods,ITestContext arg1) {
                Map<Class<?>,List<IMethodInstance>> methodsByClass = new LinkedHashMap<Class<?>,List<IMethodInstance>>();
                for (IMethodInstance method : methods){
                        ITestNGMethod testngMethod = method.getMethod();
                        //System.out.println("Test Method Name "+testngMethod.getMethodName());
                        Class<?> testClass = testngMethod.getMethod().getDeclaringClass();
                        //System.out.println("Test test Method Class "+testClass);
                        add(methodsByClass, testClass, method);
                }
               
                     allMethodsSorted = new ArrayList<IMethodInstance>();
                     System.out.println("The size of method ClassByName   "+methodsByClass.size());
                for (Map.Entry<Class<?>,List<IMethodInstance>> entry : methodsByClass.entrySet()) {
                        System.out.println(entry.getKey()+"  "+entry.getValue());
                        List<IMethodInstance> sorted = sort(entry.getKey(), entry.getValue());
                        allMethodsSorted.addAll(sorted);
                }
               
                for(int pri = 0; pri < arrayListPriority.length; pri++)
                {
                        for( int i =0; i< allMethodsSorted.size(); i++)
                        {
                                 String[] setgroupxx = allMethodsSorted.get(i).getMethod().getGroups();
                                 for(int j = 0; j < setgroupxx.length; j++)
                                 {
                                         //System.out.println(setgroupxx[j]);
                                         if(setgroupxx[j].equalsIgnoreCase(arrayListPriority[pri]))
                                         {
                                                 afterMethodsSorted.add(inc, allMethodsSorted.get(i));
                                                 inc++;
                                         }
                                 }
                        }
                }
                                         
                for(int af = 0; af < afterMethodsSorted.size(); af++)
                {
                        System.out.println("Methods should executed in order " +afterMethodsSorted.get(af).getMethod().getMethodName());
                }
               
                return afterMethodsSorted;
        }

private void add(Map<Class<?>,List<IMethodInstance>>  methodsByClass, Class<?> testClass, IMethodInstance method) {
        List<IMethodInstance> li = methodsByClass.get(testClass);
        //System.out.println("The value of List  " +li);
        if (li == null)
        {
                li = new ArrayList<IMethodInstance>();
                methodsByClass.put(testClass, li);
        }
        li.add(method);
        //System.out.println("The size of li  " +li.size());
}

private List<IMethodInstance> sort(Class<?> clazz, List<IMethodInstance> methods) {
    List<IMethodInstance> sorted = new ArrayList<IMethodInstance>();
    List<IMethodInstance> temp = new ArrayList<IMethodInstance>(methods);
    Method[] allMethods = clazz.getDeclaredMethods();
    for (int i=0; i<allMethods.length; i++) {
        Method m = allMethods[i];
        //System.out.println("The methodName  "+m.getName());
        //check whether method is annotated with Test class
        if (!m.isAnnotationPresent(Test.class)) {
            continue;
        }
        for (Iterator<IMethodInstance> it = temp.iterator(); it.hasNext();) {
            IMethodInstance imi = it.next();
            //System.out.println(imi.getMethod().getMethodName());
            if (imi.getMethod().getMethodName().equalsIgnoreCase(m.getName())) {
                sorted.add(imi);  //adding to ArrayList
                it.remove();
            }
        }
    }
   
    // Add any that were somehow left over
    for (Iterator<IMethodInstance> it = temp.iterator(); it.hasNext();) {
        IMethodInstance imi = it.next();
        sorted.add(imi);
    }

    return sorted;
}