Wednesday 21 May 2014

Creating Test Case in Eclipse using Selenium Web Driver



Creating Test Case in Eclipse using Selenium Web Driver


1.       Create a new project in your Eclipse.
File-->  New --> Project --> Java Project--> Specify the project name--> Click on Finish
Your new project will be created in Eclipse.





2. Now create a new package inside the project created.
 
Steps:
1.  Right Click on the Project name.
2.  Click on New-->Package.
3.  A pop up window will open.
4.   Enter the Package name.
A new package is created.



  1. Create a new class in the package.
Steps:

1. Right Click on the Package name.
2. Click on New-->Class.
3. A pop up window will open.
4. Enter the Class name.

A new class will be created.


  1. Add External Jars to the project.
Steps:
1.       Right Click on Project name and click on properties.
2.       Select “Java Build Path” on left side and “Libraries” in the tab.
3. Click on "Add external JARs" button.
4. Select all the jar files extracted and saved on the machine earlier.
5. Click on OK.














  1. The project structure looks like this.


  1. The package and class will be created by default . Create one new method called “OpenGoogle()” to write your testcase.


  1. Write annotation ”@Test” above  the method name. Immediately , Eclipse will show an error. Resolve the error by clicking on it and importing the “org.junit.Test”.
Meaning on annotation @Test: The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.



  1. Code will look like as shown below.










  1. Write a code for opening firefox browser.
WebDriver driver = new FirefoxDriver();
The moment you write this line of code, Eclipse will show 2 errors.



9.       Resolve the above errors by clicking on it and importing org.openqa.selenium.WebDriver and org.openqa.selenium.firefox.FirefoxDriver.


  1. Now this statement will open a new firefox browser.
Meaning of the statement
WebDriver driver = new FirefoxDriver();
WebDriver: It is a predefined imported Class from “open.openqa.selenium”
Driver : This is name of the new object created.
New : “new” java keyword is used.


  1. Write a line of code to open the URL you want to open.
driver.get("https://www.google.co.in/");


Meaning of the statement
driver.get--->WebDriver's get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter. You can specify the URL to be opened.











  1. Write code for finding UI elements and inputting text.


Meaning of the statement
driver.findElement()--->Locating elements in WebDriver is done by using the "findElement(By.locator())" method. The locator can be xpath, class, id, name.

The following are the available options for locating elements in WebDriver:
  By.className
  By.cssSelector
  By.id
  By.linkText
  By.name
  By.partialLinkText
  By.tagName
  By.xpath
  1. Click on “SIGN IN” Button.
Thread.sleep()-Suspends the current thread for the specified amount of time. It is used to add wait time.

And add the code to lcoate the element and click it.


  1. Now write a code to enter user name and password. Find the elements and input text in to he texboxes and click on Sign In Button.
Sendkeys()-->this method to simulate typing into an element, which may set its value.




  1. Now validate whether user is logged in successfully or not. For this we write the following code.
Locate the element for error message and validate the text using if and else.



  1. Now write a line of code to close the browser window.
The close() and quit() methods are used to close browser windows.

Close() is used to close a single window; while quit() is used to close all windows associated to the parent window that the Web Driver object was controlling.

  1. Now run the test case. Right click on the screenàRun asà JUnit Test case.
  2. The Result will shown as below. In case of errors, it will be displayed in Failure Trace.



No comments:

Post a Comment