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.
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.
- 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.
- 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.
- The project structure looks like this.
- The package and class will be created by default . Create one new method called “OpenGoogle()” to write your testcase.
- 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.
- Code will look like as shown below.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- Now run the test case. Right click on the screenàRun asà JUnit Test case.
- The Result will shown as below. In case of errors, it will be displayed in Failure Trace.
No comments:
Post a Comment