Open sajagkarki opened 2 years ago
When we create the framework, we did not create multiple polymorphism. We used it as dynamic polymorphism. Our focus was always to achieve dynamic polymorphism. Driver.findelement
Run-Time Polymorphism: Whenever an object is bound with the functionality at run time, this is known as runtime polymorphism. The runtime polymorphism can be achieved by method overriding. It is acheived by if else statement or switch statement and with the object initialization. In Selenium we use runtime polymorphism concept. It shows that we can execute same script on any browser.
To achieve runtime polymorphism following steps are mandatory-
1.Inheritance (firefoxdriver is inheriting from webdriver)
2.Method overriding (All the methods present in webdriver is overrided, As it is an interface)
Upcasting (casting the object to a supertype)
webdriver driver=new FirefoxDriver();
Example:-1
Program for runtime polymorphism
package webdrivercommands;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver;
public class Demo { //Method-1 public static void testmethod(WebDriver driver) throws InterruptedException {
driver.get("https://www.google.com"); System.out.println(driver.getTitle());
driver.navigate().to("https://www.google.com"); System.out.println(driver.getTitle());
driver.navigate().back(); Thread.sleep(2000);
driver.navigate().forward(); Thread.sleep(2000);
driver.navigate().refresh(); Thread.sleep(2000);
driver.quit(); }
public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver","path of geckodriver.exe"); WebDriver driver=new FirefoxDriver();
testmethod(driver);
System.setProperty("webdriver.chrome.driver"," path of chromedriver.exe");
testmethod(new ChromeDriver());//Auto upcast }
}
===================
//Class1
class ParentClass{
public void status(){
System.out.println("Parent's Method");
}
}
//Class2 class ChildClass extends ParentClass { public void status() { System.out.println("Child's Method"); }
public static void main(String args[]) { // Creating objects ParentClass parent = new ParentClass(); ParentClass parent_1 = new ChildClass(); ChildClass child = new ChildClass(); parent.status(); // This will print Parent's status method child.status(); // This will print child's status method parent_1.status(); // This will print child's status method } } Runtime Polymorphism- Define in runtime which object you have to create. Driver is the object. We achieve it with if else or switch statement. Examples if ("openbrowser".Equals(testCases.Action, StringComparison.OrdinalIgnoreCase)) { Driver = new ChromeDriver();
}
else if ("openbrowserIE".Equals(testCases.Action, StringComparison.OrdinalIgnoreCase))
{
Driver = new EdgeDriver();
}
else if ("openbrowserFirefox".Equals(testCases.Action, StringComparison.OrdinalIgnoreCase))
{
Driver = new FirefoxDriver();
}
Example 2 Elements
if ("id".Equals(testCases.LocatorType, StringComparison.OrdinalIgnoreCase)) { IWebElement webElement = Driver.FindElement(By.Id(testCases.LocatorTypeValue));
return webElement;
}
if ("xpath".Equals(testCases.LocatorType, StringComparison.OrdinalIgnoreCase))
{
IWebElement webElement = Driver.FindElement(By.XPath(testCases.LocatorTypeValue));
return webElement;
}
How did you apply OOP concept in your automation project?