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.unitedinternet.portal.selenium.utils.logging.HtmlResultFormatter;
33  import com.unitedinternet.portal.selenium.utils.logging.LoggingCommandProcessor;
34  import com.unitedinternet.portal.selenium.utils.logging.LoggingDefaultSelenium;
35  import com.unitedinternet.portal.selenium.utils.logging.LoggingResultsFormatter;
36  import com.unitedinternet.portal.selenium.utils.logging.LoggingSelenium;
37  import com.unitedinternet.portal.selenium.utils.logging.LoggingUtils;
38  
39  public class LoggingSeleniumSuccessSample {
40      protected LoggingSelenium selenium;
41  
42      private BufferedWriter loggingWriter;
43  
44      private static final String RESULT_FILE_ENCODING = "UTF-8";
45  
46      private static final String DEFAULT_TIMEOUT = "30000";
47  
48      private static final String OPENQA_URL = "http://openqa.org";
49  
50      private static final String SCREENSHOT_PATH = "screenshots";
51  
52      private final String RESULTS_BASE_PATH = "target" + File.separator + "loggingResults";
53  
54      private String resultsPath = new File(RESULTS_BASE_PATH).getAbsolutePath();
55  
56      private String screenshotsResultsPath = new File(RESULTS_BASE_PATH + File.separator + SCREENSHOT_PATH).getAbsolutePath();
57  
58      @Before
59      public void setUp() {
60          if (!new File(screenshotsResultsPath).exists()) {
61              new File(screenshotsResultsPath).mkdirs();
62          }
63          // to keep every result use a timestamped filename like this
64          // final String resultHtmlFileName = resultsPath
65          // + File.separator
66          // + "autorunResult"
67          // + LoggingUtils.timeStampForFileName()
68          // + ".html";
69          final String resultHtmlFileName = resultsPath + File.separator + "sampleResultSuccess.html";
70          System.err.println("resultHtmlFileName=" + resultHtmlFileName);
71  
72          loggingWriter = LoggingUtils.createWriter(resultHtmlFileName, LoggingSeleniumSuccessSample.RESULT_FILE_ENCODING, true);
73  
74          LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter,
75                  LoggingSeleniumSuccessSample.RESULT_FILE_ENCODING);
76          htmlFormatter.setScreenShotBaseUri(LoggingSeleniumSuccessSample.SCREENSHOT_PATH + "/"); // has to be "/" as this is a URI
77          htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
78          LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*chrome",
79                  OPENQA_URL), htmlFormatter);
80          myProcessor.setExcludedCommands(new String[] {});
81          selenium = new LoggingDefaultSelenium(myProcessor);
82          selenium.start();
83      }
84  
85      @After
86      public void tearDown() {
87          selenium.stop();
88          try {
89              if (null != loggingWriter) {
90                  loggingWriter.close();
91              }
92          } catch (IOException e) {
93              // do nothing
94          }
95      }
96  
97      @Test
98      public void loggingSeleniumSuccessSample() throws InterruptedException, ParseException {
99          selenium.setContext("loggingSeleniumSuccessSample()");
100 
101         selenium.open(OPENQA_URL);
102 
103         selenium.captureScreenshot(screenshotsResultsPath
104                 + File.separator
105                 + "openQaHomePage_"
106                 + LoggingUtils.timeStampForFileName()
107                 + ".png");
108 
109         // Note the special assertEquals from logging-selenium needs
110         // a selenium instance and a description string
111         // selenium instance is needed in order to be able to log exceptions
112         // Get rid of this is to be available in V >2.0
113         assertEquals("Expected page title not found", "OpenQA: Home", selenium.getTitle(), selenium);
114 
115         selenium.click("link=Selenium RC");
116         selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
117 
118         assertEquals("Expected page title not found", "Selenium RC: About", selenium.getTitle(), selenium);
119         assertTrue("Expected text 'Supported Platforms' not found", selenium.isTextPresent("Supported Platforms"), selenium);
120 
121         selenium.click("link=Tutorial");
122         selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
123         assertEquals("Expected page title not found", "Selenium RC: Tutorial", selenium.getTitle(), selenium);
124 
125         selenium.captureScreenshot(screenshotsResultsPath
126                 + File.separator
127                 + "openQaTutorialPage_"
128                 + LoggingUtils.timeStampForFileName()
129                 + ".png");
130     }
131 }