com.codacy.scoobydoo.web.BaseWebPage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scooby-doo-fwk Show documentation
Show all versions of scooby-doo-fwk Show documentation
Automated testing framework
package com.codacy.scoobydoo.web;
import com.codacy.scoobydoo.LoggingHelper;
import com.codacy.scoobydoo.configuration.Configuration;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.asserts.SoftAssert;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public abstract class BaseWebPage extends WebFragment {
static final Map ELEMENTS_LINK_ATTRIBUTES;
static {
ELEMENTS_LINK_ATTRIBUTES = new LinkedHashMap<>();
ELEMENTS_LINK_ATTRIBUTES.put("img", "src");
ELEMENTS_LINK_ATTRIBUTES.put("a", "href");
}
// TODO: check if this is really needed.
public WebDriverWait wait;
public BaseWebPage(RemoteWebDriver driver, Configuration configuration) {
super(driver, configuration);
PageFactory.initElements(driver, this);
this.wait = new WebDriverWait(driver, config.getTimeout().PAGE_LOAD_TIMEOUT);
}
public void refreshPage() {
getDriver().navigate().refresh();
}
public void verifyElementsUrls(List elements) {
SoftAssert softAssertions = new SoftAssert();
for (WebElement element : elements) {
try {
String tag = element.getTagName();
String urlLink = element.getAttribute(ELEMENTS_LINK_ATTRIBUTES.get(tag));
URL link = new URL(urlLink);
HttpURLConnection httpConn = (HttpURLConnection) link.openConnection();
httpConn.setConnectTimeout(30000);
httpConn.connect();
if (httpConn.getResponseCode() == 200) {
LoggingHelper.info(urlLink + " - " + httpConn.getResponseMessage());
}
if (httpConn.getResponseCode() == 404) {
LoggingHelper.info(urlLink + " - " + httpConn.getResponseMessage());
softAssertions.fail("The URL [" + urlLink + "] returned 404.");
}
} catch (Exception ignored) {
}
}
softAssertions.assertAll();
}
public void verifyImages() {
List elements = getElementsByType("img");
verifyElementsUrls(elements);
}
public void verifyLink() {
List elements = getElementsByType("a");
verifyElementsUrls(elements);
}
}