Skip to content

Commit

Permalink
Implemented fully dynamical driver exe file path obtaining with versi…
Browse files Browse the repository at this point in the history
…on folders splitting
  • Loading branch information
Hanna-Kokhanava committed Feb 11, 2019
1 parent 17faea1 commit 9355e61
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,24 @@ private static class Repositories {
* @return driver executable file
*/
public static String getDriverExecutableFilePath(String driverName) {
Logger.debug("Getting executable file path for [" + driverName + "]");
DriverRepository repository = getRepositoryObjectById(driverName);
HostMachine host = BrowserManager.getCurrentBrowser().getHost();
FileManager fileManager = FileManager.getInstance(host);

DriverRepository repository = getRepositoryObjectById(driverName);
String driverFileName = repository.getName() + repository.getVersion();
String zipFileDriverName = driverFileName + ".zip";
String zipFilePath = DRIVERS_FOLDER + File.separator + zipFileDriverName;
String directoryPath = DRIVERS_FOLDER + File.separator + repository.getName()
+ File.separator + repository.getVersion();

if (!fileManager.isFileExist(DRIVERS_FOLDER, new File(zipFilePath), zipFileDriverName)) {
if (!fileManager.isFileExist(DRIVERS_FOLDER, new File(driverName), driverName)) {
Logger.debug("Start to download and unzip driver executable file");
String driverRepositoryUrl = getRepositoryURL(host, driverName);
fileManager.downloadFileFromUrl(driverRepositoryUrl, zipFilePath);
fileManager.unzipFile(zipFilePath, DRIVERS_FOLDER.getPathToFolder(host));
}
String zipFilePath = directoryPath + File.separator + driverName + ".zip";

return DRIVERS_FOLDER + File.separator + driverFileName + ".exe";
}
fileManager.createSubDirectories(directoryPath);
fileManager.downloadFileFromUrl(getRepositoryURL(host, driverName), zipFilePath);
fileManager.unzipFile(zipFilePath, directoryPath);
}

public static void manageDriverExeFilesName() {
//TODO implement name set up - "driver + version"
//TODO not in unzip method to don't mix logic
Logger.debug("Got executable file path for [" + driverName + "] with version [" + repository.getVersion() + "]");
return directoryPath + File.separator + driverName + ".exe";
}

/**
Expand All @@ -73,7 +69,7 @@ public static void manageDriverExeFilesName() {
* @param driverName name of driver
* @return url string
*/
public static String getRepositoryURL(HostMachine hostMachine, String driverName) {
private static String getRepositoryURL(HostMachine hostMachine, String driverName) {
if (driverList.isEmpty()) {
throw new RuntimeException("List of driver repositories is empty!");
}
Expand All @@ -96,14 +92,16 @@ public static String getRepositoryURL(HostMachine hostMachine, String driverName
* @param driverName name of driver
* @return {@link DriverRepository}
*/
public static DriverRepository getRepositoryObjectById(String driverName) {
private static DriverRepository getRepositoryObjectById(String driverName) {
if (driverList.isEmpty()) {
throw new RuntimeException("List of driver repositories is empty!");
}
DriverRepository driverRepository = driverList.stream()
.filter(driver -> driver.getName().equalsIgnoreCase(driverName))
.findFirst()
.orElse(null);
return Objects.requireNonNull(driverRepository, "Driver with ID [" + driverName + "] was not found");

return Objects.requireNonNull(driverRepository,
"Driver with name [" + driverName + "] was not found in configuration file");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class WebDriverManager {
private static final long IMPLICIT_WAIT_TIMEOUT = 5;
private static WebDriver driver;

//Need to coincide with driver-repositories.xml configuration file
private static final String CHROME_DRIVER_NAME = "chromedriver";
private static final String FIREFOX_DRIVER_NAME = "geckodriver";

/**
* Creates driver depends on current browser type
*
Expand All @@ -40,12 +44,12 @@ public static void createDriver(DesiredCapabilities capabilities) {
switch (platform) {
case CHROME:
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY,
DriverRepositoryManager.getDriverExecutableFilePath("chromedriver"));
DriverRepositoryManager.getDriverExecutableFilePath(CHROME_DRIVER_NAME));
driver = new ChromeDriver((ChromeOptions) options);
break;
case FIREFOX:
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,
DriverRepositoryManager.getDriverExecutableFilePath("geckodriver"));
DriverRepositoryManager.getDriverExecutableFilePath(FIREFOX_DRIVER_NAME));
driver = new FirefoxDriver((FirefoxOptions) options);
break;
case IE10:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,34 @@ public static FileManager getInstance(HostMachine hostMachine) {
* @param filePath path to file in local system
*/
public void downloadFileFromUrl(String fileUrl, String filePath) {
if (!Files.exists(Paths.get(filePath))) {
Logger.debug("Downloading file from " + fileUrl);
ReadableByteChannel channel = null;
FileOutputStream outputStream = null;
Logger.debug("Downloading file from [" + fileUrl + "]");
ReadableByteChannel channel = null;
FileOutputStream outputStream = null;

try {
channel = Channels.newChannel(new URL(fileUrl).openStream());
outputStream = new FileOutputStream(new File(filePath));
outputStream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
} catch (SSLHandshakeException e) {
//TODO infinite recursive call ?
Logger.warn("SSL Exception was thrown, execute stub to disable certificates validation and try to download once again");
disableCertificateValidation();
downloadFileFromUrl(fileUrl, filePath);
} catch (IOException e) {
Logger.error("Exception occurred while channel creation\n" + e.getMessage());
e.printStackTrace();
} finally {
try {
channel = Channels.newChannel(new URL(fileUrl).openStream());
outputStream = new FileOutputStream(filePath);
outputStream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
} catch (SSLHandshakeException e) {
Logger.warn("SSL Exception was thrown, execute stub to disable certificates validation and try to download once again");
disableCertificateValidation();
downloadFileFromUrl(fileUrl, filePath);
if (Objects.nonNull(outputStream)) {
outputStream.close();
}
if (Objects.nonNull(channel)) {
channel.close();
}
} catch (IOException e) {
Logger.error("Exception occurred during streams closing\n" + e.getMessage());
e.printStackTrace();
} finally {
try {
if (Objects.nonNull(outputStream)) {
outputStream.close();
}
if (Objects.nonNull(channel)) {
channel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Logger.warn("File [" + filePath + "] is already exists in the folder");
}
}

Expand Down Expand Up @@ -137,23 +136,25 @@ public void unzipFile(String zipFilePath, String destPath) {
File newFile;

try {
fileInputStream = new FileInputStream(zipFilePath);
fileInputStream = new FileInputStream(new File(zipFilePath));
zipInputStream = new ZipInputStream(fileInputStream);
zipEntry = zipInputStream.getNextEntry();

while (Objects.nonNull(zipEntry)) {
filePath = destPath + File.separator + zipEntry.getName();
newFile = new File(filePath);

if (zipEntry.isDirectory()) {
new File(filePath).mkdirs();
} else {
new File(newFile.getParent()).mkdirs();
extractFile(zipInputStream, filePath);
}

zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
} catch (IOException e) {
Logger.error("Exception occurred while unzipping process\n" + e.getMessage());
e.printStackTrace();
} finally {
try {
Expand All @@ -165,9 +166,12 @@ public void unzipFile(String zipFilePath, String destPath) {
fileInputStream.close();
}
} catch (IOException e) {
Logger.error("Exception occurred while streams closing\n" + e.getMessage());
e.printStackTrace();
}
}

new File(zipFilePath).delete();
}

/**
Expand All @@ -187,6 +191,20 @@ private static void extractFile(ZipInputStream zipInputStream, String filePath)
bufferedOutputStream.close();
}

/**
* Creates nonexistent parent directories
*
* @param pathToDirectory directories to create
*/
public void createSubDirectories(String pathToDirectory) {
try {
Files.createDirectories(Paths.get(pathToDirectory));
} catch (IOException e) {
Logger.error("Unable to create nonexistent parent directories\n" + e.getMessage());
e.printStackTrace();
}
}

/**
* Copy file to folder
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getKey() {
}

/**
* Browser properties keys from browser.properties file
* Browser properties keys from web.properties file
*/
public enum BrowserProperty implements IProperty {
BROWSER_TYPE("browser.type"),
Expand Down Expand Up @@ -102,7 +102,7 @@ public String getKey() {

private static final String GENERAL_TEST_PROPERTIES_PATH = "general.properties";
private static final String MOBILE_TEST_PROPERTIES_PATH = "mobile.properties";
private static final String BROWSER_TEST_PROPERTIES_PATH = "browser.properties";
private static final String WEB_TEST_PROPERTIES_PATH = "web.properties";

private static Properties generalProperties;
private static Properties testProperties;
Expand Down Expand Up @@ -150,7 +150,7 @@ public static String get(IProperty property) {
if (property.getClass().equals(MobileProperty.class)) {
propFromFile = PropertyLoader.getPropertyFromFile(property.getKey(), MOBILE_TEST_PROPERTIES_PATH);
} else {
propFromFile = PropertyLoader.getPropertyFromFile(property.getKey(), BROWSER_TEST_PROPERTIES_PATH);
propFromFile = PropertyLoader.getPropertyFromFile(property.getKey(), WEB_TEST_PROPERTIES_PATH);
}
Objects.requireNonNull(propFromFile, "Unable to resolve '" + propFromFile + "' property value");
return propFromFile;
Expand Down
7 changes: 0 additions & 7 deletions src/main/resources/browser.properties

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/resources/web.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# firefox, chrome
#browser.type=chrome
browser.type=firefox

# Browsers list - home/work - different hostnames
browsers.xml=browsers/work/browsers.xml
#browsers.xml=browsers/home/browsers.xml

0 comments on commit 9355e61

Please sign in to comment.