-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added escaping of values when using IdStartsWith and IdEndsWith ByPro…
…ducers. This is necessary because we are using a CSS Selector and there are issues with special characters which leads to strange behavior.
- Loading branch information
Showing
10 changed files
with
121 additions
and
12 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...ain/java/info/novatec/testit/webtester/pagefragments/identification/CssSelectorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package info.novatec.testit.webtester.pagefragments.identification; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
/** | ||
* This class includes utility methods for working with CSS Selectors. | ||
* | ||
* @since 2.0.4 | ||
*/ | ||
public final class CssSelectorUtils { | ||
|
||
/** These are all special characters in CSS who need escaping. */ | ||
private static final Character[] SPECIAL_CHARS = | ||
{ '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', | ||
'\\', ']', '^', '`', '{', '|', '}', '~' }; | ||
/** See {@link #SPECIAL_CHARS}. */ | ||
private static final Set<Character> SPECIAL_CHARS_SET = Arrays.stream(SPECIAL_CHARS).collect(Collectors.toSet()); | ||
|
||
/** | ||
* Escape the given value by prefixing all {@link #SPECIAL_CHARS} with {@code \}. | ||
* <p> | ||
* This is necessary for all values used by CSS Selectors. For example when matching on the value of an attribute. | ||
* | ||
* @since 2.0.4 | ||
*/ | ||
public static String escape(String value) { | ||
StringBuilder escapedValue = new StringBuilder(value.length() * 2); | ||
for (Character c : value.toCharArray()) { | ||
if (SPECIAL_CHARS_SET.contains(c)) { | ||
escapedValue.append('\\'); | ||
} | ||
escapedValue.append(c); | ||
} | ||
return escapedValue.toString(); | ||
} | ||
|
||
private CssSelectorUtils() { | ||
// utility class | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...java/info/novatec/testit/webtester/pagefragments/identification/CssSelectorUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package info.novatec.testit.webtester.pagefragments.identification; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.Test; | ||
|
||
|
||
public class CssSelectorUtilsTest { | ||
|
||
@Test | ||
public void realisticExample() { | ||
assertThat(CssSelectorUtils.escape("table:form:selection[0]")).isEqualTo("table\\:form\\:selection\\[0\\]"); | ||
} | ||
|
||
@Test | ||
public void allRelevantSpecialCharactersAreEscaped() { | ||
SoftAssertions softly = new SoftAssertions(); | ||
softly.assertThat(CssSelectorUtils.escape("!")).isEqualTo("\\!"); | ||
softly.assertThat(CssSelectorUtils.escape("\"")).isEqualTo("\\\""); | ||
softly.assertThat(CssSelectorUtils.escape("#")).isEqualTo("\\#"); | ||
softly.assertThat(CssSelectorUtils.escape("$")).isEqualTo("\\$"); | ||
softly.assertThat(CssSelectorUtils.escape("%")).isEqualTo("\\%"); | ||
softly.assertThat(CssSelectorUtils.escape("&")).isEqualTo("\\&"); | ||
softly.assertThat(CssSelectorUtils.escape("'")).isEqualTo("\\'"); | ||
softly.assertThat(CssSelectorUtils.escape("(")).isEqualTo("\\("); | ||
softly.assertThat(CssSelectorUtils.escape(")")).isEqualTo("\\)"); | ||
softly.assertThat(CssSelectorUtils.escape("*")).isEqualTo("\\*"); | ||
softly.assertThat(CssSelectorUtils.escape("+")).isEqualTo("\\+"); | ||
softly.assertThat(CssSelectorUtils.escape(",")).isEqualTo("\\,"); | ||
softly.assertThat(CssSelectorUtils.escape("-")).isEqualTo("\\-"); | ||
softly.assertThat(CssSelectorUtils.escape(".")).isEqualTo("\\."); | ||
softly.assertThat(CssSelectorUtils.escape("/")).isEqualTo("\\/"); | ||
softly.assertThat(CssSelectorUtils.escape(":")).isEqualTo("\\:"); | ||
softly.assertThat(CssSelectorUtils.escape(";")).isEqualTo("\\;"); | ||
softly.assertThat(CssSelectorUtils.escape("<")).isEqualTo("\\<"); | ||
softly.assertThat(CssSelectorUtils.escape("=")).isEqualTo("\\="); | ||
softly.assertThat(CssSelectorUtils.escape(">")).isEqualTo("\\>"); | ||
softly.assertThat(CssSelectorUtils.escape("?")).isEqualTo("\\?"); | ||
softly.assertThat(CssSelectorUtils.escape("@")).isEqualTo("\\@"); | ||
softly.assertThat(CssSelectorUtils.escape("[")).isEqualTo("\\["); | ||
softly.assertThat(CssSelectorUtils.escape("\\")).isEqualTo("\\\\"); | ||
softly.assertThat(CssSelectorUtils.escape("]")).isEqualTo("\\]"); | ||
softly.assertThat(CssSelectorUtils.escape("^")).isEqualTo("\\^"); | ||
softly.assertThat(CssSelectorUtils.escape("`")).isEqualTo("\\`"); | ||
softly.assertThat(CssSelectorUtils.escape("{")).isEqualTo("\\{"); | ||
softly.assertThat(CssSelectorUtils.escape("|")).isEqualTo("\\|"); | ||
softly.assertThat(CssSelectorUtils.escape("}")).isEqualTo("\\}"); | ||
softly.assertThat(CssSelectorUtils.escape("~")).isEqualTo("\\~"); | ||
softly.assertAll(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters