Skip to content

Commit 673a17c

Browse files
committed
feat(functions): add .refresh() function
closes #185
1 parent 849ac4d commit 673a17c

5 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/main/java/io/github/seleniumquery/SeleniumQueryObject.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,13 @@ default <T> List<T> map(Function<? super WebElement, ? extends T> mapper) {
619619
return this.stream().map(mapper).collect(Collectors.toList());
620620
}
621621

622+
/**
623+
* Queries -- in-place -- the current seleniumQuery object again. Useful for dealing with fast-changing contexts or
624+
* <b>removing stale element exceptions</b>.
625+
*
626+
* @return The same seleniumQuery object, now updated (with more or less elements).
627+
* @since 0.18.0
628+
*/
629+
SeleniumQueryObject refresh();
630+
622631
}

src/main/java/io/github/seleniumquery/internal/SqObject.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ class SqObject implements SeleniumQueryObject {
7575
this.previous = previous;
7676
}
7777

78-
@Override
78+
@Override
79+
public SeleniumQueryObject refresh() {
80+
this.elements = ListUtils.toImmutableRandomAccessList(driver.findElements(by));
81+
return this;
82+
}
83+
84+
@Override
7985
public final SeleniumQueryFluentFunction waitUntil() {
8086
return new SqFluentFunction(this, new FluentWaitUntil());
8187
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<!--
3+
~ Copyright (c) 2017 seleniumQuery authors
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!--suppress HtmlFormInputWithoutLabel -->
19+
<html>
20+
<body>
21+
<div id="d1">aaa</div>
22+
<div id="d2">bbb</div>
23+
</body>
24+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package endtoend.functions.seleniumquery;
2+
3+
import static io.github.seleniumquery.SeleniumQuery.$;
4+
5+
import org.junit.ClassRule;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.openqa.selenium.JavascriptExecutor;
9+
10+
import io.github.seleniumquery.SeleniumQueryObject;
11+
import testinfrastructure.junitrule.SetUpAndTearDownDriver;
12+
13+
public class RefreshFunctionTest {
14+
15+
@ClassRule @Rule public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver();
16+
17+
@Test
18+
public void refresh__updates__size() {
19+
SeleniumQueryObject sqo = $("div");
20+
sqo.assertThat().size().isEqualTo(2);
21+
((JavascriptExecutor) $.driver().get()).executeScript("document.body.removeChild(document.getElementById('d2'))");
22+
sqo.assertThat().size().isEqualTo(2);
23+
$("div").assertThat().size().isEqualTo(1);
24+
sqo.refresh().assertThat().size().isEqualTo(1);
25+
sqo.assertThat().size().isEqualTo(1);
26+
}
27+
28+
}

src/test/java/testinfrastructure/testdouble/io/github/seleniumquery/SeleniumQueryObjectDummy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ private SeleniumQueryObjectDummy() { }
9494
@Override public SeleniumQueryObject filter(String selector) { throw new PseudoTestDoubleException(); }
9595
@Override public SeleniumQueryObject each(EachFunction function) { throw new PseudoTestDoubleException(); }
9696
@Override public Stream<WebElement> stream() { throw new PseudoTestDoubleException(); }
97+
@Override public SeleniumQueryObject refresh() { throw new PseudoTestDoubleException(); }
9798

9899
}

0 commit comments

Comments
 (0)