Introduced FredlTestHelper class

This commit is contained in:
Dirk Jahnke 2019-07-26 13:48:05 +02:00
parent 07e7c4fd32
commit 8512339bea
3 changed files with 121 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
.tmp
.bak

View File

@ -0,0 +1,107 @@
package org.fremo.fredl.test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class BasicTest {
private WebDriver driver;
private WebDriverWait wait;
private int implicitTimeoutMilliseconds = 3000;
private int timeoutInSeconds = 20;
String URL = "https://0.fredldev.fremo-net.eu";
@BeforeClass
public void testSetUp() {
driver = new HtmlUnitDriver(true);
wait = new WebDriverWait(driver, timeoutInSeconds);
driver.manage().timeouts().implicitlyWait(implicitTimeoutMilliseconds, TimeUnit.MILLISECONDS);
}
@AfterClass
public void tearDown() {
driver.quit();
}
private boolean elementExistsByXpath(String xpath) {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
boolean exists = driver.findElements(By.xpath(xpath)).size() != 0;
driver.manage().timeouts().implicitlyWait(implicitTimeoutMilliseconds, TimeUnit.MILLISECONDS);
return exists;
}
private boolean elementExistsById(String id) {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
boolean exists = driver.findElements(By.id(id)).size() != 0;
driver.manage().timeouts().implicitlyWait(implicitTimeoutMilliseconds, TimeUnit.MILLISECONDS);
return exists;
}
@Test
public void verifyFredlPageTitle() {
driver.navigate().to(URL);
String getTitle = driver.getTitle();
System.out.println("Page title: " + getTitle);
Assert.assertEquals(getTitle, "FREDL");
}
@Test
public void verifyLoginAsSystemUser() {
driver.navigate().to(URL);
System.out.println("Navigate to: " + URL);
if (elementExistsById("privacy-cookie-statement")) {
System.out.println("Cookies consent page is shown");
WebElement consentButton = driver.findElement(By.id("opt-in-button-allow"));
consentButton.click();
}
WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.name("doLogin")));
String scaleInfo = driver.findElement(By.xpath("//table[@class='loginall']/tbody/tr/td/div")).getText();
System.out.println("Scale: " + scaleInfo);
driver.findElement(By.xpath("//input[@name='login']")).sendKeys("system");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("system");
loginButton.click();
String expectedTitle = "FreDL";
wait.until(ExpectedConditions.titleIs(expectedTitle));
FredlTestHelper.printPageSource(driver);
//driver.switchTo().frame("oben");
String frameObenName = "oben";
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameObenName));
FredlTestHelper.printPageSource(driver);
String versionInfo = driver.findElement(By.xpath("//table[1]/tbody/tr/td[1]/b")).getText();
String userInfo = driver.findElement(By.xpath("//table[1]/tbody/tr/td[3]")).getText();
String expectedUserInfo = "Benutzer: FREDL System (Syste)";
System.out.println("Version info found: " + versionInfo);
System.out.println("User info found: " + userInfo);
Assert.assertEquals(userInfo, expectedUserInfo);
driver.switchTo().parentFrame();
String frameRechtsName = "rechts";
driver.switchTo().frame(frameRechtsName);
FredlTestHelper.printPageSource(driver);
String welcomeMessage = driver.findElement(By.xpath("//body/h1")).getText();
String expectedWelcomeMessage = "Willkommen bei FreDL";
System.out.println("Welcome message is: " + welcomeMessage);
Assert.assertEquals(welcomeMessage, expectedWelcomeMessage);
}
}

View File

@ -0,0 +1,11 @@
package org.fremo.fredl.test;
import org.openqa.selenium.WebDriver;
public class FredlTestHelper {
public static void printPageSource(WebDriver driver) {
String body; // used for debugging purposes only
body = driver.getPageSource();
System.out.println("HTML: " + body);
}
}