View Javadoc

1   /*
2       Jameleon Selenium plug-in - A plug-in that uses Selenium (http://www.openqa.org/selenium/) to drive web sites
3       Copyright (C) 2008 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.plugin.selenium.tags;
20  
21  import net.sf.jameleon.function.ContextHelper;
22  import net.sf.jameleon.plugin.selenium.SeleniumFunctionTag;
23  
24  /***
25   * Tries to validate a javascript condition during a certain amount of time. If the validation
26   * isn't successful after timeout the test is considered 'failed'.
27   * This tag is useful in AJAX-like applications where page-contents aren't always directly available
28   * after loading a page.
29   * <br/>
30   * The condition is a (set of) javascript call(s) which can call Selenium functions and webapplication-functions.
31   * To get the window of your application, you can use the JavaScript snippet selenium.browserbot.getCurrentWindow(), and then run your JavaScript in there.
32   * For more information on syntax of Selenium's javascript functions, please refer to Selenium's API documentation.
33   * <br/>
34   * To validate the contents of a HTML element with ID <b>SomeElement</b>:
35   * (&lt;span id="SomeTable" &gt;some value&lt;/span&gt;)
36   * <pre><source>
37   *     &lt;selenium-wait-for-condition
38   *            functionId="Validate HTML element contents with ID 'SomeElement' has value 'some value'"
39   *            condition="var value = selenium.getValue(\"//span[@id='SomeElement']\"); value.match(\"some value\");"&gt;
40   * </source></pre>
41   * @jameleon.function name="selenium-wait-for-condition" type="validation"
42   * @author Sannie Kwakman
43   * @author Christian Hargraves
44   */
45  public class SeleniumWaitForConditionTag extends SeleniumFunctionTag{
46  
47      /***
48       * The condition which should return true before timeout
49       * @jameleon.attribute required="true"
50       */
51      protected String condition;
52  
53      /***
54       * Amount of milliseconds to wait for the condition.
55       * @jameleon.attribute contextName="seleniumWaitForTimeoutSeconds" default=20000
56       */
57      protected int timeout = 20000;
58  
59      public int getTimeoutSeconds(){
60          return ContextHelper.getValueAsIntWithConfig(context, "seleniumWaitForTimeoutSeconds", "seleniumWaitForTimeoutSeconds", timeout);
61      }
62  
63      public void testBlock(){
64  		try
65  		{
66  			session.waitForCondition(condition, ""+getTimeoutSeconds());
67  		}
68  		catch (Exception e)
69  		{
70  		    // An exception is thrown when the condition could not be met.
71  		    fail("selenium-ait-for-condition failed: "+e.getLocalizedMessage());
72  		}
73      }
74  
75  }