Skip to content

Commit 0c503a9

Browse files
committed
Merge branch 'wip'
2 parents 19d4fb6 + 4862a43 commit 0c503a9

6 files changed

Lines changed: 195 additions & 18 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Created by .gitignore support plugin (hsz.mobi)
2+
### Maven template
3+
target/
4+
pom.xml.tag
5+
pom.xml.releaseBackup
6+
pom.xml.versionsBackup
7+
pom.xml.next
8+
release.properties

pom.xml

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>ch.racic.selenium.helper</groupId>
1313
<artifactId>SeleniumDownloadHelper</artifactId>
14-
<version>0.2-SNAPSHOT</version>
14+
<version>0.3.0-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616

1717
<name>Selenium Download Helper library</name>
@@ -38,6 +38,19 @@
3838
<url>http://github.com/rac2030/SeleniumDownloadHelper</url>
3939
</scm>
4040

41+
<distributionManagement>
42+
<snapshotRepository>
43+
<id>sonatype-nexus-snapshots</id>
44+
<name>Sonatype Nexus Snapshots</name>
45+
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
46+
</snapshotRepository>
47+
<repository>
48+
<id>sonatype-nexus-staging</id>
49+
<name>sonatype-nexus-staging</name>
50+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
51+
</repository>
52+
</distributionManagement>
53+
4154
<issueManagement>
4255
<system>github</system>
4356
<url>https://github.com/rac2030/SeleniumDownloadHelper/issues</url>
@@ -70,7 +83,7 @@
7083
<scope>test</scope>
7184
</dependency>
7285
<dependency>
73-
<groupId>junit</groupId>
86+
<groupId>junit</groupId>
7487
<artifactId>junit</artifactId>
7588
<version>4.11</version>
7689
<scope>test</scope>
@@ -81,6 +94,18 @@
8194
<version>8.1.13.v20130916</version>
8295
<scope>test</scope>
8396
</dependency>
97+
<dependency>
98+
<groupId>net.anthavio</groupId>
99+
<artifactId>phanbedder-1.9.7</artifactId>
100+
<version>1.0.0</version>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>ch.racic.testing</groupId>
105+
<artifactId>TestFrameworkHelper</artifactId>
106+
<version>0.1.4</version>
107+
<scope>test</scope>
108+
</dependency>
84109
</dependencies>
85110

86111
<build>
@@ -100,11 +125,78 @@
100125
<nosuffix>true</nosuffix>
101126
</configuration>
102127
</plugin-->
128+
<plugin>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-source-plugin</artifactId>
131+
<version>2.2.1</version>
132+
<executions>
133+
<execution>
134+
<id>attach-sources</id>
135+
<goals>
136+
<goal>jar-no-fork</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
</plugin>
141+
<plugin>
142+
<groupId>org.apache.maven.plugins</groupId>
143+
<artifactId>maven-javadoc-plugin</artifactId>
144+
<version>2.9.1</version>
145+
<executions>
146+
<execution>
147+
<id>attach-javadocs</id>
148+
<goals>
149+
<goal>jar</goal>
150+
</goals>
151+
</execution>
152+
</executions>
153+
</plugin>
154+
<plugin>
155+
<artifactId>maven-gpg-plugin</artifactId>
156+
<version>1.4</version>
157+
<executions>
158+
<execution>
159+
<phase>verify</phase>
160+
<goals>
161+
<goal>sign</goal>
162+
</goals>
163+
</execution>
164+
</executions>
165+
</plugin>
166+
<plugin>
167+
<groupId>org.sonatype.plugins</groupId>
168+
<artifactId>nexus-staging-maven-plugin</artifactId>
169+
<version>1.6</version>
170+
<extensions>true</extensions>
171+
<configuration>
172+
<serverId>sonatype-nexus-staging</serverId>
173+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
174+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
175+
</configuration>
176+
</plugin>
177+
<plugin>
178+
<groupId>org.apache.maven.plugins</groupId>
179+
<artifactId>maven-release-plugin</artifactId>
180+
<version>2.5</version>
181+
<configuration>
182+
<autoVersionSubmodules>true</autoVersionSubmodules>
183+
<useReleaseProfile>false</useReleaseProfile>
184+
<releaseProfiles>release</releaseProfiles>
185+
<goals>deploy</goals>
186+
</configuration>
187+
</plugin>
103188
</plugins>
104189
</build>
190+
105191
<profiles>
106192
<profile>
107193
<id>sign</id>
194+
<activation>
195+
<property>
196+
<name>performRelease</name>
197+
<value>true</value>
198+
</property>
199+
</activation>
108200
<build>
109201
<plugins>
110202
<!-- See http://maven.apache.org/plugins/maven-gpg-plugin/ -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyleft (c) 2014. This code is for learning purposes only. Do whatever you like with it but don't take it as perfect code.
3+
* Michel Racic (http://rac.su/+) => github.com/rac2030
4+
*/
5+
6+
package ch.racic.selenium.helper.download;
7+
8+
/**
9+
* Transport wrapper for raw data
10+
*/
11+
public class FileData {
12+
private String guessedFilename;
13+
private byte[] data;
14+
15+
public FileData(String guessedFilename, byte[] data) {
16+
this.guessedFilename = guessedFilename;
17+
this.data = data;
18+
}
19+
20+
public String getGuessedFilename() {
21+
return guessedFilename;
22+
}
23+
24+
public byte[] getData() {
25+
return data;
26+
}
27+
}

src/main/java/ch/racic/selenium/helper/download/SeleniumDownloadHelper.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
import org.openqa.selenium.WebDriver;
1313
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
1414
import org.openqa.selenium.ie.InternetExplorerDriver;
15-
import sun.plugin.dom.exception.BrowserNotSupportedException;
1615

1716
import java.io.File;
1817
import java.io.IOException;
19-
import java.io.UnsupportedEncodingException;
2018
import java.net.URL;
21-
import java.nio.charset.Charset;
2219

2320
/**
2421
* Created by rac on 08.06.14.
@@ -89,7 +86,7 @@ private String getDownloadJsCallScript(URL url) {
8986
* @param url
9087
* @return raw data
9188
*/
92-
public byte[] getFileFromUrl(URL url) {
89+
public FileData getFileFromUrlRaw(URL url) {
9390
String scriptCollection;
9491
if (js instanceof InternetExplorerDriver) {
9592
scriptCollection = ieHackTestJs + ieHackJs + base64Js + dlHelperJs + getDownloadJsCallScript(url);
@@ -99,8 +96,14 @@ public byte[] getFileFromUrl(URL url) {
9996
} else {
10097
scriptCollection = ieHackTestJs + base64Js + dlHelperJs + getDownloadJsCallScript(url);
10198
}
102-
String encodedContent = (String) js.executeScript(scriptCollection);
103-
return Base64.decodeBase64(encodedContent);
99+
String jsRetVal = (String) js.executeScript(scriptCollection);
100+
String[] jsRetArr = jsRetVal.split(":contentstarts:", 2);
101+
String encodedContent = jsRetArr[1];
102+
String fileName = jsRetArr[0];
103+
if (fileName.equals("")) {
104+
fileName = url.getFile().replaceAll("/", "");
105+
}
106+
return new FileData(fileName, Base64.decodeBase64(encodedContent));
104107
}
105108

106109
/**
@@ -113,9 +116,25 @@ public byte[] getFileFromUrl(URL url) {
113116
* @throws IOException
114117
*/
115118
public File getFileFromUrl(URL url, File outputFile) throws IOException {
116-
byte[] content = getFileFromUrl(url);
119+
byte[] content = getFileFromUrlRaw(url).getData();
117120
FileUtils.writeByteArrayToFile(outputFile, content);
118121
return outputFile;
119122
}
120123

124+
/**
125+
* Executes XHR request trough JavaScript to download the given file in the context of the current WebDriver
126+
* session. This method returns a tmp file and takes a guessed filename
127+
*
128+
* @param url
129+
* @return outpuFile
130+
* @throws IOException
131+
*/
132+
public File getFileFromUrl(URL url) throws IOException {
133+
FileData fd = getFileFromUrlRaw(url);
134+
byte[] content = fd.getData();
135+
File tmpFile = File.createTempFile("", fd.getGuessedFilename());
136+
FileUtils.writeByteArrayToFile(tmpFile, content);
137+
return tmpFile;
138+
}
139+
121140
}

src/main/resources/ch/racic/selenium/helper/download/js/seleniumDownloadHelper.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Michel Racic (http://rac.su/+) => github.com/rac2030
44
*/
55

6+
67
var seleniumDownloadHelper = {
78
getBinary: function (url) {
89
// Mozilla/Safari/IE7+
@@ -21,16 +22,29 @@ var seleniumDownloadHelper = {
2122
}
2223

2324
xhr.send(null);
25+
2426
if (xhr.status != 200) return '';
27+
28+
// Code taken from http://stackoverflow.com/q/16086162
29+
var disp = xhr.getResponseHeader('Content-Disposition');
30+
var fileName = "";
31+
if (disp && disp.search('filename') != -1) {
32+
var filenamePattern = "filename=\"(.*)\".*";
33+
var re = new RegExp(filenamePattern);
34+
if (re.test(disp)) {
35+
fileName = re.exec(disp)[1];
36+
}
37+
}
38+
2539
if (IE_HACK) {
26-
return bin2arr(xhr.responseBody);
40+
return [fileName, bin2arr(xhr.responseBody)];
2741
} else {
28-
return xhr.responseText;
42+
return [fileName, xhr.responseText];
2943
}
3044
},
3145

3246
getB64Binary: function (url) {
3347
var content = seleniumDownloadHelper.getBinary(url);
34-
return base64Encode(content);
48+
return content[0] + ":contentstarts:" + base64Encode(content[1]);
3549
}
3650
};

src/test/java/ch/racic/selenium/helper/download/SeleniumDownloadHelperTest.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55

66
package ch.racic.selenium.helper.download;
77

8+
import ch.racic.testing.annotation.TargetOS;
9+
import ch.racic.testing.junit.runner.OSSensitiveRunner;
10+
import net.anthavio.phanbedder.Phanbedder;
811
import org.apache.commons.io.FileUtils;
912
import org.eclipse.jetty.server.Handler;
1013
import org.eclipse.jetty.server.Server;
1114
import org.eclipse.jetty.server.handler.HandlerList;
1215
import org.eclipse.jetty.server.handler.ResourceHandler;
1316
import org.junit.*;
17+
import org.junit.runner.RunWith;
1418
import org.openqa.selenium.WebDriver;
1519
import org.openqa.selenium.chrome.ChromeDriver;
1620
import org.openqa.selenium.firefox.FirefoxDriver;
1721
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
1822
import org.openqa.selenium.ie.InternetExplorerDriver;
1923
import org.openqa.selenium.phantomjs.PhantomJSDriver;
24+
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
25+
import org.openqa.selenium.remote.DesiredCapabilities;
2026
import org.openqa.selenium.safari.SafariDriver;
2127

2228
import java.io.File;
@@ -25,6 +31,7 @@
2531
/**
2632
* Created by rac on 08.06.14.
2733
*/
34+
@RunWith(OSSensitiveRunner.class)
2835
public class SeleniumDownloadHelperTest {
2936

3037
private WebDriver driver;
@@ -53,7 +60,9 @@ public static void setUpClass() throws Exception {
5360

5461
@AfterClass
5562
public static void tearDownClass() throws Exception {
56-
server.stop();
63+
if (server != null) {
64+
server.stop();
65+
}
5766
}
5867

5968
@Before
@@ -69,23 +78,24 @@ public void tearDownTestDriver() throws Exception {
6978
}
7079

7180
@Test
81+
@Ignore("Known to fail")
7282
public void testGetFileFromUrlHtmlUnit() throws Exception {
73-
// Create HTMLUnit as driver for this test
7483
driver = new HtmlUnitDriver(true);
7584
invokeGetFileDataFromUrl();
7685
invokeGetFileFromUrl();
7786
}
7887

7988
@Test
8089
public void testGetFileFromUrlChrome() throws Exception {
81-
//TODO put this into pom profiles which are OS specific
90+
//TODO put this into pom profiles which are OS specific or build helper lib like phantomjs has
8291
System.setProperty("webdriver.chrome.driver", "driver/chromedriver");
8392
driver = new ChromeDriver();
8493
invokeGetFileDataFromUrl();
8594
invokeGetFileFromUrl();
8695
}
8796

8897
@Test
98+
@TargetOS(family = "mac")
8999
public void testGetFileFromUrlSafari() throws Exception {
90100
driver = new SafariDriver();
91101
invokeGetFileDataFromUrl();
@@ -100,24 +110,31 @@ public void testGetFileFromUrlFireFox() throws Exception {
100110
}
101111

102112
@Test
113+
@TargetOS(family = "windows")
103114
public void testGetFileFromUrlInternetExplorer() throws Exception {
115+
//TODO Fetch latest binaries and make arch specific profiles
104116
driver = new InternetExplorerDriver();
105117
invokeGetFileDataFromUrl();
106118
invokeGetFileFromUrl();
107119
}
108120

109121
@Test
110122
public void testGetFileFromUrlPhantomJS() throws Exception {
111-
driver = new PhantomJSDriver();
123+
// See http://blog.anthavio.net/2014/04/phantomjs-embedder-for-selenium.html
124+
File phantomjs = Phanbedder.unpack();
125+
DesiredCapabilities dcaps = new DesiredCapabilities();
126+
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
127+
driver = new PhantomJSDriver(dcaps);
112128
invokeGetFileDataFromUrl();
113129
invokeGetFileFromUrl();
114130
}
115131

116132
private void invokeGetFileDataFromUrl() throws Exception {
117133
driver.get(baseUrl + indexPage);
118134
SeleniumDownloadHelper sdlh = new SeleniumDownloadHelper(driver);
119-
byte[] fileContent = sdlh.getFileFromUrl(new URL(baseUrl + testPdf));
120-
Assert.assertArrayEquals("Raw data is correct", referenceContent, fileContent);
135+
FileData testFileData = sdlh.getFileFromUrlRaw(new URL(baseUrl + testPdf));
136+
Assert.assertArrayEquals("Raw data is correct", referenceContent, testFileData.getData());
137+
Assert.assertTrue("Guessed name from URL is correct", testPdf.equals(testFileData.getGuessedFilename()));
121138
}
122139

123140
private void invokeGetFileFromUrl() throws Exception {

0 commit comments

Comments
 (0)