Java 为什么我们将 firefoxdriver 实例分配给 webdriver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25757034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:04:15  来源:igfitidea点击:

why we assign firefoxdriver instance to webdriver

javaseleniumselenium-webdriver

提问by Uday

I am novice to Java or Selenium.

我是 Java 或 Selenium 的新手。

I just need help to understand one basic question.

我只需要帮助来理解一个基本问题。

Why we assign firefoxdriver instance to WebDriver? WebDriver driver=new FirefoxDriver()

为什么我们将 firefoxdriver 实例分配给 WebDriver?WebDriver 驱动程序=新的 FirefoxDriver()

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.

我知道这是 Java 中的一种后期绑定,因为我们可以稍后将 IEDriver 或其他一些实例分配给 WebDriver。

Question1: But this applices to classes, right?

问题 1:但这适用于课程,对吗?

Question2: WebDriver is an interface, then can we create an object instance of an interface?

问题2:WebDriver是一个接口,那么我们可以创建一个接口的对象实例吗?

采纳答案by Imran Jah

WebDriver driver = new FirefoxDriver();

In the above statement, WebDriveris 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.

在上面的语句中,WebDriver是一个接口。接口包含已定义但未实现的空方法。只要不违反方法类型和签名,任何人都可以实现这些方法。因此,接口也称为契约,因为您可以随意使用接口,但不能更改它的定义方式。而且,由于它有空方法,您实际上不需要实例化它,因此您无法实例化它。

FirefoxDriveris 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 浏览器编写的类。它具有已实现的方法并且可以实例化。它可以在 Firefox 浏览器上执行界面中定义的所有功能(或方法)WebDriver

So in the above statement, we are actually telling FirefoxDriverclass 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 WebDriverand then use it to instantiate FirefoxDriver, which means that the object (driver) is of type WebDriverbut points to the memory allocation to all data and methods in FirefoxDriver(and, as mentioned above, the FirefoxDriverclass already has the implemented version of methods in WebDriver). So all good :)

所以在上面的语句中,我们实际上是在告诉FirefoxDriverclass,“嘿,你可以在 Firefox 浏览器上自动化你想要的各种方法,但你需要坚持WebDriver”中定义的契约。所以我们声明了一个类型的引用变量,WebDriver然后用它来实例化FirefoxDriver,这意味着对象(驱动程序)是类型的,WebDriver但是指向所有数据和方法的内存分配FirefoxDriver(并且,如上所述,FirefoxDriver该类已经具有中方法的实现版本WebDriver)。所以一切都很好:)

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

通过使用这种技术,我们使测试人员可以轻松使用他或她喜欢的任何浏览器。例如,要自动化 IE 驱动程序,您必须简单地编写如下语句

WebDriver driver = new IEDriver(); //where IEDriver is the class written for IE

回答by Elliott Frisch

I know that this is kind of Late binding in Java,

我知道这是 Java 中的一种后期绑定,

No. This is an example of compile time binding. But yes, it's also an example of programming to the WebDriverinterface.

不。这是编译时绑定的示例。但是是的,它也是WebDriver接口编程的一个例子。

Question1: But this applies [sic] to classes, right?

问题 1:但这适用于 [原文如此] 类,对吗?

It could (conceivably) be an interface that extends WebDriver.

它可以(可以想象)是一个扩展WebDriver.

Question2: WebDriver is an interface, then can we create an object instance of an interface?

问题2:WebDriver是一个接口,那么我们可以创建一个接口的对象实例吗?

Yes, you can create concrete instances that implement an interface. In fact, to use any interface there must be at least one concrete implementation.

是的,您可以创建实现接口的具体实例。事实上,要使用任何接口,必须至少有一个具体的实现。

回答by Shailesh Modi

WebDriveris an interface and FirefoxDriveris a class which implementsthis WebDrivercontract. See Selenium doc enter link description here

WebDriver是一个接口,FirefoxDriver是一个实现这个WebDriver契约的类。请参阅 Selenium 文档, 在此处输入链接描述

Now we can create a reference variable of an interface but we can't instantiate any interfacesince it is just a contract to be implemented.But we can assign the instance of a class (In this case FirefoxDriver ) to its Parents(WebDriver).

现在我们可以创建一个接口的引用变量,但我们不能实例化任何接口,因为它只是一个要实现的契约。但是我们可以将一个类的实例(在本例中为 FirefoxDriver )分配给它的Parents(WebDriver)。

We can assign child classes to its parents(It may be a classor an interfaceto which it extends/implements).

我们可以将子类分配给它的父类(它可能是一个或它扩展/实现的接口)。

Now The reason behind doing WebDriver driver=new FirefoxDriver() is just to create an abstractionto the client(Java Program) because you can use Any Driver class according to browser.

现在做 WebDriver driver=new FirefoxDriver() 的原因只是为了创建对客户端(Java 程序)的抽象,因为您可以根据浏览器使用 Any Driver 类。

回答by Saurabh Shukla

Webdriver is an interface, not a class. We create a Webdriver reference driver and assign it to an object of class FirefoxDriver. To perform the testing on Firefox, make an object of class FirefoxDriver. Likewise, to test on chrome, use Chromedriver class' object and assign it to Webdriver. Webdriver is an interface which is implemented by both FirefoxDriver class and ChromeDriver class(and classes for other browsers like IE, Safari). Objects of only those classes can be assigned to an interface reference which implement that interface(which in this case is Webdriver interface)

Webdriver 是一个接口,而不是一个类。我们创建一个 Webdriver 引用驱动程序并将其分配给 FirefoxDriver 类的对象。要在 Firefox 上执行测试,请创建一个 FirefoxDriver 类的对象。同样,要在 chrome 上进行测试,请使用 Chromedriver 类的对象并将其分配给 Webdriver。Webdriver 是一个由 FirefoxDriver 类和 ChromeDriver 类(以及其他浏览器如 IE、Safari 的类)实现的接口。只能将那些类的对象分配给实现该接口的接口引用(在本例中为 Webdriver 接口)

回答by john_chinnam

It will make use of Interface Concept.See the below code.

它将利用接口概念。见下面的代码。

 FirefoxDriver  f = new FirefoxDriver();
 ChromeDriver c = new ChromeDriver();

To load a page using chrome,

要使用 chrome 加载页面,

c.get("google.com");

c.get("google.com");

If I want to use same call with firefox, I need to comment the above line and need to write a new line like this

如果我想对 Firefox 使用相同的调用,我需要注释上面的行并需要像这样写一个新行

//c.get("google.com");
f.get("google.com");

This will go on increasing , ending up with specific code for each instance of driver. But if I assign the the driver instance to WebDriver interface variable, I can write single set of code which will work for all drivers.

这将继续增加,最终为每个驱动程序实例提供特定代码。但是如果我将驱动程序实例分配给 WebDriver 接口变量,我可以编写一组适用于所有驱动程序的代码。

 WebDriver d;
    d  = FireFoxDriver();
    //just replace the above line with d = ChromeDriver() or InternetExplorer();
    d.get("google.com");
    d.getTitle();
    d.close();

回答by Moin

The Answer is simple,

答案很简单,

WebDriver is an interface which has a common behavior and we upcast so that the same behavior can be used across the Classes. Example:

WebDriver 是一个接口,它有一个共同的行为,我们向上转换以便可以跨类使用相同的行为。例子:

Interface: Consider WebDriver has the behavior of Switch

接口:考虑 WebDriver 具有 Switch 的行为

public interface WebDriver

公共接口 WebDriver

{

{

void on();

void off();

int voltage=220;;

}

}

Class1: Consider this as ChromeDriver class

Class1:将其视为 ChromeDriver 类

public class ChromeDriver implements WebDriver {

公共类 ChromeDriver 实现了 WebDriver {

@Override
public void on() {
    System.out.println("ChromeDriver On");

}

@Override
public void off() {
System.out.println("ChromeDriver Off");

}

} Class 2: Consider this class as FireFoxDriver class

类 2:将此类视为 FireFoxDriver 类

public class FireFoxDriver implements WebDriver {

公共类 FireFoxDriver 实现了 WebDriver {

@Override
public void on() {
    System.out.println("FireFoxDriver On");

}

@Override
public void off() {
System.out.println("FireFoxDriver Off");

}

}

}

Now Consider Runner Class:

现在考虑Runner类:

//Here to change the implementation we just need to change the object
    //Whether you need to access Mozilla or Chrome

public class Runner {

公共类跑步者{

 Webdriver driver = new ChromeDriver();//new FireFoxDriver();
 driver.on();
 driver.off();``

}

}

回答by lakshmireddy

When creating a new instance of browser new ChromeBrowser();ChromeBrowser(ChromeDriver.exe) is running on local machine and will die with the instance. So the browser restarts with each instance creation. To avoid restart of browser multiple times, Webdriver takes responsibility by referring Browser instance and controls browser not to be restarted for each instance creation.

创建浏览器的新实例时,new ChromeBrowser();ChromeBrowser(ChromeDriver.exe) 正在本地计算机上运行,​​并且会随着实例而消亡。所以浏览器会随着每个实例的创建而重新启动。为避免浏览器多次重启,Webdriver 通过引用 Browser 实例负责,并控制浏览器不会在每次创建实例时重启。

WebDriver driver = new ChromeDriver();

From the documentation;

来自文档;

A WebDriver implementation that controls a Chrome browser running on the local machine. This class is provided as a convenience for easily testing the Chrome browser. The control server which each instance communicates with will live and die with the instance. To avoid unnecessarily restarting the ChromeDriver server with each instance, use a RemoteWebDriver coupled with the desired ChromeDriverService, which is managed separately.

控制在本地计算机上运行的 Chrome 浏览器的 WebDriver 实现。提供这个类是为了方便测试 Chrome 浏览器。每个实例与之通信的控制服务器将与实例一起生存和消亡。为避免在每个实例中不必要地重新启动 ChromeDriver 服务器,请使用 RemoteWebDriver 与所需的 ChromeDriverService 结合使用,该服务是单独管理的。