If you are test cases using jUnits then it is very simple to execute them in Eclipse. But before that you need to execute the core Selenium classes which will come to your help very frequently.
org.openqa.selenium.WebDriver
: The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories – Control of the browser itself, Selection of WebElements, Debugging aids
org.openqa.selenium.WebElement
: Represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface.
org.openqa.selenium.By
: Mechanism used to locate elements within a document.
Below is a sample class which illustrates a Selenium test case.
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class TestEndToEndPages {
private WebDriver driver;
@Before
public void setUp() {
// Create a new instance of the html unit driver
driver = new HtmlUnitDriver();
//Navigate to desired web page
driver.get("http://localhost:6060/WebApplication4Selenium");
}
@Test
public void shouldBeAbleEnterUserNameAndClickSubmitToVerifyWelcomeMessage()
{
// verify title of index page
verifyTitle("Enter your name");
//verify header of index page
verifyHeaderMessage("Please enter your name");
//enter user name as Allen
enterUserName("Allen");
//verify title of welcome page
verifyTitle("Welcome");
//verify header of welcome page
verifyHeaderMessage("Welcome Allen!!!");
//verify back link and click on it
backToPreviousPage("go back");
//verify title of index page again to make sure link is working
verifyTitle("Enter your name");
}
private void verifyTitle(String expectedTitle) {
//get the title of the page
String actualTitle = driver.getTitle();
// verify title
assertThat(actualTitle, equalTo(expectedTitle));
}
private void verifyHeaderMessage(String expectedHeaderMessage) {
// find header element
WebElement element = driver.findElement(By.tagName("h3"));
String actualHeaderMessage = element.getText();
// verify header text
assertThat(actualHeaderMessage, equalTo(expectedHeaderMessage));
}
private void enterUserName(String userName) {
// find the input text box
WebElement element = driver.findElement(By.name("userName"));
// set the user name in input text box
element.sendKeys(userName);
// submit form
element.submit();
}
private void backToPreviousPage(String expectedLinkText) {
// find the link by its id
WebElement element = driver.findElement(By.id("back"));
//get the actual link text
String actualLinkText = element.getText();
//verify link text with expected like text
assertThat(actualLinkText, equalTo(expectedLinkText));
// click the link
element.click();
}
}
Source: Test your web application’s UI with JUnit and Selenium
If you look closely at the comments in the above mentioned test class, you will be able to find how you can navigate to a page or how you can find an element to perform certain operations like get the text, set a value, trigger any event, etc.
UPDATE: Creating Executable JAR file.
Since you have your working JUnit test cases in place, you can use below snippet of main
method and create an executable JAR file.
public static void main(String[] args) throws Exception {
JUnitCore.main("foo.bar.MyTestSuite");
}
Hope this makes for what you were looking for.
Shishir