1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
64
65
66
67
68
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 + "/");
78
79
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
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
113
114
115
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 }