Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Selenium WebDriver and JUnit to test a web application
package newpackage;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTestSuite {
private WebDriver driver;
private void showTestAlert(WebDriver driver, String message) throws Exception {
//Generating Alert Using Javascript Executor
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript(message);
Thread.sleep(2000);
driver.switchTo().alert().accept();
}
// Tests preparations
@Before
public void testsPreparations() throws Exception {
// Optional, if not specified, WebDriver will search your path for Chromedriver
System.setProperty("webdriver.chrome.driver", "/Users/Antonio/Dropbox/Projects/seleniumtest/chromedriver/chromedriver.exe");
// Launch Chrome and direct it to the URL
driver = new ChromeDriver();
driver.get("http://psmiscrumtest.herokuapp.com/starttest");
// Let the user actually see something!
Thread.sleep(500);
}
// After every test execution
@After
public void testsRelease() throws Exception {
// Let the user actually see something!
Thread.sleep(500);
driver.quit();
}
// Check if page opens and shows welcome page
@Test
public void testFirstPage() throws Exception {
String expectedTitle = "Scrumtest";
String actualTitle = "";
// Get the actual value of the title
actualTitle = driver.getTitle()
assertEquals(actualTitle, expectedTitle);
}
// Check if Start Button leads you to question #0
@Test
public void testFirstQuestion() throws Exception {
WebElement searchBox = driver.findElement(By.className("button_to"));
searchBox.submit();
Thread.sleep(500); // Let the user actually see something!
String expectedUrl = "http://psmiscrumtest.herokuapp.com/questions/0";
String actualUrl = "";
// Get the actual value of the Url
actualUrl = driver.getCurrentUrl();
assertEquals(actualUrl, expectedUrl);
}
// Check if there are ten multiple choice questions (no more, no less) and a results page after that
@Test
public void test10Questions() throws Exception {
showTestAlert(driver, "Ten multiple choice questions test is about to start now...");
WebElement searchBox = driver.findElement(By.className("button_to"));
searchBox.submit();
Thread.sleep(500); // Let the user actually see something!
for (int i = 0; i < 10; i++) {
List<WebElement> answersList = driver.findElements(By.name("answers_radio"));
double d = Math.random() * 2;
int option = (int) Math.round(d);
WebElement firstAnswer = answersList.get(option);
firstAnswer.click();
// Let the user actually see something!
Thread.sleep(500);
WebElement nextButton = driver.findElement(By.id("answers_submit"));
nextButton.click();
}
String resultsPageTitle = driver.getCurrentUrl();
assertEquals(resultsPageTitle, "http://psmiscrumtest.herokuapp.com/results");
}
}
@antoniomartel

This comment has been minimized.

Copy link
Owner Author

antoniomartel commented Jul 17, 2016

Dummy class to play around building Functional Tests with Selenium WebDriver and JUnit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.