JUnit tutorial¶
Installing selenium in Eclipse¶
There are many tutorials that will guide you through adding selenium to eclipse. For instance, Configure Eclipse with Selenium.
Writing tests¶
I encourage you to write your selenium tests as JUnit tests. Here are some resources to help you:
Sample code¶
Here is the code from the tutorial:
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyFirstTest {
WebDriver driver;
@Before
public void goToFire() {
driver = new FirefoxDriver();
driver.get("http://127.0.0.1:5000/lbs/");
}
private void login(String username, String password) {
driver.findElement(By.name("email")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("loginbtn")).click();
}
@Test
public void testFireLoginAndLogout() {
login("arnarbi@gmail.com","test");
assertEquals("Laboration overview for Student Birgisson (TDA602)",
driver.findElement(By.tagName("h1")).getText());
}
@Test
public void testFireBadLogin() {
login("arnarbi@gmail.com", "testxxx");
assertEquals("Please log in",
driver.findElement(By.tagName("h1")).getText());
}
@After
public void quitBrowser() {
driver.quit();
}
}