Skip to content

Commit

Permalink
New feature: file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ArKniiD committed Jan 30, 2018
1 parent 5988b74 commit c4ce448
Show file tree
Hide file tree
Showing 20 changed files with 327 additions and 185 deletions.
Empty file added downloadFiles/test.txt
Empty file.
69 changes: 5 additions & 64 deletions src/main/java/com/github/noraui/application/steps/CommonSteps.java
Expand Up @@ -6,19 +6,13 @@
*/
package com.github.noraui.application.steps;

import static com.github.noraui.utils.Constants.USER_DIR;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
Expand Down Expand Up @@ -46,7 +40,6 @@
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.api.java.fr.Alors;
import cucumber.api.java.fr.Et;
import cucumber.api.java.fr.Lorsque;
import cucumber.api.java.fr.Quand;
Expand All @@ -60,16 +53,6 @@ public class CommonSteps extends Step {
*/
private static final Logger logger = LoggerFactory.getLogger(CommonSteps.class);

public static final String DOWNLOAD_FILES_FOLDER = "downloadFiles";

@Conditioned
@Lorsque("Je vide le repertoire des téléchargements[\\.|\\?]")
@Given("I clean download directory[\\.|\\?]")
public void cleanDownloadDirectory(List<GherkinStepCondition> conditions) throws IOException {
FileUtils.forceMkdir(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOAD_FILES_FOLDER));
FileUtils.cleanDirectory(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOAD_FILES_FOLDER));
}

/**
* Waits a time in second.
*
Expand All @@ -88,36 +71,7 @@ public void wait(int time, List<GherkinStepCondition> conditions) throws Interru
}

/**
* Waits a time in second.
*
* @param time
* is time to wait
* @param conditions
* list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
* @throws InterruptedException
* Exception for the sleep
* @throws TechnicalException
* @throws FailureException
*/
@Conditioned
@Lorsque("J'attends que le fichier nommé '(.*)' soit téléchargé avec un timeout de '(.*)' secondes[\\.|\\?]")
@Then("I wait file named '(.*)' to be downloaded with timeout of '(.*)' seconds[\\.|\\?]")
public void waitDownloadFile(String file, int timeout, List<GherkinStepCondition> conditions) throws InterruptedException, FailureException, TechnicalException {
File f;
int nbTry = 0;
do {
if (nbTry >= timeout) {
new Result.Failure<>(file, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_DOWNLOADED_FILE_NOT_FOUND), file), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
Thread.sleep(1000);
f = new File(System.getProperty(USER_DIR) + File.separator + DOWNLOAD_FILES_FOLDER + File.separator + file);
nbTry++;
} while (!(f.exists() && !f.isDirectory()));
logger.debug("File downloaded in {} seconds.", nbTry);
}

/**
* Wait invisibility of element with timeout of x seconds.
* Waits invisibility of element with timeout of x seconds.
*
* @param page
* The concerned page of field
Expand All @@ -139,7 +93,7 @@ public void waitInvisibilityOf(String page, String element, int time, List<Gherk
}

/**
* Wait staleness of element with timeout of x seconds.
* Waits staleness of element with timeout of x seconds.
*
* @param page
* The concerned page of field
Expand All @@ -160,22 +114,9 @@ public void waitStalenessOf(String page, String element, int time, List<GherkinS
Context.waitUntil(ExpectedConditions.stalenessOf(we), time);
}

@Conditioned
@Alors("Le fichier '(.*)' encodé en '(.*)' vérifie '(.*)'[\\.|\\?]")
@Then("The file '(.*)' encoded in '(.*)' matches '(.*)'[\\.|\\?]")
public void checkFile(String file, String encoding, String regexp, List<GherkinStepCondition> conditions) throws TechnicalException, FailureException {
try {
Matcher m = Pattern.compile(regexp).matcher(FileUtils.readFileToString(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOAD_FILES_FOLDER + File.separator + file), encoding));
if (!m.find()) {
new Result.Failure<>(file, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_FILE_NOT_MATCHES), file, regexp), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
} catch (IOException e) {
new Result.Failure<>(file, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_FILE_NOT_FOUND), file), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
}

/**
* Loop on steps execution for a specific number of times.
* Loops on steps execution for a specific number of times.
*
* @param actual
* actual value for global condition.
Expand All @@ -201,7 +142,7 @@ public void loop(String actual, String expected, int times, List<GherkinConditio
}

/**
* Do steps execution until a given condition is unverified.
* Does steps execution until a given condition is unverified.
*
* @param actual
* actual value for global condition.
Expand Down Expand Up @@ -233,7 +174,7 @@ public void doUntil(String actual, String expected, String key, String breakCond
}

/**
* While a given condition is verified, do steps execution.
* While a given condition is verified, does steps execution.
*
* @param actual
* actual value for global condition.
Expand Down
143 changes: 143 additions & 0 deletions src/main/java/com/github/noraui/application/steps/FileSteps.java
@@ -0,0 +1,143 @@
/**
* NoraUi is licensed under the licence GNU AFFERO GENERAL PUBLIC LICENSE
*
* @author Nicolas HALLOUIN
* @author Stéphane GRILLON
*/
package com.github.noraui.application.steps;

import static com.github.noraui.utils.Constants.DOWNLOADED_FILES_FOLDER;
import static com.github.noraui.utils.Constants.USER_DIR;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.noraui.application.page.Page;
import com.github.noraui.cucumber.annotation.Conditioned;
import com.github.noraui.exception.Callbacks;
import com.github.noraui.exception.FailureException;
import com.github.noraui.exception.Result;
import com.github.noraui.exception.TechnicalException;
import com.github.noraui.gherkin.GherkinStepCondition;
import com.github.noraui.utils.Context;
import com.github.noraui.utils.Messages;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.fr.Alors;
import cucumber.api.java.fr.Lorsque;

/**
* This class contains Gherkin callable steps that handle file uploading and downloading.
*/
public class FileSteps extends Step {

/**
* Specific logger
*/
private static final Logger logger = LoggerFactory.getLogger(CommonSteps.class);

/**
* Empties the default downloaded files folder
*
* @param conditions
* List of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
* @throws IOException
*/
@Conditioned
@Lorsque("Je vide le repertoire des téléchargements[\\.|\\?]")
@Given("I clean download directory[\\.|\\?]")
public void cleanDownloadDirectory(List<GherkinStepCondition> conditions) throws IOException {
FileUtils.forceMkdir(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER));
FileUtils.cleanDirectory(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER));
}

/**
* Checks that a file in the default downloaded files folder matches the given regexp.
*
* @param file
* The name of the file
* @param encoding
* The file encoding
* @param regexp
* The pattern to match
* @param conditions
* List of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
* @throws TechnicalException
* @throws FailureException
*/
@Conditioned
@Alors("Le fichier '(.*)' encodé en '(.*)' vérifie '(.*)'[\\.|\\?]")
@Then("The file '(.*)' encoded in '(.*)' matches '(.*)'[\\.|\\?]")
public void checkFile(String file, String encoding, String regexp, List<GherkinStepCondition> conditions) throws TechnicalException, FailureException {
try {
final Matcher m = Pattern.compile(regexp)
.matcher(FileUtils.readFileToString(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + file), encoding));
if (!m.find()) {
new Result.Failure<>(file, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_FILE_NOT_MATCHES), file, regexp), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
} catch (final IOException e) {
new Result.Failure<>(file, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_FILE_NOT_FOUND), file), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
}

/**
* Waits the full download of a file with a maximum timeout in seconds.
*
* @param file
* The name of the file to download
* @param timeout
* The maximum timeout
* @param conditions
* List of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
* @throws InterruptedException
* Exception for the sleep
* @throws TechnicalException
* @throws FailureException
*/
@Conditioned
@Lorsque("J'attends que le fichier nommé '(.*)' soit téléchargé avec un timeout de '(.*)' secondes[\\.|\\?]")
@Then("I wait file named '(.*)' to be downloaded with timeout of '(.*)' seconds[\\.|\\?]")
public void waitDownloadFile(String file, int timeout, List<GherkinStepCondition> conditions) throws InterruptedException, FailureException, TechnicalException {
File f;
int nbTry = 0;
do {
if (nbTry >= timeout) {
new Result.Failure<>(file, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_DOWNLOADED_FILE_NOT_FOUND), file), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
}
Thread.sleep(1000);
f = new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + file);
nbTry++;
} while (!(f.exists() && !f.isDirectory()));
logger.debug("File downloaded in {} seconds.", nbTry);
}

/**
* Waits the full download of a file with a maximum timeout in seconds.
*
* @param page
* The page to upload the file from
* @param element
* The file input field
* @param filename
* The name of the file to upload (from the default downloaded files directory)
* @param conditions
* List of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
* @throws TechnicalException
* @throws FailureException
*/
@Conditioned
@Lorsque("J'utilise l'élément '(.*)-(.*)' pour uploader le fichier '(.*)'[\\.|\\?]")
@Then("I use '(.*)-(.*)' element to upload '(.*)' file[\\.|\\?]")
public void uploadFile(String page, String element, String filename, List<GherkinStepCondition> conditions) throws FailureException, TechnicalException {
uploadFile(Page.getInstance(page).getPageElementByKey('-' + element), filename);

}
}

0 comments on commit c4ce448

Please sign in to comment.