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

io.webfolder.cdp.session.Dom Maven / Gradle / Ivy

There is a newer version: 3.0.15
Show newest version
/**
 * cdp4j Commercial License
 *
 * Copyright 2017, 2019 WebFolder OÜ
 *
 * Permission  is hereby  granted,  to "____" obtaining  a  copy of  this software  and
 * associated  documentation files  (the "Software"), to deal in  the Software  without
 * restriction, including without limitation  the rights  to use, copy, modify,  merge,
 * publish, distribute  and sublicense  of the Software,  and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  IMPLIED,
 * INCLUDING  BUT NOT  LIMITED  TO THE  WARRANTIES  OF  MERCHANTABILITY, FITNESS  FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL  THE AUTHORS  OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package io.webfolder.cdp.session;

import static io.webfolder.cdp.session.Option.TYPE_TOKEN;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.lang.Math.floor;
import static java.lang.String.format;
import static java.lang.String.valueOf;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toList;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import com.google.gson.Gson;

import io.webfolder.cdp.command.DOM;
import io.webfolder.cdp.exception.ElementNotFoundException;
import io.webfolder.cdp.type.dom.BoxModel;
import io.webfolder.cdp.type.runtime.CallFunctionOnResult;
import io.webfolder.cdp.type.runtime.ExceptionDetails;
import io.webfolder.cdp.type.runtime.PropertyDescriptor;
import io.webfolder.cdp.type.runtime.RemoteObject;
import io.webfolder.cdp.type.util.Point;

/**
 * Provides the interfaces for the Document Object Model (DOM).
 */
public interface Dom {

    /**
     * textContent property represents the text content of a node and its descendants.
     * 
     * textContent returns null if the element is a document, a document type, or a notation.
     * To grab all of the text and CDATA data for the whole document, one could use document.documentElement.textContent.
     * 
     * @param selector css or xpath selector
     * 
     * @return textContent returns the concatenation of the textContent property value of every child node,
* excluding comments and processing instruction nodes. This is an empty string if the node has no children. */ default String getText(final String selector) { return getText(selector, Constant.EMPTY_ARGS); } /** * textContent property represents the text content of a node and its descendants. * * textContent returns null if the element is a document, a document type, or a notation. * To grab all of the text and CDATA data for the whole document, one could use document.documentElement.textContent. * * @param selector css or xpath selector * @param args format string * * @return textContent returns the concatenation of the textContent property value of every child node,
* excluding comments and processing instruction nodes. This is an empty string if the node has no children. */ default String getText(final String selector, final Object ...args) { return (String) getThis().getProperty(selector, "textContent", args); } /** * The HTMLInputElement.select() method selects all the text in a <textarea> element
* or an <input> element with a text field. * * @param selector css or xpath selector * * @return this */ default Session selectInputText(final String selector) { return selectInputText(selector, Constant.EMPTY_ARGS); } /** * The HTMLInputElement.select() method selects all the text in a <textarea> element
* or an <input> element with a text field. * * @param selector css or xpath selector * @param selector format string * * @return this */ default Session selectInputText(final String selector, final Object ...args) { getThis().logEntry("selectInputText", format(selector, args)); String objectId = getThis().getObjectId(selector, args); if (objectId == null) { throw new ElementNotFoundException(format(selector, args)); } CallFunctionOnResult functionResult = getThis() .getCommand().getRuntime() .callFunctionOn("function() { this.select(); }", objectId, null, null, null, null, null, null, null, null); if (functionResult != null) { RemoteObject result = functionResult.getResult(); if (result != null) { getThis().releaseObject(result.getObjectId()); } ExceptionDetails exceptionDetails = functionResult.getExceptionDetails(); if (exceptionDetails != null) { RemoteObject exception = exceptionDetails.getException(); if (exception != null) { getThis().error(exception.getDescription()); } } } getThis().releaseObject(objectId); return getThis(); } /** * The HTMLElement.focus() method sets focus on the specified element, if it can be focused. * * @param selector css or xpath selector * * @return this */ default Session focus(final String selector) { return focus(null, selector); } /** * The HTMLElement.focus() method sets focus on the specified element, if it * can be focused. * * @param selector * css or xpath selector * @param contextId * Context id of the frame * @return this */ default Session focus(final Integer contextId, final String selector) { return focus(contextId, selector, Constant.EMPTY_ARGS); } /** * The HTMLElement.focus() method sets focus on the specified element, if it * can be focused. * * @param selector * css or xpath selector * @param args * format string * @param contextId * Context id of the frame * @return this */ default Session focus(final Integer contextId, final String selector, final Object... args) { getThis().logEntry("focus", format(selector, args)); Integer nodeId = getThis().getNodeId(contextId, selector, contextId, args); if (nodeId == null || Constant.EMPTY_NODE_ID.equals(nodeId)) { throw new ElementNotFoundException(format(selector, args)); } DOM dom = getThis().getCommand().getDOM(); dom.focus(nodeId, null, null); return getThis(); } /** * The HTMLSelectElement.selectedIndex() is a int that reflects the index of the first selected <option> element. * The selectedIndex property returns -1 if a select object does not contain any selected items. * * @param selector css or xpath selector * * * @return selected index of the first <option> element. */ default int getSelectedIndex(final String selector) { return getSelectedIndex(selector, Constant.EMPTY_ARGS); } /** * The HTMLSelectElement.selectedIndex() is a int that reflects the index of the first selected <option> element. * The selectedIndex property returns -1 if a select object does not contain any selected items. * * @param selector css or xpath selector * @param args format string * * @return selected index of the first <option> element. */ default int getSelectedIndex(final String selector, final Object ...args) { String objectId = getThis().getObjectId(selector, args); if (objectId == null) { throw new ElementNotFoundException(format(selector, args)); } Double selectedIndex = (Double) getThis().getPropertyByObjectId(objectId, "selectedIndex"); getThis().releaseObject(objectId); if (selectedIndex == null) { selectedIndex = -1D; } getThis().logExit("getSelectedIndex", format(selector, args), selectedIndex.intValue()); return selectedIndex.intValue(); } /** * Set selectedIndex of <option> element. * * When you set the selectedIndex property, the display of the select object updates immediately. * This method most useful when used with select objects that support selecting only one item at a time. * Use {@link #setSelectedOptions(String, List)} method if the multiple attribute is specified for a <select> element. * * @param selector css or xpath selector * @param index he index of the first selected <option> element. * * @return this */ default Session setSelectedIndex(final String selector, final int index) { return setSelectedIndex(selector, index, Constant.EMPTY_ARGS); } /** * Set selectedIndex of <option> element. * * When you set the selectedIndex property, the display of the select object updates immediately. * This method is most useful when used with select objects that support selecting only one item at a time. * Use {@link #setSelectedOptions(String, List, Object...)} method if the multiple attribute is specified for a <select> element. * * @param selector css or xpath selector * @param index he index of the first selected <option> element. * * @return this */ default Session setSelectedIndex( final String selector, final int index, final Object ...args) { if (index < -1) { return getThis(); } if (index != -1) { getThis().logEntry("setSelectedIndex", format(selector, args) + "\", \"" + index); } String objectId = getThis().getObjectId(selector, args); if (objectId == null) { throw new ElementNotFoundException(format(selector, args)); } CallFunctionOnResult result = getThis().getCommand().getRuntime().callFunctionOn( format("function() { this.selectedIndex = %d }", index), objectId, null, null, null, null, null, null, null, null); if (result != null && result.getResult() != null) { getThis().releaseObject(result.getResult().getObjectId()); } getThis().releaseObject(objectId); return getThis(); } /** * The list of options for a <select> element consists of all the option element children of the select element, * and all the <option> element children of all the <optgroup> element children of the <select> element. * * @param css selector * * @return list of HTML <option> elements (in document order). */ default List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy