In This Section, We will learn how to Implement Your First Selenium Script Using Java and Eclipse IDE. Complete automation environment setup will be required to execute the below example and run the tools.
Prerequisites: –
• Download and Install Java JDK
• Download and Install Eclipse IDE
• Download Selenium Jar and Web Driver Based on The Browser of your choice.
End to End automation with Selenium WebDriver in Java
In This example, let’s automate an end-to-end test case on a chrome browser.
Test Scenario:
- Open the browser.
- Load the URL “google.com”.
- And Verify the page title as “Google”.
Create a Java project in Eclipse
• Open Eclipse IDE and click on file >> new >> class
• Enter Class Name “First Script” and Click Finish
Under this project create Java main class “First_script .java”
package project1; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class First_script { public static void main(String[]args) // Java Main Class { System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");// initialize Chrome Webdriver WebDriver driver = new ChromeDriver();// Create Instance of Webdriver driver.manage().window().maximize();// Maximize the window driver.get("https://www.google.com/");// Request the url System.out.println(driver.getTitle());//driver.getTitle()is Used to Capture the Title From Google.com } }
Code Explaination:
System.setProperty(“webdriver.chrome.driver”,”E:/CodeBun/project1/chromedriver.exe”);
In the Above class, “System.setproperty()” method is Used to Initialize the Web driver.
WebDriver driver = new ChromeDriver();
To Create an instance of Chrome “WebDriver driver = new ChromeDriver()” is Used.
driver.manage().window().maximize();
“driver.manage().window().maximize()” is used for maximizing the chrome instance window.
driver.get(“https://www.google.com/”);
“driver.get(“URL”)” is used for Requesting Specific Url.
System.out.println(driver.getTitle());
driver.getTitle(); is Used for Capturing the title From Google.com and System.out.Println(); is Used for Print that value
Output: –
We Have Successfully Completed the Implementation of Our First Selenium Script in Eclipse Ide and requested Google.com and Printed the title Value.
Note: In automation for the result validation and verification will use TestNG but in this article, we are only using Selenium Web Driver and Java.