Selenium WebDriver - Google Chrome Browser
Selenium WebDriver - Google Chrome Browser
In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser.
To implement the WebDriver protocol with the help of an executable file, which is called as the ChromeDriver.exe.
The above executable file starts the server in our system that is responsible for running our test scripts in the Selenium WebDriver.
And we will create one sample test case in the same test suite (new_test), which we create in the previous tutorials.
Step1:
- Firstly, right-click on the src folder and create a new Class File from New ? Class.
- And give our Class name as test_chrome and click onthe Finish button.
Step2:
Now, go to the selenium community and download the Chrome driver server. In the Selenium community, we will find the third party driver division. And click on the link beside the Google Chrome driver.
Or
Directly open the below link, it will navigate you to the download page of Chrome driver in your browser.
https://sites.google.com/a/chromium.org/chromedriver/downloads
Step3:
If you click on the above link, it will redirect you to the Current releases page where you can download the operating system, which you are working currently on.
- Click on the chromedriver_win32.zip link and download it for the Windows platform.
- Download the zip file into your local system, and unzip the folder, it will generate chromedriver.exe file automatically.
Step4:
Run the server before launching the Chrome browser, with the help of System.property,
Syntax:
System.SetProperty(“key”, ”value”);
To set the system property for the Chrome driver in real-time where
Key = “webdriver.chrome.driver”
And the path of our ChromeDriver.exe file to invoke the ChromeDriver class.
Value=”C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe"
Note: Make sure that, while copying a path for the chroedriver.exe file, it should always be in a double slash (//).
Sample code for system property:
// System Property for Chrome Driver System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe"); // create an object for ChromeDriver class WebDriver driver=new ChromeDriver();
Let us see one example where we will try to automate the following scenarios in the Google Chrome browser.
Steps | Action | Method Used | Input | Excepted Result |
1. | Open the Google Chrome Browser. | System.setProperty() | The Google Chrome browser must be opened. | |
2. | Navigate to the Given URL of the website. | get() | https://www.tutorialandexample.com | The home page window must be displayed. |
3. | Maximize the browser. | The browser window should be maximized. | ||
4. | Identify the Angular 8 tutorials from the latest tutorial section. | The angular 8 tutorials should be identified in the latest tutorial section. | ||
5. | Close the Browser. | close() | The browser should beterminated. |
Here is the sample code for the above example.
package testpackage; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class test_chrome { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_wi32\\chromedriver.exe"); // create driver object for Chrome browser WebDriver driver=new ChromeDriver(); // Launch the Website driver.navigate().to("https://www.tutorialandexample.com"); //Maximize the browser window driver.manage().window().maximize(); System.out.println("window size is maximized"); // Click on the angular 8 tutorial link driver.findElement(By.xpath("//a[contains(text(),'Angular 8 Tutorial')]")).click(); System.out.println("angular 8 tutorial page is opened"); //close the browser driver.close(); System.out.println("existing browser is closed"); } }
Now, right-click on the Eclipse code and select Run as ? Java Application.
The output of the above test script would be displayed in the Google Chrome browser.
The output of all print command of the above test script would be displayed in the Eclipse console window.
The output of all print command of the above test script would be displayed in the Eclipse console window.