为什么我们将firefoxdriver实例分配给webdriver [英] why we assign firefoxdriver instance to webdriver
问题描述
我是Java或Selenium的新手。
I am novice to Java or Selenium.
我只需要帮助即可理解一个基本问题。
I just need help to understand one basic question.
为什么我们将firefoxdriver实例分配给WebDriver?
WebDriver driver = new FirefoxDriver()
Why we assign firefoxdriver instance to WebDriver? WebDriver driver=new FirefoxDriver()
我知道这是Java中的后期绑定,因为我们可以将IEDriver或其他一些实例分配给WebDriver at稍后的时间点。
I know that this is kind of Late binding in Java, because we can assign IEDriver or some other instance to WebDriver at later point of time.
问题1:但这适用于课程,对吗?
Question1: But this applices to classes, right?
问题2:WebDriver是一个接口,那么我们可以创建一个接口的对象实例吗?
Question2: WebDriver is an interface, then can we create an object instance of an interface?
推荐答案
WebDriver driver = new FirefoxDriver();
在上面的陈述中, WebDriver
是一个接口。接口包含已定义但未实现的空方法。只要不违反方法类型和签名,任何人都可以实现这些方法。因此,接口也称为契约,因为您可以根据需要使用接口,但不能更改它的定义方式。并且,因为它有空方法,你实际上不需要实例化它,因此你无法实例化它。
In the above statement, WebDriver
is an interface. An interface contains empty methods that have been defined but not implemented. These methods can be implemented by anyone as long as the method type and signatures are not violated. Therefore, an interface is also known as contract, because you can use an interface as you like but you cannot change the way it has been defined. And, since it has empty methods you won't actually need to instantiate it and so you cannot instantiate it.
FirefoxDriver
是专门为Firefox浏览器编写的类。它具有已实现的方法,可以实例化。它可以在接口 WebDriver
中定义的Firefox浏览器上执行所有功能(或方法)。
FirefoxDriver
is a class that has been written specifically for the Firefox browser. It has methods that are implemented and it can be instantiated. It can perform all functions (or methods) on the Firefox browser as defined in the interface WebDriver
.
所以在上面的陈述中,我们实际上是在告诉 FirefoxDriver
类,嘿,你可以自动化各种方法你想在Firefox浏览器上,但你需要坚持在 WebDriver
中定义的合同。所以我们声明一个类型为 WebDriver
的引用变量,然后用它来实例化 FirefoxDriver
,这意味着该对象(驱动程序) )类型为 WebDriver
但指向 FirefoxDriver
中所有数据和方法的内存分配(并且,如上所述) , FirefoxDriver
类已经在 WebDriver
中具有已实现的方法版本。所以一切都很好:))
So in the above statement, we are actually telling FirefoxDriver
class that "hey you can automate the various methods that you want on the Firefox browser but you need to stick to the contract defined in WebDriver
". So we declare a reference variable of type WebDriver
and then use it to instantiate FirefoxDriver
, which means that the object (driver) is of type WebDriver
but points to the memory allocation to all data and methods in FirefoxDriver
(and, as mentioned above, the FirefoxDriver
class already has the implemented version of methods in WebDriver
). So all good :)
通过使用这种技术,我们可以让测试人员轻松使用他或她喜欢的任何浏览器。例如,要自动化IE驱动程序,必须简单地编写一个语句,如
By using this technique, we have made it easy for the tester to use any browser of his or her liking. For example, to automate on IE driver, one will have to simply write a statement like
WebDriver driver = new IEDriver(); //where IEDriver is the class written for IE
这篇关于为什么我们将firefoxdriver实例分配给webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!