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 LoggingSeleniumJunitAssertionFailedSample {
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 + "sampleResultJunitAssertionFailed.html";
70          System.err.println("resultHtmlFileName=" + resultHtmlFileName);
71  
72          loggingWriter = LoggingUtils.createWriter(resultHtmlFileName,
73                  LoggingSeleniumJunitAssertionFailedSample.RESULT_FILE_ENCODING, true);
74  
75          LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter,
76                  LoggingSeleniumJunitAssertionFailedSample.RESULT_FILE_ENCODING);
77          htmlFormatter.setScreenShotBaseUri(LoggingSeleniumJunitAssertionFailedSample.SCREENSHOT_PATH + "/"); // has to be "/"
78                                                                                                                  // as this is a
79                                                                                                                  // URI
80          htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
81          LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*chrome",
82                  OPENQA_URL), htmlFormatter);
83          myProcessor.setExcludedCommands(new String[] {});
84          selenium = new LoggingDefaultSelenium(myProcessor);
85          selenium.start();
86      }
87  
88      @After
89      public void tearDown() {
90          selenium.stop();
91          try {
92              if (null != loggingWriter) {
93                  loggingWriter.close();
94              }
95          } catch (IOException e) {
96              // do nothing
97          }
98      }
99  
100     @Test(expected=AssertionError.class)
101     public void loggingSeleniumJunitAssertionFailedSample() throws InterruptedException, ParseException {
102         selenium.setContext("loggingSeleniumJunitAssertionFailedSample()");
103 
104         selenium.open(OPENQA_URL);
105 
106         selenium.captureScreenshot(screenshotsResultsPath
107                 + File.separator
108                 + "openQaHomePage_"
109                 + LoggingUtils.timeStampForFileName()
110                 + ".png");
111 
112         // Note the special assertEquals from logging-selenium needs
113         // a selenium instance and a description string
114         // selenium instance is needed in order to be able to log exceptions
115         // Get rid of this is to be available in V >2.0
116         assertEquals("Expected page title not found", "OpenQA: Home", selenium.getTitle(), selenium);
117 
118         selenium.click("link=Selenium RC");
119         selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
120 
121         assertEquals("Expected page title not found", "Selenium RC: About", selenium.getTitle(), selenium);
122         assertTrue("Expected text 'Supported Platforms' not found", selenium.isTextPresent("Supported Platforms"), selenium);
123 
124         selenium.click("link=Tutorial");
125         selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
126         assertEquals("Sample Assertion Failure - Designed to fail", "Bad Title", selenium.getTitle(), selenium);
127 
128         selenium.captureScreenshot(screenshotsResultsPath
129                 + File.separator
130                 + "openQaTutorialPage_"
131                 + LoggingUtils.timeStampForFileName()
132                 + ".png");
133     }
134 }