Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Writing the First Program in Selenium - Testingpool

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:

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("shekhar@gmail.com");
		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();

	}
}

Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

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 […]