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.

Sign In page

4. Create the object of FirefoxDriver and open Gmail application using get() method.

WebDriver driver = new FirefoxDriver();
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.

Object identify in selenium

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 “shekhar@gmail.com” by using below line of code.

driver.findElement(By.id(“Email”)).sendKeys(“shekhar@gmail.com”);

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’]“.

Next Button

driver.findElement(By.xpath(“.//*[@id=’next’]”)).click();

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.

Click on button in selenium

 

driver.findElement(By.id(“Passwd”)).sendKeys(“12345”); driver.findElement(By.id(“signIn”)).click();

 

We have used Thread.Sleep(3000L) which will pause the execution for 3 seconds before executing next step. It will add an exception(InterruptedException) in method signature using throws keyword.

Now, Run the program and it should login into the Gmail application.

Full Code of Login into Gmail:


Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum
Print Friendly, PDF & Email
Difference between findElement() and findElements()
Close() and Quit() method in Selenium WebDriver
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. August 16, 2015

    […] the previous post, we have written a simple program. In this post, we will learn about the close() and Quit() method in […]

Leave a Reply

Your email address will not be published. Required fields are marked *