What is the difference between ChromeDriver and WebDriver in selenium?

百般思念 提交于 2019-11-26 09:14:21

问题


If we create :

ChromeDriver driver=new ChromeDriver();

chrome driver methods will be executed.

And if we create :

WebDriver driver=new ChromeDriver();

again ChromeDriver methods are executed [as per method overriding].

Then why we write latter one only while executing?


回答1:


ChromeDriver driver = new ChromeDriver();

If you use ChromeDriver driver = new ChromeDriver(); the ChromeDriver instance which will get created through that we will be only able to invoke and act on the methods implemented by ChromeDriver and supported by Chrome Browser only. To act with other browsers we have to specifically create individual objects as below :

  • FirefoxDriver driver = new FirefoxDriver();
  • InternetExplorerDriver driver = new InternetExplorerDriver();

WebDriver Interface

From Selenium perspective, the WebDriver Interface is similar like a agreement which the 3rd party Browser Vendors like Mozilla, Chrome, Internet Explorer, Safari, etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available Browsers without any change.


Why WebDriver driver = new ChromeDriver();

Through WebDriver driver = new ChromeDriver(); we are creating an instance of the WebDriver interface and casting it to ChromeDriver Class. All the Browser Drivers like FirefoxDriver, ChromeDriver, InternetExplorerDriver, PhantomJSDriver, SafariDriver etc implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the Browser Drivers extends RemoteWebDriver). So if we use WebDriver driver, then we can use the already initialized driver (as common object variable) for all browsers we want to automate e.g. Mozilla, Chrome, InternetExplorer, PhantomJS, Safari.

WebDriver driver = new FirefoxDriver();
driver = new ChromeDriver();
driver = new FirefoxDriver();
driver = new SafariDriver();

Trivia WebDriver Interface

As per current scenario, we have to instantiate the implementations of WebDriver Interface directly. As per the current practice we write our Automated Test Script against this interface so that in future we may swap in a more fully featured Browser when there is a requirement for one.



来源:https://stackoverflow.com/questions/48079120/what-is-the-difference-between-chromedriver-and-webdriver-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!