// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.support.pagefactory;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Mechanism used to locate elements within a document using a series of other lookups.  This class
 * will find all DOM elements that matches each of the locators in sequence, e.g.
 *
 * <pre>
 * driver.findElements(new ByChained(by1, by2))
 * </pre>
 *
 * will find all elements that match <var>by2</var> and appear under an element that matches
 * <var>by1</var>.
 */
public class ByChained extends By implements Serializable {

  private static final long serialVersionUID = 1563769051170172451L;

  private By[] bys;

  public ByChained(By... bys) {
    this.bys = bys;
  }

  @Override
  public WebElement findElement(SearchContext context) {
    List<WebElement> elements = findElements(context);
    if (elements.isEmpty())
      throw new NoSuchElementException("Cannot locate an element using " + toString());
    return elements.get(0);
  }

  @Override
  public List<WebElement> findElements(SearchContext context) {
    if (bys.length == 0) {
      return new ArrayList<>();
    }

    List<WebElement> elems = null;
    for (By by : bys) {
      List<WebElement> newElems = new ArrayList<>();

      if (elems == null) {
        newElems.addAll(by.findElements(context));
      } else {
        for (WebElement elem : elems) {
          newElems.addAll(elem.findElements(by));
        }
      }
      elems = newElems;
    }

    return elems;
  }

  @Override
  public String toString() {
    StringBuilder stringBuilder = new StringBuilder("By.chained(");
    stringBuilder.append("{");

    boolean first = true;
    for (By by : bys) {
      stringBuilder.append((first ? "" : ",")).append(by);
      first = false;
    }
    stringBuilder.append("})");
    return stringBuilder.toString();
  }

}

@@ -1741,24 +1741,286 @@
  context) %7B%0A
+    if (bys.length == 0) %7B%0A      throw new NoSuchElementException(%22Cannot locate an element: %22%0A                                       + %22no Bys were specified in this ByChained%22);%0A    %7D%0A    if (bys.length == 1) %7B%0A      return context.findElement(bys%5B0%5D);%0A    %7D%0A%0A
     List%3CWeb
@@ -2039,16 +2039,23 @@
 ments = 
+bys%5B0%5D.
 findElem
@@ -2077,147 +2077,747 @@
     
-if (elements.isEmpty())%0A      throw new NoSuchElementException(%22Cannot locate an element using %22 + toString());%0A    return elements.get(0);
+for (WebElement element : elements) %7B%0A      WebElement leftMostLeaf = findLeftMostLeaf(element, 1);%0A      if (leftMostLeaf != null) %7B%0A        return leftMostLeaf;%0A      %7D%0A    %7D%0A    throw new NoSuchElementException(%22Cannot locate an element using %22 + toString());%0A  %7D%0A%0A  private WebElement findLeftMostLeaf(WebElement root, int i) %7B%0A    if (i == bys.length) %7B // reached max depth: found!%0A      return root;%0A    %7D%0A%0A    List%3CWebElement%3E children = root.findElements(bys%5Bi%5D); // find elements at depth i%0A    for (WebElement child : children) %7B%0A      WebElement leftMostLeaf = findLeftMostLeaf(child, i + 1);%0A      if (leftMostLeaf != null) %7B%0A        return leftMostLeaf;%0A      %7D%0A    %7D%0A    return null; // wrong path: could not reach max depth
 %0A  %7D

Optimize ByChained#findElement