How to launch Chrome browser in Selenium

Since Selenium alone is not capable of launching the Chrome browser, we need to take help of third party executable driver which will launch the browser for selenium. Selenium do have default methods to initiate Chrome browser and most of other browsers, but executable driver is necessary.
Let’s take a look on how to launch Chrome browser. The method available for launching chrome browser is ChromeDriver(); We need to download ChromeDriver.exe which will help Selenium to launch Chrome. You can get latest version of ChromeDriver.exe from ChromeDriver .
Using below simple code snippet one can launch the Chrome browser.
System.setProperty(“webdriver.chrome.driver”, “D:\\New folder\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
Code Explanation:
In the first part of the code we are setting property as path for the Chrome driver.exe which is downloaded in the local machine. And in second line default method ChromeDriver() will use this property to launch browser. If we do not set any property, then we will get exception stating :
Capture.PNG
Browser Capability:
Browser capability is way of setting browser profile so that execution can happen in the way we want it. For example, if any unexpected window pop up comes up during execution, we will end up having error and test case got failed. To avoid such conditions or to avoid SSL certification errors, capability needs to be set for browser. For chrome browser we can mention below simple capability using ChromeOptions.
System.setProperty(“webdriver.chrome.driver”, “D:\\New folder\\chromedriver.exe”);
ChromeOptions options = new ChromeOptions(); options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT); options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(options);
In above code snippet, UnexpectedAlertBehaviour.ACCEPT will accept any alert popping up abruptly and Page loading strategy will define if whole HTML dome and other supporting images needs to be downloaded.

Comments

Popular posts from this blog

API Concepts