001    // Copyright 2006 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.integration;
016    
017    import org.openqa.selenium.server.SeleniumServer;
018    import org.testng.Assert;
019    import org.testng.annotations.AfterClass;
020    import org.testng.annotations.BeforeClass;
021    import org.testng.annotations.Test;
022    
023    import com.thoughtworks.selenium.DefaultSelenium;
024    import com.thoughtworks.selenium.Selenium;
025    
026    /**
027     * Note: If these tests fail with BindException when starting Jetty, it could be Skype. At least on
028     * my system, Skype is listening on localhost:80.
029     */
030    @Test(timeOut = 50000, groups = "integration", sequential=true)
031    public class TestBrowserIssues extends Assert
032    {
033        private static final int JETTY_PORT = 9999;
034        private static final String BASE_URL = "http://localhost:9999/";
035    
036        /** 60 seconds. */
037        public static final String PAGE_LOAD_TIMEOUT = "60000";
038    
039        private Selenium _selenium;
040    
041        private SeleniumServer _server;
042    
043        private JettyRunner _jettyRunner;
044    
045        @BeforeClass
046        public void startupBackground() throws Exception
047        {
048            _jettyRunner = new JettyRunner("/", JETTY_PORT, "src/test-data/app1");
049    
050            _server = new SeleniumServer();
051            
052            _server.start();
053    
054            _selenium = new DefaultSelenium("localhost", SeleniumServer.DEFAULT_PORT, "*firefox", BASE_URL);
055    
056            _selenium.start();
057        }
058    
059        @AfterClass
060        public void shutdownBackground() throws Exception
061        {
062            _selenium.stop();
063            _selenium = null;
064    
065            _server.stop();
066            _server = null;
067    
068            _jettyRunner.stop();
069            _jettyRunner = null;
070        }
071    
072        public void test_issue_1141() throws Exception
073        {
074            openIssuePage("TAPESTRY-1141");
075    
076            String body = _selenium.getBodyText();
077    
078            assertTrue(body.contains("[]"));
079            
080            assertTrue(_selenium.isElementPresent("num"));
081            
082            _selenium.type("num_1", "4");
083            
084            _selenium.click("Submit");
085            
086            waitForInnerHTML("testme", "[4]");        
087            
088            _selenium.type("num_1", "5");
089            
090            submitFromTextfield("num_1");
091            
092            waitForInnerHTML("testme", "[5]");        
093            
094            _selenium.type("num_1", "6");
095            
096            _selenium.type("num_0", "2");
097            
098            submitFromTextfield("num_0");
099            
100            waitForInnerHTML("testme", "[6]");
101        }
102    
103        public void test_issue_1129() throws Exception
104        {
105            openIssuePage("TAPESTRY-1129");
106    
107            String body = _selenium.getBodyText();
108    
109            assertTrue(body.contains("false"));
110    
111            _selenium.click("link=refresh");
112    
113            waitForInnerHTML("flag", "true");
114    
115            assertTrue(_selenium.isElementPresent("TextArea"));
116    
117            assertTrue("".equals(_selenium.getValue("TextArea").trim()));
118        }
119    
120        public void test_issue_1775_a() throws Exception
121        {
122            openIssuePage("TAPESTRY-1775");
123    
124            assertFalse(_selenium.isElementPresent("msg"));
125    
126            _selenium.click("first");
127    
128            waitForInnerHTML("msg", "FIRST");
129    
130            _selenium.click("second");
131    
132            waitForInnerHTML("msg", "SECOND");
133    
134            _selenium.click("nothing");
135    
136            waitForNodeToDisappear("msg");
137        }
138    
139        public void test_issue_1775_b() throws Exception
140        {
141            openIssuePage("TAPESTRY-1775");
142    
143            assertTrue("".equals(_selenium.getText("msg2").trim()));
144    
145            _selenium.click("success");
146    
147            waitForInnerHTML("msg2", "SUCCESS");
148    
149            _selenium.click("cancel");
150    
151            waitForInnerHTML("msg2", "CANCEL");
152    
153            _selenium.click("success");
154    
155            waitForInnerHTML("msg2", "SUCCESS");
156    
157            _selenium.click("refresh");
158    
159            waitForInnerHTML("msg2", "REFRESH");
160    
161            _selenium.click("success");
162    
163            waitForInnerHTML("msg2", "SUCCESS");
164        }
165    
166        public void test_issue_2225() throws Exception
167        {
168            openIssuePage("TAPESTRY-2225");
169    
170            assertFalse(_selenium.isElementPresent("normal"));
171            assertTrue(_selenium.isElementPresent("status"));
172    
173            _selenium.click("asyncR");
174            waitForInnerHTML("status", "asyncREFRESH");
175            
176            _selenium.click("asyncC");
177            waitForInnerHTML("status", "asyncCANCEL");
178    
179            clickAndWait("normR");
180            assertTrue(_selenium.isElementPresent("normal"));
181            assertEquals(getInnerHTML("normal"), "refresh");
182    
183            clickAndWait("normC");
184            assertTrue(_selenium.isElementPresent("normal"));
185            assertEquals(getInnerHTML("normal"), "cancel");
186        }
187    
188        private void openIssuePage(String issue)
189        {
190            _selenium.open(BASE_URL);
191    
192            clickAndWait("link=" + issue);
193    
194            assertTrue(_selenium.getTitle().contains(issue));
195        }
196    
197        private String getInnerHTML(String elm)
198        {
199            return _selenium.getEval("selenium.browserbot.getCurrentWindow().document.getElementById('"
200                + elm + "').innerHTML");
201        }
202    
203        private void waitForInnerHTML(String elm, String content)
204        {
205            _selenium.waitForCondition("selenium.browserbot.getCurrentWindow().document.getElementById('"
206                    + elm + "').innerHTML=='" + content + "'","6000");                
207        }
208    
209        private void waitForNodeToDisappear(String elm)
210        {
211            _selenium.waitForCondition("!selenium.browserbot.getCurrentWindow().document.getElementById('"
212                    + elm + "')","6000");
213        }
214        
215        private void submitFromTextfield(String field)
216        {
217            _selenium.keyPress(field, "13");
218            //_selenium.fireEvent(field, "command");
219            /*_selenium.click(field);
220            _selenium.keyDown(field, "13");
221            _selenium.keyPress(field, "0");
222            _selenium.keyUp(field, "13");*/
223        }
224    
225        private void clickAndWait(String link)
226        {
227            _selenium.click(link);
228            _selenium.waitForPageToLoad(PAGE_LOAD_TIMEOUT);
229        }
230    }