Writing the First Program in Selenium
In the previous post, we have seen the difference between findelement and findElements methods in selenium. In this post, we will write the first program in selenium.
We have already learnt how to create the project and class in Eclipse. It is assumed that you have already configure Selenium as already explained in the earlier post.We will perform login functionality in Gmail application. To start with this, we need to perform the following steps.
1. Open the Gmail application in Firefox browser.
2. Make sure , Sign In page is opened.
3. Press F12 to open firebug. We need to identify the properties of the elements like username,password ,button etc.
4. Create the object of FirefoxDriver and open Gmail application using get() method.
driver.get(“http://www.gmail.com”);
5. Inspect the element Username in the application with the help of Firebug. As we can see, it is having id attribute as “Email“. We will use this to identify username.
How to use this id in selenium to find this element?
For that we use a method named ‘findelement’, where we need to pass locator and its value for creating that particular element. We have use use method to perform some operations according to the type of element. As username is a edit box and we enter the data into that. So, there is method we will use called ‘sendKeys’ which enters the data into edit box.
e.g. We are entering email id “[email protected]” by using below line of code.
6. Same way, we will inspect the another element which is a next button. We need to click on this button to proceed. We will identify its properties as well. But here we will try with another locator which is XPATH. The XPATH of next button is “//*[@id=’next’]“.
As it is a button, action perform on this element will be Click().
7. Similar way, we will create object for Password and Sign In button. You can use any unique property for those elements.
Now, Run the program and it should login into the Gmail application.
Full Code of Login into Gmail:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class LoginGmail { private static WebDriver driver; public static void main(String[] args) throws InterruptedException { driver = new FirefoxDriver(); driver.get("http://www.gmail.com"); driver.findElement(By.id("Email")).sendKeys("[email protected]"); driver.findElement(By.xpath(".//*[@id='next']")).click(); Thread.sleep(3000L); // Thread.sleep is used to put some wait before executing next step (3 seconds) driver.findElement(By.id("Passwd")).sendKeys("1234");; driver.findElement(By.id("signIn")).click(); } }
1 Response
[…] the previous post, we have written a simple program. In this post, we will learn about the close() and Quit() method in […]