Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Citations test #102

Merged
merged 9 commits into from
Oct 4, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package gov.cancer.pageobject.crosscutting;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.WebElement;

import gov.cancer.framework.ElementHelper;
import gov.cancer.pageobject.PageObjectBase;
import gov.cancer.pageobject.helper.Citation;

/**
* Pseudo page object representing any page in the system. The Citation class is
* used solely for verifying attributes of a page's Citation section.
*/
public class PageWithCitations extends PageObjectBase {

// The overall section.
WebElement citationsSection = null;

// Citation header
WebElement citationHeader;

/********* CITATION SELECTORS ***********************/

final static String SECTION_SELECTOR = "#cgvCitationSl";
final static String HEADER_SELECTOR = ":scope h6";
final static String CITATION_SELECTOR = ":scope ol > li";

/**
* Constructor
*
* @param path
* server-relative path of the page to load.
*/
public PageWithCitations(String path) {
super(path);

this.citationsSection = ElementHelper.findElement(getBrowser(), SECTION_SELECTOR);
}

/**
* Reports whether the citation section was found.
*
* @return True if it was, false otherwise.
*/
public boolean isSectionPresent() {
if (citationsSection != null)
return citationsSection.isDisplayed();
else
return false;
}

/* Returns true if header of Citation is displayed */
/**
* Is the Citation header present?
*
* @return True if present, false otherwise.
*/
public boolean isHeaderPresent() {
citationHeader = ElementHelper.findElement(citationsSection, HEADER_SELECTOR);
if (citationHeader != null)
return citationHeader.isDisplayed();
else
return false;
}

/**
* Returns the citation section's header.
*
* @return A String containing the header if present.
*/
public String getHeaderText() {
this.citationHeader = ElementHelper.findElement(citationsSection, HEADER_SELECTOR);
if (citationHeader != null)
return citationHeader.getText();
else
return null;
}

/**
* Find all of the citation objects on the page.
*
* @return A List of zero or more Citation objects.
*/
public List<Citation> getCitationList() {

List<Citation> citationList = new ArrayList<Citation>();

List<WebElement> elements = ElementHelper.findElements(this.citationsSection, CITATION_SELECTOR);
for (WebElement el : elements) {
Citation citation = new Citation(el);
citationList.add(citation);
}

return citationList;

}

}
71 changes: 71 additions & 0 deletions src/main/java/gov/cancer/pageobject/helper/Citation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package gov.cancer.pageobject.helper;

import org.openqa.selenium.WebElement;

import gov.cancer.framework.ElementHelper;

/**
* Class representing a single citation.
*/
public class Citation {

static final String TEXT_SELECTOR = ":scope p:nth-child(1)";

// The selector for PUBMED links must look for an immediate child of the
// scope in order to avoid catching links embedded in the citation text.
static final String PUBMED_SELECTOR = ":scope > a";

// The element representing the entire citation.
private WebElement theCitation;

// The citation text. This must be present.
private WebElement textElement;

// The optional PUBMED link.
private WebElement pubmedLink;

/**
* Constructor
*
* @param element
* WebElement containing the markup for the entire citation.
*/
public Citation(WebElement element) {
this.theCitation = element;
this.textElement = ElementHelper.findElement(theCitation, TEXT_SELECTOR);
this.pubmedLink = ElementHelper.findElement(theCitation, PUBMED_SELECTOR);
}

/**
* The text of the citation.
*
* @return String containing the citation text.
*/
public String getText() {
return textElement.getText();
}

/**
* Reports whether the citaiton has a PUBMED link
*
* @return
*/
public boolean hasPubMedLink() {
return (pubmedLink != null && pubmedLink.getText().contains("[PubMed Abstract]"));
}

/**
* Retrieve the citation's PUBMED link.
*
* @return Returns a Link object representing the link if one is present.
* Returns null otherwise.
*/
public Link getPubMedLink() {

if (pubmedLink != null && pubmedLink.getText().contains("[PubMed Abstract]")) {
return new Link(pubmedLink);

} else
return null;
}
}
43 changes: 43 additions & 0 deletions src/main/java/gov/cancer/pageobject/helper/Link.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gov.cancer.pageobject.helper;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebElement;

/**
* Exposes useful methods for working with an <b>a</b> tag.
*/
public class Link {
WebElement element;

/**
* Constructor
* @param element The tag's WebElement.
*/
public Link(WebElement element) {
this.element = element;
}

/**
* Returns the href attribute as a URL object.
*
* @return A URL instance containing the parsed link from the elements href
* attribute, or NULL if the href was missing, empty, or malformed.
*/
public URL getUrl() {

URL theUrl = null;

String href = element.getAttribute("href");
if (href != null && !href.trim().isEmpty()) {
try {
theUrl = new URL(href);
} catch (MalformedURLException e) {
theUrl = null;
}
}

return theUrl;
}
}
Loading