Last active
July 17, 2016 19:41
-
-
Save antoniomartel/fb708c7d2ac8c5a713038661f59c28ff to your computer and use it in GitHub Desktop.
Selenium WebDriver and JUnit to test a web application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dummy class to play around building Functional Tests with Selenium WebDriver and JUnit