1   /*
2    * Copyright 2007 united internet (unitedinternet.com) Robert Zimmermann
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package com.unitedinternet.portal.selenium.utils.logging.integrationtest;
18  
19  import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertEquals;
20  import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue;
21  
22  import java.io.BufferedWriter;
23  import java.io.File;
24  import java.io.IOException;
25  import java.text.ParseException;
26  
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import com.thoughtworks.selenium.HttpCommandProcessor;
32  import com.thoughtworks.selenium.Wait;
33  import com.unitedinternet.portal.selenium.utils.logging.HtmlResultFormatter;
34  import com.unitedinternet.portal.selenium.utils.logging.LoggingCommandProcessor;
35  import com.unitedinternet.portal.selenium.utils.logging.LoggingDefaultSelenium;
36  import com.unitedinternet.portal.selenium.utils.logging.LoggingResultsFormatter;
37  import com.unitedinternet.portal.selenium.utils.logging.LoggingSelenium;
38  import com.unitedinternet.portal.selenium.utils.logging.LoggingUtils;
39  import com.unitedinternet.portal.selenium.utils.logging.LoggingWait;
40  
41  public class LoggingSeleniumWaitTimedOutSample {
42      protected LoggingSelenium selenium;
43  
44      private BufferedWriter loggingWriter;
45  
46      private static final String RESULT_FILE_ENCODING = "UTF-8";
47  
48      private static final String DEFAULT_TIMEOUT = "30000";
49  
50      private static final long WAIT_TIMEOUT = 3000;
51  
52      private static final String OPENQA_URL = "http://openqa.org";
53  
54      private static final String SCREENSHOT_PATH = "screenshots";
55  
56      private final String RESULTS_BASE_PATH = "target" + File.separator + "loggingResults";
57  
58      private String resultsPath = new File(RESULTS_BASE_PATH).getAbsolutePath();
59  
60      private String screenshotsResultsPath = new File(RESULTS_BASE_PATH + File.separator + SCREENSHOT_PATH).getAbsolutePath();
61  
62      @Before
63      public void setUp() {
64          if (!new File(screenshotsResultsPath).exists()) {
65              new File(screenshotsResultsPath).mkdirs();
66          }
67          // to keep every result use a timestamped filename like this
68          // final String resultHtmlFileName = resultsPath
69          // + File.separator
70          // + "autorunResult"
71          // + LoggingUtils.timeStampForFileName()
72          // + ".html";
73          final String resultHtmlFileName = resultsPath + File.separator + "sampleResultWaitTimedOut.html";
74          System.err.println("resultHtmlFileName=" + resultHtmlFileName);
75  
76          loggingWriter = LoggingUtils.createWriter(resultHtmlFileName, LoggingSeleniumWaitTimedOutSample.RESULT_FILE_ENCODING,
77                  true);
78  
79          LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter,
80                  LoggingSeleniumWaitTimedOutSample.RESULT_FILE_ENCODING);
81          htmlFormatter.setScreenShotBaseUri(LoggingSeleniumWaitTimedOutSample.SCREENSHOT_PATH + "/"); // has to be "/" as this
82                                                                                                          // is a URI
83          htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
84          LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*chrome",
85                  OPENQA_URL), htmlFormatter);
86          myProcessor.setExcludedCommands(new String[] {});
87          selenium = new LoggingDefaultSelenium(myProcessor);
88          selenium.start();
89      }
90  
91      @After
92      public void tearDown() {
93          selenium.stop();
94          try {
95              if (null != loggingWriter) {
96                  loggingWriter.close();
97              }
98          } catch (IOException e) {
99              // do nothing
100         }
101     }
102 
103     @Test(expected=com.thoughtworks.selenium.Wait.WaitTimedOutException.class)
104     public void loggingSeleniumWaitTimedOutSample() throws InterruptedException, ParseException {
105         selenium.setContext("loggingSeleniumWaitTimedOutSample()");
106 
107         selenium.open(OPENQA_URL);
108 
109         selenium.captureScreenshot(screenshotsResultsPath
110                 + File.separator
111                 + "openQaHomePage_"
112                 + LoggingUtils.timeStampForFileName()
113                 + ".png");
114 
115         // Note the special assertEquals from logging-selenium needs
116         // a selenium instance and a description string
117         // selenium instance is needed in order to be able to log exceptions
118         // Get rid of this is to be available in V >2.0
119         assertEquals("Expected page title not found", "OpenQA: Home", selenium.getTitle(), selenium);
120 
121         selenium.click("link=Selenium RC");
122         selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
123 
124         assertEquals("Expected page title not found", "Selenium RC: About", selenium.getTitle(), selenium);
125         assertTrue("Expected text 'Supported Platforms' not found", selenium.isTextPresent("Supported Platforms"), selenium);
126 
127         selenium.click("link=Tutorial");
128         selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
129         assertEquals("Expected page title not found", "Selenium RC: Tutorial", selenium.getTitle(), selenium);
130 
131         new LoggingWait(selenium) {
132             public boolean until() {
133                 return selenium.isTextPresent("This Text should never be present");
134             }
135         }.wait("Wait Example - Designed to fail", LoggingSeleniumWaitTimedOutSample.WAIT_TIMEOUT);
136     }
137 }