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 corresponding element is found under web element. 050 * 051 * @since 8.4 052 */ 053 public static boolean hasChild(WebElement webElement, By by) { 054 try { 055 webElement.findElement(by); 056 return true; 057 } catch (NoSuchElementException e) { 058 return false; 059 } 060 } 061 062 /** 063 * Returns true if {@code text} is present in the element retrieved with the given method. 064 * 065 * @since 5.7.3 066 */ 067 public static boolean isTextPresent(By by, String text) { 068 return isTextPresent(AbstractTest.driver.findElement(by), text); 069 } 070 071 /** 072 * Returns true if {@code text} is present in the given element. 073 * 074 * @since 5.7.3 075 */ 076 public static boolean isTextPresent(WebElement element, String text) { 077 return element.getText().contains(text); 078 } 079 080}