Class inheritance and class casting

I'm writing some test scripts in Selenium that need to apply across multiple browser types. My idea is to create a base browser object with multiple sub-classes specific to each browser type, e.g. Chrome, Firefox, IE, etc. Each subclass contains the browser-specific details, but common functional definitions hide the implementation details.

I'm trying to avoid a single class loaded down with if-then-else structures to cover all the driver-specific details, but I'm having trouble figuring out how to make it work.

Example:

public class Browser {
    public Browser() {
        //does stuff...
    }

    public void launch(String url) {
        //launches browser from WebDriver object
    }

    public void close () {
        //closes browser from WebDriver object
    }
}

public class Browser_Chrome() extends Browser {
    public Browser_Chrome() {
        super();
        // WebDriver specifics...
    }
}

public class Browser_Firefox() extends Browser {
    public Browser_Firefox() {
        super();
        // WebDriver specifics...
    }
}


public class webApp extends Browser {
    public webAbb() {
        super();
    }
}

I only want to have a single test script that doesn't care what browser is currently running. I'm fairly new to Java, so perhaps there is a better way to do this? I'm open to suggestions.

Answers


As i understood you want your web app to communicate with browser class regardless of its type (chrome,firefox,ie). if that's the issue then the Browser_Chrome and the Browser_Firefox should extend the Browser class and override the implementation of the lunch and close functions in each child class if the Browser class.

For the wabapp should have a variable of type Browser and call lunch or close if needed.

like the following

public abstract class Browser { // make it abstract because you cant make a browser that's not ch
    public Browser() {
        //does stuff...
    }

    public void launch(String url) {
        //launches browser from WebDriver object
    }

    public void close () {
        //closes browser from WebDriver object
    }
}

public class Browser_Chrome extends Browser {
    public Browser_Chrome() {
        super();
        // WebDriver specifics...
    }
}

public class Browser_Firefox extends Browser {
    public Browser_Firefox() {
        super();
        // WebDriver specifics...
    }
}


public class webApp {
    private Browser borwser; 
    public webAbb(Browser borwser) {
        super();
        this.borwser= borwser;
    }

    public void lunchBrowser(){
       borwser.lunch();
    }

}

You could write an abstract class with methods that you want each browser to do which each browser would overwrite for their own specific use.

So maybe something like this:

public abstract Browser { 
    abstract void launch(String url);
    abstract void close();
}

public class Chrome extends Browser {
    public Chrome() {
        ...
    }        

    @override
    public void launch(String url) {
        ...
    }
    @override
    public void close() {
        ...
    }
    //then add class specific methods to make those 2 methods function as you like.
}

Need Your Help

Image Bouncing while duing interface orientation

iphone ios objective-c image uiimageview

I am having paging in my application. Each page having Image. during the orientation i am changing the frame according to the landscape and portrait orientation. but image is bouncing. It should not

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.