Skip to content

Commit

Permalink
[plugin-web-app-to-rest-api] Revert back safe building of CSS selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Jul 15, 2024
1 parent a82b2b9 commit ffa9b85
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jbehave.core.model.ExamplesTable;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Selector.SelectorParseException;
import org.vividus.html.HtmlLocatorType;
import org.vividus.html.JsoupUtils;
import org.vividus.http.HttpMethod;
Expand Down Expand Up @@ -162,7 +163,7 @@ private Optional<WebPageResourceValidation> parseElement(Element element, boolea
return Optional.empty();
}

String elementCssSelector = element.cssSelector();
String elementCssSelector = getCssSelector(element);
if (elementUriAsString.isEmpty())
{
return Optional.of(ResourceValidationError.EMPTY_HREF_SRC
Expand Down Expand Up @@ -246,6 +247,18 @@ private static String getElementUri(Element element)
return element.attr("src");
}

private String getCssSelector(Element element)
{
try
{
return element.cssSelector();
}
catch (SelectorParseException exception)
{
return String.format("Unable to build CSS selector for '%s' element", element.tagName());
}
}

private URI resolveUri(String uri) throws URISyntaxException
{
URI uriToCheck = new URI(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class ResourceCheckStepsTests
<p id='section'>Section</p>
<a id='jump-link-using-name' href='#named-section'>Jump link using name</a>
<p name='named-section'>Named Section</a>
<img class="vds-flex_1 vds-d_block lg:vds-d_flex vds-flex_column vds-items_flex-end
[&amp;_>_*:first-child]:vds-pt_0"
href="https://docs.vividus.dev/vividus/latest/_images/subscribe-to-releases.gif"></a>
</body>
</html>""";

Expand Down Expand Up @@ -228,13 +231,16 @@ void shouldCheckDesiredResourcesAndPostAttachment() throws InterruptedException,
@SuppressWarnings(UNCHECKED)
Set<WebPageResourceValidation> validationsToReport = ((Map<String, Set<WebPageResourceValidation>>) m)
.get(RESULTS);
assertThat(validationsToReport, hasSize(16));
assertThat(validationsToReport, hasSize(17));
Iterator<WebPageResourceValidation> resourceValidations = validationsToReport.iterator();
validate(resourceValidations, URI.create(NAMED_SECTION_SELECTOR), JUMP_LINK_USING_NAME_SELECTOR,
CheckStatus.PASSED, N_A);
validate(resourceValidations, URI.create(SECTION_SELECTOR), JUMP_LINK_SELECTOR, CheckStatus.PASSED, N_A);
validate(resourceValidations, SERENITY_URI, HTTP_ID, CheckStatus.PASSED, N_A);
validate(resourceValidations, imageUri, "#image", CheckStatus.PASSED, N_A);
validate(resourceValidations,
URI.create("https://docs.vividus.dev/vividus/latest/_images/subscribe-to-releases.gif"),
"Unable to build CSS selector for 'img' element", CheckStatus.PASSED, N_A);
validate(resourceValidations, EXTERNAL_SECTION_LINK, EXTERNAL_SECTION_LINK_SELECTOR, CheckStatus.PASSED,
N_A);
validate(resourceValidations, gifImageUri, "html > body > img.image.\\(gif\\)",
Expand Down

0 comments on commit ffa9b85

Please sign in to comment.