Skip to content

Commit

Permalink
Fix #593: Include dylib libraries together with msedgedriver
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Jan 17, 2021
1 parent a389e76 commit 11ecd19
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static java.lang.String.valueOf;
import static java.lang.invoke.MethodHandles.lookup;
import static java.nio.charset.Charset.defaultCharset;
import static java.util.Collections.singletonList;
import static java.util.Collections.sort;
import static java.util.Locale.ROOT;
import static java.util.Optional.empty;
Expand Down Expand Up @@ -686,13 +687,13 @@ protected Optional<String> getValueFromResolutionCache(
return optionalBrowserVersion;
}

protected File postDownload(File archive) {
protected List<File> postDownload(File archive) {
File parentFolder = archive.getParentFile();
File[] ls = parentFolder.listFiles();
for (File f : ls) {
if (getDriverName().contains(removeExtension(f.getName()))) {
log.trace("Found driver in post-download: {}", f);
return f;
return singletonList(f);
}
}
throw new WebDriverManagerException("Driver " + getDriverName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.bonigarcia.wdm.managers;

import static io.github.bonigarcia.wdm.config.DriverManagerType.EDGE;
import static java.util.Locale.ROOT;
import static java.util.Optional.empty;
import static org.apache.commons.io.FileUtils.listFiles;

Expand All @@ -26,6 +27,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -108,18 +110,28 @@ protected List<URL> getDriverUrls() throws IOException {
}

@Override
protected File postDownload(File archive) {
protected List<File> postDownload(File archive) {
Collection<File> listFiles = listFiles(new File(archive.getParent()),
null, true);
Iterator<File> iterator = listFiles.iterator();
File file = null;

List<File> files = new ArrayList<>();
while (iterator.hasNext()) {
file = iterator.next();
if (file.getName().contains(getDriverName())) {
return file;
String fileName = file.getName();
if (fileName.contains(getDriverName())) {
log.trace(
"Adding {} at the begining of the resulting file list",
fileName);
files.add(0, file);
} else if (fileName.toLowerCase(ROOT).endsWith(".dylib")) {
log.trace("Adding {} to the resulting file list", fileName);
files.add(file);
}
}
return file;

return files;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.bonigarcia.wdm.managers;

import static io.github.bonigarcia.wdm.config.DriverManagerType.OPERA;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;

import java.io.File;
Expand Down Expand Up @@ -112,7 +113,7 @@ protected List<URL> getDriverUrls() throws IOException {
}

@Override
protected File postDownload(File archive) {
protected List<File> postDownload(File archive) {
log.trace("Post processing for Opera: {}", archive);

File extractFolder = archive.getParentFile()
Expand Down Expand Up @@ -148,7 +149,7 @@ protected File postDownload(File archive) {
} finally {
downloader.deleteFolder(extractFolder);
}
return target;
return singletonList(target);
} else {
return super.postDownload(archive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static io.github.bonigarcia.wdm.config.DriverManagerType.PHANTOMJS;
import static io.github.bonigarcia.wdm.online.UrlHandler.BETA;
import static java.io.File.separator;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;

import java.io.File;
Expand Down Expand Up @@ -110,7 +111,7 @@ protected String getCurrentVersion(URL url) {
}

@Override
protected File postDownload(File archive) {
protected List<File> postDownload(File archive) {
log.trace("PhantomJS package name: {}", archive);

File extractFolder = archive.getParentFile()
Expand Down Expand Up @@ -139,7 +140,8 @@ protected File postDownload(File archive) {

downloader.renameFile(phantomjs, target);
downloader.deleteFolder(extractFolder);
return target;

return singletonList(target);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.bonigarcia.wdm.managers;

import static io.github.bonigarcia.wdm.config.DriverManagerType.SELENIUM_SERVER_STANDALONE;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;

import java.io.File;
Expand Down Expand Up @@ -79,8 +80,8 @@ protected void setDriverUrl(URL url) {
}

@Override
protected File postDownload(File archive) {
return archive;
protected List<File> postDownload(File archive) {
return singletonList(archive);
}

@Override
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/io/github/bonigarcia/wdm/online/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.function.Function;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand All @@ -69,10 +70,10 @@ public class Downloader {

HttpClient httpClient;
Config config;
UnaryOperator<File> postDownloadFunction;
Function<File, List<File>> postDownloadFunction;

public Downloader(HttpClient httpClient, Config config,
UnaryOperator<File> postDownloadFunction) {
Function<File, List<File>> postDownloadFunction) {
this.httpClient = httpClient;
this.config = config;
this.postDownloadFunction = postDownloadFunction;
Expand Down Expand Up @@ -132,16 +133,19 @@ private Optional<File> downloadAndExtract(URL url, File targetFile)
copyInputStreamToFile(httpClient.execute(httpClient.createHttpGet(url))
.getEntity().getContent(), temporaryFile);

File extractedFile = extract(temporaryFile);
File resultingDriver = new File(targetFolder, extractedFile.getName());
List<File> extractedFiles = extract(temporaryFile);
File resultingDriver = new File(targetFolder,
extractedFiles.iterator().next().getName());
boolean driverExists = resultingDriver.exists();

if (!driverExists || config.isForceDownload()) {
if (driverExists) {
log.debug("Overriding former driver {}", resultingDriver);
deleteFile(resultingDriver);
}
moveFileToDirectory(extractedFile, targetFolder, true);
for (File f : extractedFiles) {
moveFileToDirectory(f, targetFolder, true);
}
}
if (!config.isExecutable(resultingDriver)) {
setFileExecutable(resultingDriver);
Expand Down Expand Up @@ -170,7 +174,7 @@ private Optional<File> checkDriver(String driverName, File targetFile) {
return empty();
}

private File extract(File compressedFile) throws IOException {
private List<File> extract(File compressedFile) throws IOException {
String fileName = compressedFile.getName().toLowerCase(ROOT);

boolean extractFile = !fileName.endsWith("exe")
Expand All @@ -192,9 +196,7 @@ private File extract(File compressedFile) throws IOException {
deleteFile(compressedFile);
}

File result = postDownloadFunction.apply(compressedFile)
.getAbsoluteFile();
log.trace("Resulting driver {}", result);
List<File> result = postDownloadFunction.apply(compressedFile);

return result;
}
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/io/github/bonigarcia/wdm/test/edge/EdgeMacTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* (C) Copyright 2021 Boni Garcia (http://bonigarcia.github.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.github.bonigarcia.wdm.test.edge;

import static java.lang.invoke.MethodHandles.lookup;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;

import org.junit.Test;
import org.slf4j.Logger;

import io.github.bonigarcia.wdm.WebDriverManager;

/**
* Test msedgedriver in Mac OS.
*
* @author Boni Garcia (boni.gg@gmail.com)
* @since 4.3.1
*/
public class EdgeMacTest {

static final Logger log = getLogger(lookup().lookupClass());

@Test
public void testEdgeMac() {
String libName = "libc++.dylib";
String driverVersion = "87.0.664.75";

WebDriverManager.edgedriver().clearDriverCache()
.driverVersion(driverVersion).mac().setup();
String downloadedDriverPath = WebDriverManager.edgedriver()
.getDownloadedDriverPath();

log.debug("The downloaded driver path is: {}", downloadedDriverPath);
File driver = new File(downloadedDriverPath);
assertTrue(driver.exists());

File lib = new File(driver.getParent(), libName);
assertTrue("The file " + libName
+ " should be placed together with msedgedriver in version "
+ driverVersion, lib.exists());
}

}

0 comments on commit 11ecd19

Please sign in to comment.