How to start automation with selenium webdriver
In last article on selenium we discussed basics of selenium. How selenium works and what are different ways to automate with selenium. (read here What is selenium? How selenium works ?). we discussed selenium IDE and selenium webdriver in that post. In this article will learn How to start automation with selenium webdriver.
This post is for beginners to right hello world script with selenium webdriver. Here we will discuss setting up java, Start with eclipse, create new project, Set up java build path for selenium webdriver, a sample script.
Here is the simple automation code you can add in to class and run. This code will open Bing URL, search query and press Go Button. This is only sample code like Hello word script for selenium webdriver automation for beginners.
This post is for beginners to right hello world script with selenium webdriver. Here we will discuss setting up java, Start with eclipse, create new project, Set up java build path for selenium webdriver, a sample script.
Setup needed for writing Automation script with selenium webdriver
- Latest Java setup. Install JDK and JRE and set the Environment variables properly. (to check java is properly set or not, OPEN Command Prompt, type "java -version")
- Download Eclipse. Open Eclipse and create work space.
- Download selenium standalone server jar (click here to download. Download it for java)
- Download chromedriver.exe (to automate in chrome) Click here to download
Writing first script with Selenium webdriver with Java
- First you need to create new project in eclipse
For this, Go to File>New>Project. Select java Project. Go next Give project name and Finish. You will see a folder with given project name in Project Explorer - Create a package inside the project
For this, right click on project, select new, package. Give package name and finish. - Create a class file inside project.
for this, right click on package, select new, class. Give proper class name and finish.
- Add Selenium standalone server jar to libs.
For this right click on Project, Go to Build path>Configure build path. Go to Libraries, click on Add External Jars and jar you downloaded.
here you can see added selenium server standalone jar. Now go to Order and Export and select added jar, Apply and you are done.Java Build path
Here is the simple automation code you can add in to class and run. This code will open Bing URL, search query and press Go Button. This is only sample code like Hello word script for selenium webdriver automation for beginners.
So First you need to define webdriver
public WebDriver driver;
public WebDriver driver;
String winHandleBefore = driver.getWindowHandle();
Based on browser (chrome, mozila, safari or IE ) you need to set the property. For chrome you need to pass driver id and path where it is located
System.setProperty("webdriver.chrome.driver","C:\\Automation\\chromedriver.exe")
WebDriver driver = new ChromeDriver();
To open chrome browserdriver.manage().window().maximize();
To hit URL
driver.get("http://www.bing.com");
driver.get("http://www.bing.com");
To find element driver.findElement is method to find the element. as shown in code, you can find element by Name, Class or othe Xpath.
To find Name,Class,Id for element
To find Name,Class,Id for element
- right click on element you want to find
- click on inspect
- you can elements as shown in below screenshot.
Here is the detailed post How to find Xpaths in Selenium Webdriver