All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.support.pagefactory.ByChained Maven / Gradle / Ivy

Go to download

Selenium automates browsers. That's it! What you do with that power is entirely up to you.

There is a newer version: 4.19.1
Show newest version
// 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.
 *
 * 
 * driver.findElements(new ByChained(by1, by2))
 * 
* * will find all elements that match by2 and appear under an element that matches * by1. */ 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 elements = findElements(context); if (elements.isEmpty()) throw new NoSuchElementException("Cannot locate an element using " + toString()); return elements.get(0); } @Override public List findElements(SearchContext context) { if (bys.length == 0) { return new ArrayList<>(); } List elems = null; for (By by : bys) { List 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(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy