Monday, April 16, 2012

Selenium Testcase with Junit4 and more about @Before and @After Annotation

LinkShare_468x60v1
Untitled 3
1.Create a Project in Eclipse
2.Create a Package under the project
3.Create the Junit Test case(Selecting Junit4)

Import the Selenium and JUnit. You can the use the following code:
import com.thoughtworks.selenium.*;
import org.junit.*;

We now need to start a browser. So declare Selenium variable outside of any method.
Create a new method that setUP() but before that use the @Before annotation. Before is used to execute set of precondition before executing the test. This Method start the browser.

Create a second method that is logoTest that is basically our Test case.

Create a third Method that is Teardown but before that use @After annotation This annotation execute after execution of every Test This method kill the browser when test will be finished.

import com.thoughtworks.selenium.*;
import org.junit.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SampleTest {
Selenium selenium; //Creating the Selenium instance

@Before
public void setUP(){
selenium =new DefaultSelenium("localhost",4444,"*chrome","http://www.facebook.com");
selenium.start();
}

@Test
public void logoTest()throws Exception{
selenium.open("http://www.facebook.com");
verifyTrue(selenium.isElementPresent("//*[@id='blueBar']/div/div/a/img"));
}

@After
public void tearDown(){
selenium =new DefaultSelenium("localhost",4444,"*chrome","http://www.facebook.com");
selenium.start();
}

}

No comments:

Post a Comment