View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.qa.glnm.selectors;
21  
22  import java.util.Collection;
23  
24  import org.openqa.selenium.By;
25  
26  import com.galenframework.specs.page.Locator;
27  
28  import io.wcm.qa.glnm.selectors.base.NestedSelector;
29  import io.wcm.qa.glnm.selectors.base.Selector;
30  
31  /**
32   *  {@link io.wcm.qa.glnm.selectors.base.NestedSelector} implementation immediately setting {@link org.openqa.selenium.By}, {@link com.galenframework.specs.page.Locator}, parent and children values on
33   * instantiation. This avoids harder to trace errors inherent in lazy evaluation used in other implementations.
34   *
35   * @since 1.0.0
36   */
37  public class FixedValueNestedSelector extends FixedValueSelector implements NestedSelector {
38  
39    private Selector absolute;
40    private Collection<NestedSelector> children;
41    private NestedSelector parent;
42    private Selector relative;
43  
44    /**
45     * Uses the element name, selector CSS, {@link org.openqa.selenium.By}, {@link com.galenframework.specs.page.Locator}, parent and children from selector.
46     *
47     * @param selector to extract values from
48     */
49    public FixedValueNestedSelector(NestedSelector selector) {
50      this(selector.elementName(), selector.asString(), selector.asBy(), selector.asLocator(), selector.asAbsolute(), selector.asRelative(),
51          selector.getParent(), selector.getChildren());
52    }
53  
54    /**
55     * Uses the parameters as values.
56     *
57     * @param elementName to use for selector
58     * @param css to use for selector
59     * @param by to use for selector
60     * @param locator to use for selector
61     * @param absolute absolute version of selector
62     * @param relative relative version of selector
63     * @param parent to use for selector
64     * @param children to use for selector
65     */
66    public FixedValueNestedSelector(String elementName, String css, By by, Locator locator, Selector absolute,
67        Selector relative, NestedSelector parent, Collection<NestedSelector> children) {
68      super(elementName, css, by, locator);
69      this.absolute = absolute;
70      this.children = children;
71      this.parent = parent;
72      this.relative = relative;
73    }
74  
75    /** {@inheritDoc} */
76    @Override
77    public Selector asAbsolute() {
78      return absolute;
79    }
80  
81    /** {@inheritDoc} */
82    @Override
83    public Selector asRelative() {
84      return relative;
85    }
86  
87    /** {@inheritDoc} */
88    @Override
89    public Collection<NestedSelector> getChildren() {
90      return children;
91    }
92  
93    /** {@inheritDoc} */
94    @Override
95    public NestedSelector getParent() {
96      return parent;
97    }
98  
99    /** {@inheritDoc} */
100   @Override
101   public boolean hasChildren() {
102     boolean hasNoChildren = ((children == null) || (children.isEmpty()));
103     return !hasNoChildren;
104   }
105 
106   /** {@inheritDoc} */
107   @Override
108   public boolean hasParent() {
109     return parent != null;
110   }
111 
112 }