package mytest; import static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class CalculatorTest { //The URL of the calculator to be opened in the browser private static String CalculatorUrl = "https://specflowoss.github.io/Calculator-Demo/Calculator.html"; //The path of the WebDriver for Chrome private static String driverPath = "D:/Selenium/chromedriver_win32/chromedriver.exe"; //The Selenium web driver to automate the browser private static ChromeDriver chromeDriver; //Test objects private WebElement firstNumberElement = chromeDriver.findElement(By.id("first-number")); private WebElement secondNumberElement = chromeDriver.findElement(By.id("second-number")); private WebElement addButtonElement = chromeDriver.findElement(By.id("add-button")); // by "Add" caption // private WebElement addButtonElement = chromeDriver.findElement(By.xpath("//button[@type='submit' and contains(., 'Add')]")); private WebElement resultElement = chromeDriver.findElement(By.id("result")); private WebElement resetButtonElement = chromeDriver.findElement(By.id("reset-button")); //The default wait time in seconds for wait.until() private int defaultWaitInSeconds = 5; @BeforeClass public static void setUpBeforeClass() throws Exception { //Sets the ChromeDriver path System.setProperty("webdriver.chrome.driver", driverPath); //Creates the Selenium web driver ChromeDriverService chromeDriverService = ChromeDriverService.createDefaultService(); ChromeOptions chromeOptions = new ChromeOptions(); chromeDriver = new ChromeDriver(chromeDriverService, chromeOptions); //Open the calculator page in the browser chromeDriver.navigate().to(CalculatorUrl); } @AfterClass public static void tearDownAfterClass() throws Exception { // Disposes the Selenium web driver (closing the browser) after the test class completed chromeDriver.quit(); } @Before public void setUp() throws Exception { //Click the reset button resetButtonElement.click(); //Wait until the result field is empty waitUntilEmpty(chromeDriver, resultElement); } @After public void tearDown() throws Exception { } // Helper method to wait for the given field to be not empty private void waitUntilNotEmpty(WebDriver driver, WebElement el) { WebDriverWait wait = new WebDriverWait(driver, defaultWaitInSeconds); wait.until(new ExpectedCondition() { public Boolean apply(WebDriver webDriver) { return el.getAttribute("value").length() != 0; } }); } // Helper method to wait for the given field to be empty private void waitUntilEmpty(WebDriver driver, WebElement el) { WebDriverWait wait = new WebDriverWait(driver, defaultWaitInSeconds); wait.until(new ExpectedCondition() { public Boolean apply(WebDriver webDriver) { return el.getAttribute("value").length() == 0; } }); } public void enterFirstNumber(int number) { //Clear text box firstNumberElement.clear(); //Enter text firstNumberElement.sendKeys(Integer.toString(number)); //Check if the text was entered assertEquals(Integer.toString(number), firstNumberElement.getAttribute("value")); } public void enterSecondNumber(int number) { //Clear text box secondNumberElement.clear(); //Enter text secondNumberElement.sendKeys(Integer.toString(number)); //Check if the text was entered assertEquals(Integer.toString(number), secondNumberElement.getAttribute("value")); } public void clickAdd() { //Click the add button addButtonElement.click(); //wait for the result field not empty waitUntilNotEmpty(chromeDriver, resultElement); } /** * The general procedure * @param firstNumber first addendum * @param secondNumber second addendum */ public void sumTestProcedure(int firstNumber, int secondNumber) { //Enter the first addendum enterFirstNumber(firstNumber); //Enter the first addendum enterSecondNumber(secondNumber); //Click the add button clickAdd(); //Check for the result assertEquals(Integer.toString(firstNumber + secondNumber), resultElement.getAttribute("value")); } /** * This tests sums two negative numbers */ @Test public void testNegativeNumbers() { sumTestProcedure(-1, -1); } /** * This tests sums two positive numbers */ @Test public void testPositiveNumbers() { sumTestProcedure(2, 2); } /** * This tests sums one negative and one positive numbers */ @Test public void testPositiveNegativeNumbers() { sumTestProcedure(-2, 3); } }