Instantiating a WebDriver – Chrome

There are many available (Chrome, Safari, IE, Edge, Firefox, Opera, and so on) to navigate the internet, and each of them is built with its own features, specific approaches, and technologies. This means that a web application might behave slightly different on Google Chrome than it does on Internet Explorer. With this in mind, the WebDriver class can be instantiated according to the browser that we want to perform the test on.

In Environment Configuration, from the previous chapter, you learned how to prepare browser drivers. Once you've prepared the browser drivers, you can use any browser with any given automation script. The WebDriver class provides constructors for each browser. As we will only use Chrome throughout this book, we will focus on ChromeDriver. We can instantiate a WebDriver variable for use on Chrome as follows:

WebDriver driver = new ChromeDriver();

If you did not set the ChromeDriver on a system path (covered in the third section of this chapter), you will also have to let ChromeDriver know the location of your Chrome for every automation script that you write.

If you have set your ChromeDriver to the system path, you can skip the following step:

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/location");
WebDriver driver = new ChromeDriver(options);

You can also use ChromeOptions to start Chrome with a Chrome extension, as follows:

ChromeOptions options = new ChromeOptions();
options.addExtension("example.crx")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.63.87