001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.functionaltests;
020
021import org.openqa.selenium.By;
022import org.openqa.selenium.NoSuchElementException;
023import org.openqa.selenium.WebElement;
024
025/**
026 * Helper class providing assert methods on WebElement conditions.
027 *
028 * @since 5.9.2
029 */
030public class Assert {
031
032    /**
033     * Returns true if corresponding element is found in the test page.
034     *
035     * @since 5.7
036     */
037    public static boolean hasElement(By by) {
038        boolean present;
039        try {
040            AbstractTest.driver.findElement(by);
041            present = true;
042        } catch (NoSuchElementException e) {
043            present = false;
044        }
045        return present;
046    }
047
048    /**
049     * Returns true if {@code text} is present in the element retrieved with the given method.
050     *
051     * @since 5.7.3
052     */
053    public static boolean isTextPresent(By by, String text) {
054        return isTextPresent(AbstractTest.driver.findElement(by), text);
055    }
056
057    /**
058     * Returns true if {@code text} is present in the given element.
059     *
060     * @since 5.7.3
061     */
062    public static boolean isTextPresent(WebElement element, String text) {
063        return element.getText().contains(text);
064    }
065
066}