Skip to content

Commit 9305f37

Browse files
authored
Add synchronization to tab page resolver (#300)
1 parent 797c527 commit 9305f37

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.nordstrom.automation.selenium.examples;
22

3+
import org.openqa.selenium.TimeoutException;
4+
import org.openqa.selenium.WebElement;
5+
6+
import com.nordstrom.automation.selenium.AbstractSeleniumConfig.WaitType;
37
import com.nordstrom.automation.selenium.exceptions.UnresolvedContainerTypeException;
48
import com.nordstrom.automation.selenium.interfaces.ContainerResolver;
5-
import com.nordstrom.automation.selenium.model.RobustWebElement;
9+
import com.nordstrom.automation.selenium.support.Coordinators;
610

711
/**
812
* This class implements the container resolver for the tab pages opened by the "Open A/B Tab" button on the 'Example'
@@ -11,17 +15,19 @@
1115
public class TabPageResolver implements ContainerResolver<TabPage> {
1216
@Override
1317
public TabPage resolve(TabPage container) {
14-
RobustWebElement element = container.findOptional(TabPage.Using.HEADING);
15-
if (element.hasReference()) {
18+
try {
19+
WebElement element = container.getWait(WaitType.PAGE_LOAD)
20+
.until(Coordinators.presenceOfElementLocated(TabPage.Using.HEADING.locator()));
1621
String pageContent = element.getText();
1722
if (pageContent.equals(TabPageA.EXPECT_CONTENT)) {
1823
return new TabPageA(container.getWrappedDriver());
1924
}
2025
if (pageContent.equals(TabPageB.EXPECT_CONTENT)) {
2126
return new TabPageB(container.getWrappedDriver());
2227
}
23-
throw new UnresolvedContainerTypeException("Unsupported tab page heading: " + pageContent);
28+
throw new UnresolvedContainerTypeException("Unsupported tab page heading: " + pageContent);
29+
} catch (TimeoutException e) {
30+
throw new UnresolvedContainerTypeException("Tab page heading not found");
2431
}
25-
throw new UnresolvedContainerTypeException("Tab page heading not found");
2632
}
2733
}

src/main/java/com/nordstrom/automation/selenium/model/ComponentContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public interface ByEnum {
106106
* Constructor for component container
107107
*
108108
* @param context container search context
109-
* @param parent container parent (may be {@code null} for {@link Page} objects
109+
* @param parent container parent (may be {@code null} for {@link Page} objects)
110110
*/
111111
public ComponentContainer(final SearchContext context, final ComponentContainer parent) {
112112
Objects.requireNonNull(context, "[context] must be non-null");
@@ -370,7 +370,7 @@ public RobustWebElement findOptional(final ByEnum constant) {
370370
* @return robust web element
371371
*/
372372
public RobustWebElement findOptional(final By by) {
373-
return (RobustWebElement) RobustElementFactory.getElement(this, by, RobustElementWrapper.OPTIONAL);
373+
return RobustElementFactory.getOptional(this, by);
374374
}
375375

376376
/**

src/main/java/com/nordstrom/automation/selenium/model/RobustElementFactory.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,17 @@ public static WebElement getElement(final WrapsContext context, final By locator
248248
public static WebElement getElement(final WrapsContext context, final By locator, final int index) {
249249
return makeRobustElement(null, context, locator, index);
250250
}
251+
252+
/**
253+
* Get a wrapped reference to the first element matching the specified locator.
254+
* <p>
255+
* <b>NOTE</b>: Use {@link RobustWebElement#hasReference()} to determine if a valid reference was acquired.
256+
*
257+
* @param context element search context
258+
* @param locator element locator
259+
* @return optional robust element in context that matches the locator
260+
*/
261+
public static RobustWebElement getOptional(final WrapsContext context, final By locator) {
262+
return (RobustWebElement) makeRobustElement(null, context, locator, RobustElementWrapper.OPTIONAL);
263+
}
251264
}

src/main/java/com/nordstrom/automation/selenium/utility/SearchContextUtils.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,30 +159,28 @@ public static String quoteSelector(final String selector) {
159159
* @return containing {@link ComponentContainer}
160160
*/
161161
public static ComponentContainer getContainingContext(final SearchContext context) {
162-
SearchContext unwrapped = unwrapContext(context);
163-
while (unwrapped != null) {
164-
if (unwrapped instanceof ComponentContainer) {
165-
return (ComponentContainer) unwrapped;
166-
}
167-
unwrapped = unwrapContext(unwrapped);
162+
SearchContext candidate = context;
163+
while (candidate instanceof RobustWebElement) {
164+
candidate = (SearchContext) ((RobustWebElement) candidate).getContext();
168165
}
169-
return null;
166+
if (candidate instanceof ComponentContainer) {
167+
return (ComponentContainer) candidate;
168+
}
169+
throw new UnsupportedOperationException("Failed getting containing context");
170170
}
171171

172172
/**
173-
* Get the parent context for the specified search context.
173+
* Unwrap the specified search context.
174174
*
175175
* @param context target search context
176-
* @return parent {@link SearchContext}
176+
* @return unwrapped {@link SearchContext}
177177
*/
178178
public static SearchContext unwrapContext(final SearchContext context) {
179-
if (context instanceof RobustWebElement) {
180-
return (SearchContext) ((RobustWebElement) context).getContext();
181-
}
182-
if (context instanceof WrapsContext) {
183-
return ((WrapsContext) context).getWrappedContext();
179+
SearchContext unwrapped = context;
180+
while (unwrapped instanceof WrapsContext) {
181+
unwrapped = ((WrapsContext) context).getWrappedContext();
184182
}
185-
return null;
183+
return unwrapped;
186184
}
187185

188186
/**

0 commit comments

Comments
 (0)