Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 366ce08

Browse files
committed
Added a method to change the value of any attribute of any page fragment using JavaScript
1 parent fca8849 commit 366ce08

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/pagefragments/PageFragment.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
import org.openqa.selenium.NoSuchElementException;
77
import org.openqa.selenium.WebElement;
88

9+
import info.novatec.testit.webtester.adhoc.AdHocFinder;
910
import info.novatec.testit.webtester.browser.Browser;
11+
import info.novatec.testit.webtester.browser.operations.JavaScript;
12+
import info.novatec.testit.webtester.internal.OffersAdHocFinding;
13+
import info.novatec.testit.webtester.internal.OffersBrowserGetter;
14+
import info.novatec.testit.webtester.internal.OffersPageCreation;
1015
import info.novatec.testit.webtester.internal.annotations.ReturnsName;
1116
import info.novatec.testit.webtester.internal.annotations.ReturnsWebElement;
17+
import info.novatec.testit.webtester.pagefragments.annotations.As;
1218
import info.novatec.testit.webtester.pagefragments.annotations.Cached;
1319
import info.novatec.testit.webtester.pagefragments.annotations.IdentifyUsing;
1420
import info.novatec.testit.webtester.pagefragments.annotations.Mark;
15-
import info.novatec.testit.webtester.pagefragments.annotations.As;
1621
import info.novatec.testit.webtester.pagefragments.annotations.Named;
17-
import info.novatec.testit.webtester.adhoc.AdHocFinder;
18-
import info.novatec.testit.webtester.internal.OffersAdHocFinding;
19-
import info.novatec.testit.webtester.internal.OffersBrowserGetter;
20-
import info.novatec.testit.webtester.internal.OffersPageCreation;
2122

2223

2324
/**
@@ -140,6 +141,27 @@ default Optional<String> getAttribute(String attributeName) {
140141
return Optional.ofNullable(webElement().getAttribute(attributeName));
141142
}
142143

144+
/**
145+
* Sets the value of an attribute of this {@link PageFragment} using JavaScript.
146+
* <p>
147+
* <b>Example:</b>
148+
* <pre>
149+
* // will change the value of a text field to 'foo'
150+
* textField.setAttribute("value", "foo");
151+
* </pre>
152+
*
153+
* @param attributeName the name of the attribute to set
154+
* @param value the value to set the attribute to
155+
* @see PageFragment
156+
* @see JavaScript
157+
* @since 2.0
158+
*/
159+
default void setAttribute(String attributeName, String value) {
160+
String escapedValue = StringUtils.replace(value, "\"", "\\\"");
161+
String script = "arguments[0]." + attributeName + " = \"" + escapedValue + "\"";
162+
getBrowser().javaScript().execute(script, this, value);
163+
}
164+
143165
/**
144166
* Gets the optional value of the given CSS property as a string.
145167
* If the CSS property is not set, an empty optional is returned.

webtester-core/src/test/java/integration/pagefragments/PageFragmentIntegrationTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import integration.BaseIntegrationTest;
1515

1616
import info.novatec.testit.webtester.pagefragments.PageFragment;
17+
import info.novatec.testit.webtester.pagefragments.TextField;
1718
import info.novatec.testit.webtester.pagefragments.annotations.IdentifyUsing;
1819
import info.novatec.testit.webtester.pagefragments.annotations.Named;
1920
import info.novatec.testit.webtester.pages.Page;
@@ -114,6 +115,22 @@ public void nonExistingAttributesAreReturnedAsEmptyOptionals() {
114115
assertThat(value).isEmpty();
115116
}
116117

118+
/* set attribute */
119+
120+
@Test
121+
public void setAttribute() {
122+
TextField textField = page.textField();
123+
textField.setAttribute("value", "hello world!");
124+
assertThat(textField.getText()).isEqualTo("hello world!");
125+
}
126+
127+
@Test
128+
public void setAttributeWithCharactersThatNeedEscaping() {
129+
TextField textField = page.textField();
130+
textField.setAttribute("value", "hello \"world!\"");
131+
assertThat(textField.getText()).isEqualTo("hello \"world!\"");
132+
}
133+
117134
/* get CSS values */
118135

119136
@Test
@@ -240,6 +257,9 @@ private interface TestPage extends Page {
240257
@IdentifyUsing("#disabled")
241258
PageFragment disabled();
242259

260+
@IdentifyUsing("#textField")
261+
TextField textField();
262+
243263
@IdentifyUsing("#pageFragment")
244264
Stream<PageFragment> pageFragments();
245265

webtester-core/src/test/resources/html/pagefragments/pageFragment.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ <h3>This page contains elements for testing the functionality of the PageFragmen
5555
<input id="disabled" disabled>
5656
</td>
5757
</tr>
58+
<tr>
59+
<td>A input field for changing attributes</td>
60+
<td>
61+
<input id="textField" type="text">
62+
</td>
63+
</tr>
5864
</tbody>
5965
</table>
6066

0 commit comments

Comments
 (0)