Skip to content

Commit

Permalink
feat: rework "containing" selectors via addition of remaining attribu…
Browse files Browse the repository at this point in the history
…te operators (#1786)

Refactored name of "xContaining" attribute selector methods to "xContainingToken".

Added true "xContaining" attribute selector methods that match on substrings instead of on tokens.

Added remaining attribute matching operators and a corresponding selector method to utilize them. Refactored existing attribute-based selector methods to use this method.

Fixes #1554
  • Loading branch information
joelpop authored Apr 24, 2024
1 parent 67fbea7 commit 971421a
Show file tree
Hide file tree
Showing 5 changed files with 534 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
Expand Down Expand Up @@ -26,6 +26,7 @@
import java.util.function.Function;
import java.util.function.Predicate;

import static com.vaadin.testbench.ElementQuery.AttributeMatch.Comparison.CONTAINS_WORD;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -315,17 +316,35 @@ void findInElement_byWithAttributeContaining() {
findFirstInElement(query -> query
.withAttributeContaining("foo", "bar")
.first(),
"[foo~='bar']",
"Search should fail as no element containing the attribute value exists in element");
"[foo*='bar']",
"Search should fail as no element containing the attribute substring exists in element");
}

@Test
void findInDocument_byWithAttributeContaining() {
findFirstInDocument(query -> query
.withAttributeContaining("foo", "bar")
.first(),
"[foo*='bar']",
"Search should fail as no element containing the attribute substring exists in document");
}

@Test
void findInElement_byWithAttributeContainingWord() {
findFirstInElement(query -> query
.withAttributeContainingWord("foo", "bar")
.first(),
"[foo~='bar']",
"Search should fail as no element containing the attribute value exists in document");
"Search should fail as no element containing the attribute word exists in element");
}

@Test
void findInDocument_byWithAttributeContainingWord() {
findFirstInDocument(query -> query
.withAttributeContainingWord("foo", "bar")
.first(),
"[foo~='bar']",
"Search should fail as no element containing the attribute word exists in document");
}

@Test
Expand Down Expand Up @@ -369,7 +388,7 @@ void findInElement_byWithoutAttributeContaining() {
findFirstInElement(query -> query
.withoutAttributeContaining("foo", "bar")
.first(),
":not([foo~='bar'])",
":not([foo*='bar'])",
null);
}

Expand All @@ -378,6 +397,24 @@ void findInDocument_byWithoutAttributeContaining() {
findFirstInDocument(query -> query
.withoutAttributeContaining("foo", "bar")
.first(),
":not([foo*='bar'])",
null);
}

@Test
void findInElement_byWithoutAttributeContainingWord() {
findFirstInElement(query -> query
.withoutAttributeContainingWord("foo", "bar")
.first(),
":not([foo~='bar'])",
null);
}

@Test
void findInDocument_byWithoutAttributeContainingWord() {
findFirstInDocument(query -> query
.withoutAttributeContainingWord("foo", "bar")
.first(),
":not([foo~='bar'])",
null);
}
Expand Down Expand Up @@ -761,11 +798,11 @@ void attributesConventionValue() {
}

@Test
void attributesConventionContains() {
void attributesConventionContainsWord() {
Set<AttributeMatch> attributes = ElementQuery
.getAttributes(MyFancyViewContainsElement.class);
assertEquals(set(
new AttributeMatch("class", "~=", "my-fancy-view-contains")),
new AttributeMatch("class", CONTAINS_WORD, "my-fancy-view-contains")),
attributes);
}

Expand All @@ -787,11 +824,11 @@ void attributesCanBeOverridden() {
}

@Test
void multipleAttributeAnnotations() {
void multipleAttributeAnnotationWords() {
Set<AttributeMatch> attributes = ElementQuery
.getAttributes(MultipleAnnotationElement.class);
assertEquals(set(new AttributeMatch("class", "~=", "foo"),
new AttributeMatch("class", "~=", "bar")), attributes);
assertEquals(set(new AttributeMatch("class", CONTAINS_WORD, "foo"),
new AttributeMatch("class", CONTAINS_WORD, "bar")), attributes);
}

@SafeVarargs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
Expand Down Expand Up @@ -26,6 +26,7 @@
import java.util.function.Function;
import java.util.function.Predicate;

import static com.vaadin.testbench.ElementQuery.AttributeMatch.Comparison.CONTAINS_WORD;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -315,17 +316,35 @@ public void findInElement_byWithAttributeContaining() {
findFirstInElement(query -> query
.withAttributeContaining("foo", "bar")
.first(),
"[foo~='bar']",
"Search should fail as no element containing the attribute value exists in element");
"[foo*='bar']",
"Search should fail as no element containing the attribute substring exists in element");
}

@Test
public void findInDocument_byWithAttributeContaining() {
findFirstInDocument(query -> query
.withAttributeContaining("foo", "bar")
.first(),
"[foo*='bar']",
"Search should fail as no element containing the attribute substring exists in document");
}

@Test
public void findInElement_byWithAttributeContainingWord() {
findFirstInElement(query -> query
.withAttributeContainingWord("foo", "bar")
.first(),
"[foo~='bar']",
"Search should fail as no element containing the attribute value exists in document");
"Search should fail as no element containing the attribute word exists in element");
}

@Test
public void findInDocument_byWithAttributeContainingWord() {
findFirstInDocument(query -> query
.withAttributeContainingWord("foo", "bar")
.first(),
"[foo~='bar']",
"Search should fail as no element containing the attribute word exists in document");
}

@Test
Expand Down Expand Up @@ -369,7 +388,7 @@ public void findInElement_byWithoutAttributeContaining() {
findFirstInElement(query -> query
.withoutAttributeContaining("foo", "bar")
.first(),
":not([foo~='bar'])",
":not([foo*='bar'])",
null);
}

Expand All @@ -378,6 +397,24 @@ public void findInDocument_byWithoutAttributeContaining() {
findFirstInDocument(query -> query
.withoutAttributeContaining("foo", "bar")
.first(),
":not([foo*='bar'])",
null);
}

@Test
public void findInElement_byWithoutAttributeContainingWord() {
findFirstInElement(query -> query
.withoutAttributeContainingWord("foo", "bar")
.first(),
":not([foo~='bar'])",
null);
}

@Test
public void findInDocument_byWithoutAttributeContainingWord() {
findFirstInDocument(query -> query
.withoutAttributeContainingWord("foo", "bar")
.first(),
":not([foo~='bar'])",
null);
}
Expand Down Expand Up @@ -762,11 +799,11 @@ public void attributesConventionValue() {
}

@Test
public void attributesConventionContains() {
public void attributesConventionContainsWord() {
Set<AttributeMatch> attributes = ElementQuery
.getAttributes(MyFancyViewContainsElement.class);
assertEquals(set(
new AttributeMatch("class", "~=", "my-fancy-view-contains")),
new AttributeMatch("class", CONTAINS_WORD, "my-fancy-view-contains")),
attributes);
}

Expand All @@ -788,11 +825,11 @@ public void attributesCanBeOverridden() {
}

@Test
public void multipleAttributeAnnotations() {
public void multipleAttributeAnnotationWords() {
Set<AttributeMatch> attributes = ElementQuery
.getAttributes(MultipleAnnotationElement.class);
assertEquals(set(new AttributeMatch("class", "~=", "foo"),
new AttributeMatch("class", "~=", "bar")), attributes);
assertEquals(set(new AttributeMatch("class", CONTAINS_WORD, "foo"),
new AttributeMatch("class", CONTAINS_WORD, "bar")), attributes);
}

@SafeVarargs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2000-2022 Vaadin Ltd
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
Expand All @@ -19,6 +19,9 @@

import java.util.List;

import static com.vaadin.testbench.ElementQuery.AttributeMatch.Comparison.BEGINS_WITH;
import static com.vaadin.testbench.ElementQuery.AttributeMatch.Comparison.ENDS_WITH;
import static com.vaadin.testbench.ElementQuery.AttributeMatch.Comparison.NOT_CONTAINS_PREFIX;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ElementQueryIT extends AbstractBrowserTB9Test {
Expand Down Expand Up @@ -157,15 +160,48 @@ void withAttribute() {
Assertions.assertEquals(0, noSlottedButtons.size());
}

@BrowserTest
void withAttributeOperator() {
openTestURL();
TemplateViewElement view = $(TemplateViewElement.class).waitForFirst();

List<NativeButtonElement> nativeButtonElements = view.$(NativeButtonElement.class)
.withAttribute("id", "shadow", BEGINS_WITH)
.all();
Assertions.assertEquals(2, nativeButtonElements.size());

nativeButtonElements = view.$(NativeButtonElement.class)
.withAttribute("class", "1", ENDS_WITH)
.all();
Assertions.assertEquals(2, nativeButtonElements.size());

nativeButtonElements = view.$(NativeButtonElement.class)
.withAttribute("id", "shadow-button", NOT_CONTAINS_PREFIX)
.all();
Assertions.assertEquals(8, nativeButtonElements.size());
}

@BrowserTest
void withAttributeContaining() {
openTestURL();
TemplateViewElement view = $(TemplateViewElement.class).waitForFirst();
List<NativeButtonElement> button1s = view.$(NativeButtonElement.class)
.withAttributeContaining("class", "button-1").all();
.withAttributeContaining("id", "light-button-").all();
Assertions.assertEquals(5, button1s.size());
List<NativeButtonElement> allButtons = view.$(NativeButtonElement.class)
.withAttributeContaining("class", "button-shadow-").all();
Assertions.assertEquals(3, allButtons.size());
}

@BrowserTest
void withAttributeContainingWord() {
openTestURL();
TemplateViewElement view = $(TemplateViewElement.class).waitForFirst();
List<NativeButtonElement> button1s = view.$(NativeButtonElement.class)
.withAttributeContainingWord("class", "button-1").all();
Assertions.assertEquals(1, button1s.size());
List<NativeButtonElement> allButtons = view.$(NativeButtonElement.class)
.withAttributeContaining("class", "button").all();
.withAttributeContainingWord("class", "button").all();
Assertions.assertEquals(10, allButtons.size());
}

Expand Down Expand Up @@ -201,7 +237,17 @@ void withoutAttributeContaining() {
TemplateViewElement view = $(TemplateViewElement.class).waitForFirst();

List<NativeButtonElement> allButtons = view.$(NativeButtonElement.class)
.withoutAttributeContaining("class", "button-special-slot").all();
.withoutAttributeContaining("class", "button-").all();
Assertions.assertEquals(1, allButtons.size());
}

@BrowserTest
void withoutAttributeContainingWord() {
openTestURL();
TemplateViewElement view = $(TemplateViewElement.class).waitForFirst();

List<NativeButtonElement> allButtons = view.$(NativeButtonElement.class)
.withoutAttributeContainingWord("class", "button-special-slot").all();
Assertions.assertEquals(9, allButtons.size());
}

Expand Down
Loading

0 comments on commit 971421a

Please sign in to comment.