001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.testsupport; 021 022 import org.apache.geronimo.testsupport.TestSupport; 023 024 import com.thoughtworks.selenium.Selenium; 025 026 import org.openqa.selenium.server.SeleniumServer; 027 028 import org.testng.annotations.BeforeSuite; 029 import org.testng.annotations.AfterSuite; 030 031 /** 032 * Provides support for Selenium test cases. 033 * 034 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 035 */ 036 public class SeleniumTestSupport 037 extends TestSupport 038 { 039 protected static ExtendedSelenium selenium; 040 041 protected ExtendedSelenium createSeleniumClient(String url) throws Exception { 042 super.setUp(); 043 044 if (url == null) { 045 // url = "http://localhost:" + SeleniumServer.DEFAULT_PORT; 046 // post 1.0-beta-1 builds don't define DEFAULT_PORT 047 url = "http://localhost:4444"; 048 } 049 050 String browser = System.getProperty("browser", "*firefox"); 051 052 log.info("Creating Selenium client for URL: " + url + ", Browser: " + browser); 053 054 //ExtendedSelenium selenium = new ExtendedSelenium("localhost", SeleniumServer.DEFAULT_PORT, "*firefox", url); 055 ExtendedSelenium selenium = new ExtendedSelenium("localhost", 4444, browser, url); 056 057 return selenium; 058 } 059 060 protected void ensureSeleniumClientInitialized() { 061 if (selenium == null) { 062 throw new IllegalStateException("Selenium client was not initalized"); 063 } 064 } 065 066 @BeforeSuite 067 protected synchronized void startSeleniumClient() throws Exception { 068 log.info("Starting Selenium client"); 069 070 selenium = createSeleniumClient("http://localhost:8080/"); 071 selenium.start(); 072 } 073 074 @AfterSuite 075 protected synchronized void stopSeleniumClient() throws Exception { 076 ensureSeleniumClientInitialized(); 077 078 log.info("Stopping Selenium client"); 079 080 selenium.stop(); 081 } 082 083 protected void waitForPageLoad() throws Exception { 084 ensureSeleniumClientInitialized(); 085 086 selenium.waitForPageToLoad("30000"); 087 } 088 } 089