dimanche 27 mars 2022

using reflection method to call function in Selenium

given a TestNG test within Selenium

public boolean TestTabTagLog( Matters m, String logfile) {
    boolean passFail = false;

    if( m == null) {
        System.out.println("TestTagTabLog: m is null and should not be!");
    } else {
        System.out.println("TestTabTagLog: m.getPageName: " + m.getPageName());
        /* m does get here and is populated , great*/
    }

    WebElement divTopTabs = m.getDriver().findElement(By.id("tab-topTabs"));
    // --- this fails with an NoSuchElementException --- why? -- and more importantly, how to fix?

    /* more logic goes here */

    return passFail;
}

@Test()
public boolean runTest( Matters m, String testToRun) {
    boolean passFail = false;

    List<Boolean> results = new LinkedList<Boolean>();

    Matters m = new Matters();
    WebElement divTopTabs = m.getDriver().findElement(By.id("tab-topTabs"));
    // m and divTopTabs exist and are populated

    List<String> list_tabName = new LinkedList<String>();
    list_tabName.add("TagLog");

    for( String tabName : list_tabName) {
        String functName = "TestTab" + tabName;
        try {
            Class c = Class.forName("foo.fighters.Test");
            Object obj = c.newInstance();
            Class[] params = new Class[2];
            params[0] = Matters.class;
            params[1] = String.class;
            Method mth = c.getDeclaredMethod( functName, params);
            mth.setAccessible(true);
            Boolean result = (Boolean) mth.invoke( obj, m, "C:/User/Me/alogfile.txt");
            results.add( result);
        } catch ( ClassNotFoundException cnfe) {
            System.out.println("cnfe:" + cnfe.getMessage());
        } catch ( InstantiantionException | IllegalAccessException iae) {
            System.out.println("iae:" + iae.getMessage());
        } catch ( NoSuchMethodException | SecurityException nsme) {
            System.out.println("nsme:" + nsme.getMessage());
        } catch ( IllegalArgumentException | InvocationTargetException ite ) {
            System.out.println("ite:" + ite.getMessage());
        }
    }

    Boolean atLeastOneTestFailed = results.stream()filter(e -> e == false).findAny().isPresent();

    if( !atLeastOneTestFailed) {
        passFail = true;
    }

    return passFail;
}

I'm trying to dynamically call my functions by name - hoping to prevent coding a giant switch/case statement. Within the top function runTest() I can find m and use m to find divTopTabs. I can pass m to called function TestTabTagLog but within TestTabTagLog canNOT use m to find divTopTags. That's my problem.

I have tried passing in divTopTabs to TestTabTagLog as an argument but that doesn't work for some reason, maybe cuz it's a WebElement and that is a class inside openqa ie not a class I create within my project like Matters. But it that's true why can I pass in the String logfile ?

A little out of my depth here, could use some help/advice/constructive critism

TIA,

Still-learning Steve





Aucun commentaire:

Enregistrer un commentaire