premganz / SeleniumPageObjects

An implementation of the Page Object Pattern in Selenium along with a bunch of other features like KeyWords for oft used Webdriver calls, driver lifecycle management, effective logging and Test Script Templates
Other
2 stars 1 forks source link

Fixes: Init method to be consistently used #40

Closed premganz closed 8 years ago

premganz commented 8 years ago

The major idea of configuration of the framework is through the method init() inside the extenisibleService interface. A lot of services implement this, on the sides of the runner, the command and the injected library. The components of the injected libraries are more often extensible services.

This is an effective alternate to use build-time injection using Spring or other framework in the light that such framework themselves are now moving on to annotation based setups or even pure java configuration. The use of the init() method is the configuration setup of each service and contains the code therefor, including the assignment of its protected members which themselves can be recursively extensible. The place where all these services are initialised is the ServiceHubwhich calls on init() methods progressively and initialises the plugins (which is practically all the services used in the framework). Then while invocation through the service hub all the components are suitably initialized and available.

As for the configuration of the test command and the runner(including the runstrategy) these are also available through the extensible service interface however the initialization (recursively if needed) happens at the BasicLauncher which initilizes the runner, which in turn initializes the runstrategy and then the testcommand and its components.

The init() method usually calls the super.init() as the first line in order to extend a parent service and it adds the modifying code to the end of it. For those functionalities where some component needs to be added in a service so that it prevents a default initialization(after null checking the explicit initialization of the component somewhere down in the heirarchY) happening in the parent class, then the code should run before the super.init() call.

premganz commented 8 years ago

Example for the use could be

@Override
    public void init() {
        SessionContext.appConfig.WEBDRIVER_TIMEOUT=360;
        kw.setContentProvider(new Lib_Content_xyz()); //Because the content provider is initalized to default in super class calls.
        super.init();   //super call
        kw.getNavContainer().getDefaulModel().getFactory().removeValidator("(.*)"); //post initialziation manipulation

    }

Fixed in 1.4.8

premganz commented 8 years ago

Another example from an extension of JunitScript (A testCommand)

@Override
    public  void init() {
        ApplicationNavContainerImpl navContainer = new ApplicationNavContainerAUT(){
            @Override
            public void init() {
                super.init();
                if(SessionContext.testEnv.equals("UAT")){
                    getDefaulModel().changePageState("Home", "AT:url="+SessionContext.appConfig.URL_AT);                    
                }else{
                    getDefaulModel().changePageState("Home", "Regular:");       
                    getDefaulModel().getFactory().addValidator("(.*)", new PageValidator1());
                    getDefaulModel().getFactory().addValidator("Home", null);
                }

            }
        };
            kw.setNavContainer(navContainer); //This is default initialized after null check in ServiceHub

        super.init();//Defaults do not have necessary pre conditions so post init super class

    }