If you use test cases with jUnits, then they are very easy to execute in Eclipse. But before that, you need to run the main Selenium classes, which very often come to your aid.
org.openqa.selenium.WebDriver : The main testing interface, which is an idealized web browser. Methods in this class are divided into three categories: control of the browser itself, selection of WebElements, debugging toolsorg.openqa.selenium.WebElement : Represents an HTML element. As a rule, all interesting operations related to interacting with the page will be performed through this interface.org.openqa.selenium.By : a mechanism used to search for elements within a document.
The following is an example class that illustrates the 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: Check Web Application User Interface Using JUnit and Selenium
If you carefully look at the comments in the aforementioned test class, you can find how you can go to the page or how you can find an element to perform certain operations, such as receiving text, setting a value, triggering any event, etc.
UPDATE: Creating an executable JAR file.
Since you have valid JUnit test cases, you can use the main method fragment below and create an executable JAR file.
public static void main(String[] args) throws Exception { JUnitCore.main("foo.bar.MyTestSuite"); }
Hope this does what you were looking for.
Shishir
Shishir kumar
source share