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) 2006 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;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import net.sf.jameleon.function.FunctionTag;
25  import net.sf.jameleon.util.JameleonUtility;
26  
27  import com.thoughtworks.selenium.Selenium;
28  
29  public abstract class SeleniumFunctionTag extends FunctionTag{
30  
31      protected SeleniumSessionTag sessionTag;
32      protected Selenium session;
33  
34      /***
35       * Gets the references of the session and the test results from the parent TestCase
36       * This method gets called once all attributes are set from the macro language. Any required
37       * setup should go here.
38       */
39      public void setupEnvironment() {
40          super.setupEnvironment();
41          Object obj = findAncestorWithClass(SeleniumSessionTag.class);
42          if (obj instanceof SeleniumSessionTag) {
43              sessionTag = (SeleniumSessionTag) obj;
44          } else {
45              throw new ClassCastException("Can only execute an Selenium function tags under the Selenium session tag ( selenium-session )! " +
46                      "Please change the session tag surrounding function point " + getClass().getName());
47          }
48          session = sessionTag.getSession();
49      }
50  
51      public void store(String fName, int event) throws IOException{
52           File stateFile = null;
53           if ( session != null ) {
54               String html = session.getHtmlSource();
55               String filename = fName+".html";
56               if ( html != null && html.length() > 0 ) {
57                   stateFile = new File(filename);
58                   JameleonUtility.recordResultsToFile(stateFile, "<html>"+html+"</html>");
59                   getFunctionResults().setErrorFile(stateFile);
60               }
61           }
62      }
63  
64  }