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.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
68
69
70
71
72
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 + "/");
82
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
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
116
117
118
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 }