Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
44.5k views
by
retagged by
Can you please provide me real-time examples of methods we have used as Java Polymorphism we have used in Selenium webdriver. I want the only practical example and not the definition of Polymorphism like Overloading and Overriding,

I know what is Overloading and Overriding but I need the real-time example which is used in Selenium webdriver frameworks.

Thanks in advance...

2 Answers

0 like 0 dislike
by Expert (572 points)
selected by
 
Best answer

Overloading happens when 2 methods in the same class have the same name but different parameters.

Overriding means having 2 methods with the same name and same parameters, one being in a parent class and the other in a child class that inherits from the parent class.

Method overloading example

You want to have your own Listbox class to interact with dropdown lists.

The Listbox class is just a wrapper around the Select class.

You want it to have simpler method names for selecting list options:

public class Listbox {
 
  Select list;
 
  public Listbox(Select list) {
    this.list = list;
  }
 
  public void select(int i) {
    this.list.selectByIndex(i);
  }
 
  public void select(String text) {
    this.list.selectByVisibleText(text);
  }
 
  public void deSelect(int i) {
    this.list.deselectByIndex(i);
  }
 
  public void deSelect(String text) {
    this.list.deselectByVisibleText(text);
  }
 
  //other methods
 
}

The class has 2 select methods, one with an integer parameter, the other with a string parameter.

The 2 methods have the same name and both have no return type.

This is an example of overloading.

The difference between the 2 methods is being made by the parameter type.

The same applies to the deSelect() methods.


Method overriding example

Method overriding happens when you have child classes inheriting from parent classes.

For example, you may have a base page class with methods that work for any page:

public abstract class BasePage {
 
  String name;
  String url;
  String title;
  WebDriver driver;
 
  public BasePage(String name, String title, String url, WebDriver drv) {
    this.name = name;
    this.title = title;
    this.url = url;
    this.driver = driver;
  }
 
  protected String title() {
    return this.title;
  }
 
  protected String url() {
    return this.url;
  }
 
  protected String name() {
    return this.name;
  }
 
  protected WebDriver driver() {
    return this.driver;
  }
 
  protected boolean isDisplayed() {
    return this.title.equalsIgnoreCase(this.driver.getTitle()) &&
           this.url.equalsIgnoreCase(this.driver.getCurrentUrl());
  }
}

isDisplayed() method works by checking that the title and URL of the page are equal to the title and URL of the current page.

HomePage class inherits from BasePage class:

public class HomePage extends BasePage { 

  public HomePage(WebDriver driver) {

    super(“HomePage”, 

          “Home Page Title”,

          “http://www.abcdef.com/”, 

          driver);

  }

  //other methods

}

src: Quora Answer

0 like 0 dislike
by

@SunilBhaskar

Overriding in Selenium is best depicted by "get" and "navigate" methods for different browser.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...