Skip to content

Commit 01e182e

Browse files
committed
SimonStewart: Sorting out text handling a little bit
r4661
1 parent 050b3e0 commit 01e182e

File tree

9 files changed

+78
-50
lines changed

9 files changed

+78
-50
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2008 Google Inc. All Rights Reserved.
2+
3+
package com.thoughtworks.webdriver.internal;
4+
5+
public enum OperatingSystem {
6+
WINDOWS("win") {
7+
@Override
8+
public String getLineEnding() {
9+
return "\r\n";
10+
}
11+
},
12+
MAC("mac") {
13+
@Override
14+
public String getLineEnding() {
15+
return "\r";
16+
}
17+
},
18+
UNIX("x") {
19+
@Override
20+
public String getLineEnding() {
21+
return "\n";
22+
}
23+
};
24+
25+
private static OperatingSystem currentOs;
26+
private final String partOfOsName;
27+
28+
private OperatingSystem(String partOfOsName) {
29+
this.partOfOsName = partOfOsName;
30+
}
31+
32+
public static OperatingSystem getCurrentPlatform() {
33+
if (currentOs != null) {
34+
return currentOs;
35+
}
36+
37+
for (OperatingSystem os : OperatingSystem.values()) {
38+
if (os.isCurrentPlatform()) {
39+
currentOs = os;
40+
return os;
41+
}
42+
}
43+
44+
// Default to assuming we're on a unix variant (including linux)
45+
currentOs = UNIX;
46+
return UNIX;
47+
}
48+
49+
public abstract String getLineEnding();
50+
51+
private boolean isCurrentPlatform() {
52+
String osName = System.getProperty("os.name").toLowerCase();
53+
return osName.indexOf(partOfOsName) != -1;
54+
}
55+
}

common/test/java/com/thoughtworks/webdriver/CookieImplementationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testGetAllCookies() {
5454
assertThat(cookies.contains(cookie2), is(true));
5555
}
5656

57-
@Ignore("safari")
57+
@Ignore("ie, safari")
5858
public void testCookieIntegrity() {
5959
driver.get(alternateBaseUrl + "animals");
6060
driver.manage().deleteAllCookies();

common/test/java/com/thoughtworks/webdriver/FormHandlingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ public void testShouldThrowAnExceptionWhenTogglingTheStateOfARadioButton() {
166166
}
167167
}
168168

169-
@Ignore(value = "ie, safari", reason = "Test fails")
169+
@Ignore(value = "safari", reason = "Test fails")
170170
public void testShouldBeAbleToAlterTheContentsOfAFileUploadInputElement() {
171171
driver.get(formPage);
172172
WebElement uploadElement = driver.findElement(By.id("upload"));
173173
assertThat(uploadElement.getValue(), equalTo(""));
174-
uploadElement.setValue("Cheese");
175-
assertThat(uploadElement.getValue(), equalTo("Cheese"));
174+
uploadElement.setValue("Cheese/\\/");
175+
assertThat(uploadElement.getValue(), equalTo("Cheese/\\/"));
176176
}
177177

178178
public void testShouldThrowAnExceptionWhenSelectingAnUnselectableElement() {

common/test/java/com/thoughtworks/webdriver/TextHandlingTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
import static org.hamcrest.Matchers.equalTo;
55
import static org.hamcrest.Matchers.is;
66

7+
import com.thoughtworks.webdriver.internal.OperatingSystem;
8+
79
public class TextHandlingTest extends AbstractDriverTestCase {
810
private String newLine;
911

1012
@Override
1113
protected void setUp() throws Exception {
1214
super.setUp();
1315

14-
String os = System.getProperty("os.name").toLowerCase();
15-
if (os.indexOf("win") != -1)
16-
newLine = "\r\n";
17-
else
18-
newLine = "\n";
16+
newLine = OperatingSystem.getCurrentPlatform().getLineEnding();
1917
}
2018

2119
public void testShouldReturnTheTextContentOfASingleElementWithNoChildren() {

firefox/src/java/com/thoughtworks/webdriver/firefox/FirefoxDriver.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.thoughtworks.webdriver.internal.FindsById;
1111
import com.thoughtworks.webdriver.internal.FindsByLinkText;
1212
import com.thoughtworks.webdriver.internal.FindsByXPath;
13+
import com.thoughtworks.webdriver.internal.OperatingSystem;
1314

1415
import java.beans.BeanInfo;
1516
import java.beans.IntrospectionException;
@@ -56,9 +57,11 @@ public FirefoxDriver(String profileName) {
5657

5758
if (!extension.isConnected()) {
5859
throw new RuntimeException(
59-
"Unable to connect to Firefox. Is the WebDriver extension installed, and is there a profile called WebDriver?\n" +
60-
"To set up a profile for WebDriver, simply start firefox from the command line with the \"ProfileManager\" switch\n" +
61-
"This will look like: firefox -ProfileManager. Alternatively, use the FirefoxLauncher support class from this project");
60+
"Unable to connect to Firefox. Is the WebDriver extension installed, and is there a profile called WebDriver?" +
61+
OperatingSystem.getCurrentPlatform().getLineEnding() +
62+
"To set up a profile for WebDriver, simply start firefox from the command line with the \"ProfileManager\" switch" +
63+
OperatingSystem.getCurrentPlatform().getLineEnding() +
64+
"This will look like: firefox -ProfileManager. Alternatively, use the FirefoxLauncher support class from this project");
6265
}
6366

6467
fixId();

firefox/src/java/com/thoughtworks/webdriver/firefox/FirefoxLauncher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import com.thoughtworks.webdriver.internal.OperatingSystem;
23+
2224
public class FirefoxLauncher {
2325
public static void main(String[] args) {
2426
FirefoxLauncher launcher = new FirefoxLauncher();

firefox/src/java/com/thoughtworks/webdriver/firefox/FirefoxWebElement.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.thoughtworks.webdriver.WebDriver;
44
import com.thoughtworks.webdriver.WebElement;
55
import com.thoughtworks.webdriver.RenderedWebElement;
6+
import com.thoughtworks.webdriver.internal.OperatingSystem;
67

78
import java.awt.Point;
89
import java.awt.Dimension;
@@ -45,7 +46,7 @@ public String getValue() {
4546
if (!"OK".equals(status))
4647
return null;
4748

48-
return remainder;
49+
return remainder.replace("\n", OperatingSystem.getCurrentPlatform().getLineEnding());
4950
}
5051

5152
public WebDriver setValue(String value) {
@@ -91,7 +92,8 @@ public boolean isEnabled() {
9192
}
9293

9394
public String getText() {
94-
return parent.sendMessage("getElementText", elementId);
95+
String toReturn = parent.sendMessage("getElementText", elementId);
96+
return toReturn.replace("\n", OperatingSystem.getCurrentPlatform().getLineEnding());
9597
}
9698

9799
public List<WebElement> getChildrenOfType(String tagName) {

firefox/src/java/com/thoughtworks/webdriver/firefox/OperatingSystem.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

htmlunit/src/java/com/thoughtworks/webdriver/htmlunit/HtmlUnitWebElement.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.thoughtworks.webdriver.NoSuchElementException;
2222
import com.thoughtworks.webdriver.WebDriver;
2323
import com.thoughtworks.webdriver.WebElement;
24+
import com.thoughtworks.webdriver.internal.OperatingSystem;
2425

2526
import java.io.IOException;
2627
import java.util.ArrayList;
@@ -39,6 +40,8 @@ public class HtmlUnitWebElement implements WebElement {
3940
public HtmlUnitWebElement(HtmlUnitDriver parent, HtmlElement element) {
4041
this.parent = parent;
4142
this.element = element;
43+
44+
4245
}
4346

4447
public WebDriver click() {
@@ -239,7 +242,7 @@ private void getTextFromNode(DomNode node, StringBuffer toReturn, StringBuffer t
239242
}
240243

241244
if (isBlockLevel(node)) {
242-
toReturn.append(collapseWhitespace(textSoFar)).append("\n");
245+
toReturn.append(collapseWhitespace(textSoFar)).append(OperatingSystem.getCurrentPlatform().getLineEnding());
243246
textSoFar.delete(0, textSoFar.length());
244247
}
245248
}

0 commit comments

Comments
 (0)