Untitled
unknown
java
3 years ago
4.0 kB
5
Indexable
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.*; import org.openqa.selenium.*; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.io.File; import java.io.IOException; import java.time.Duration; import java.util.*; public class Proto { static List<WebElement> builds; static List<WebElement> operations = new ArrayList<>(); static JsonNode data; static WebDriver driver; @BeforeAll static void setup() throws IOException { driver = new EdgeDriver(); // change driver here driver.get("https://testsheepnz.github.io/BasicCalculator.html"); WebElement build = driver.findElement(By.id("selectBuild")); builds = build.findElements(By.tagName("option")); WebElement operation = driver.findElement(By.id("selectOperationDropdown")); operations = operation.findElements(By.tagName("option")); ObjectMapper mapper = new ObjectMapper(); File file = new File("src/test/java/data.json"); data = mapper.readTree(file); } private static void testStep(WebElement type, String firstNumber, String secondNumber, boolean integersOnly) { WebElement first = driver.findElement(By.id("number1Field")); WebElement second = driver.findElement(By.id("number2Field")); WebElement calculate = driver.findElement(By.id("calculateButton")); type.click(); if (!first.isDisplayed() || !second.isDisplayed() || !calculate.isDisplayed()) { return; } new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(first)); new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(second)); ((JavascriptExecutor)driver) .executeScript("document.getElementById('integerSelect').checked = " + (integersOnly ? "true" : "false") + ";"); first.clear(); second.clear(); first.sendKeys(firstNumber); second.sendKeys(secondNumber); new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(calculate)); calculate.click(); } @AfterAll static void breakdown() { driver.quit(); } @DisplayName("hello") // change display name @TestFactory List<DynamicTest> testFunc() { List<DynamicTest> tests = new ArrayList<>(); for (WebElement build : builds) { for (WebElement type : operations) { build.click(); for (JsonNode test : data.get(type.getText())) { final String tcName = test.get("TCName").textValue(); final String firstNumber = test.get("firstNumber").textValue(); final String secondNumber = test.get("secondNumber").textValue(); final boolean integersOnly = test.get("integersOnly").booleanValue(); final String expectedResult = test.get("expectedResult").textValue(); String error = driver.findElement(By.id("errorMsgField")).getText(); testStep(type, firstNumber, secondNumber, integersOnly); if (!error.equals("") || !error.isEmpty()) { // TODO Skip calculating loading JavascriptExecutor executor = (JavascriptExecutor) driver; executor.executeScript("document.getElementById('calculateButton').removeAttribute('disabled');"); tests.add(DynamicTest.dynamicTest(build + ", " + tcName, () -> Assertions.assertEquals(expectedResult, error))); testStep(operations.get(0),"1", "1", true); } else { String answer; WebElement answerEle = driver.findElement(By.id("numberAnswerField")); new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.visibilityOf(answerEle)); answer = answerEle.getAttribute("value"); String finalAnswer = answer; tests.add(DynamicTest.dynamicTest(build + ", " + tcName, () -> Assertions.assertEquals(expectedResult, finalAnswer))); } } } } return tests; } }
Editor is loading...