I want to override the method Selenium::WebDriver.for
. This is what I tried:
module SeleniumWebDriverExtension
def self.for(browser, *args)
if browser != :phantomjs
super(browser, *args)
else
options = {
"phantomjs.cli.args" => ["--ssl-protocol=tlsv1"]
}
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs(options)
super(browser, desired_capabilities: capabilities)
end
end
end
Selenium::WebDriver.prepend(SeleniumWebDriverExtension)
But I got error when Selenium::Webdriver.for(:phantomjs)
is called.
NoMethodError: super: no superclass method `for' for Selenium::WebDriver::Driver:Class
How can I call the original method from the overriding method?
An override method is a new implementation of a member that is inherited from a base class. The overridden base method must be virtual, abstract, or override. Here the base class is inherited in the derived class and the method gfg() which has the same signature in both the classes, is overridden.
quick command search : ctrl + shift + A Type constructor to override all of them quickly :D.
In C#, class methods, indexers, properties and events can all be overridden. Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
Using a method that overrides a class, you can "copy" another class, avoiding duplication of code, and at the same time improve or customize part of it. Thus, overriding a method is part of the inheritance mechanism. In Python, overriding a method is done by simply defining it in a child method class with the same method name in the parent class.
Overriding Super Class Methods in Java | Inheritance in Java 9.3 Overriding Superclass Methods In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
How to “properly” override a base class method? 1 Call base.Method (), and then provide my implementation. 2 Provide my implementation and then call base.Method () 3 Just provide my implementation. More ...
Method overloading vs. method overriding If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
module SeleniumWebDriverExtension
def for(browser, *args)
...
end
end
Selenium::WebDriver.singleton_class.prepend(SeleniumWebDriverExtension)
When to you use self
inside a module like this:
def self.for(browser, *args)
end
it is declared as a module function
, not an instance method on the class that will include this module. What this means is that won't appear on included classes when the module is mixed into another class.
It is similar to writing:
def SeleniumWebDriverExtension::for
end
So if you want to call super
from within the module, declare it as a simple instance method
like the accepted answer has suggested. Just wanted to clear you on the reasoning behind this.
Btw SeleniumWebDriverExtension.ancestors
to be clear on the inheritance hierarchy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With