Skip to content

Instantly share code, notes, and snippets.

View azakordonets's full-sized avatar

Andrew Zakordonets azakordonets

View GitHub Profile
@azakordonets
azakordonets / getDate.java
Last active August 29, 2015 14:01
get date as a string basing on date format and number of days since now
/**
* This method allows to generate time stamp with predefined format, but not only from today, but from date that we
* pass as a parameter to the method.
* If days = 0 , then we return current date stamp
* If number is > 0, then we return date in the future
* If number is < 0 then we return date from the past
* @param days number of days since stated date
* @param format it's a format in which date string will be returned
* @return date as a String
*/
@azakordonets
azakordonets / getLastDayOfTheMonth.java
Created May 19, 2014 15:33
Getting last day of the month
/**
* This method will return as a date string of the last day of the month.
* @param month if 0 then we look for current month
* @param format format in which date should be returned as a string
* @return
*/
public static String getEndOfTheMonth(int month, String format){
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date today = new Date();
Calendar calendar = Calendar.getInstance();
@azakordonets
azakordonets / getAbsolutePath.java
Created May 21, 2014 14:24
returns absolute path to the file
public static String getResourcePath(String relativePath) {
return new Utils().getClass().getResource("/").getPath() + relativePath;
}
@azakordonets
azakordonets / createFileInDirectory.java
Created May 22, 2014 09:23
This method allow you to create file in specific folder under your project root. If you want to to save it somewhere on your computer, you can use "user.home" instead of "user.dir"
// get System properties :
Properties properties = System.getProperties();
// get Operating System home directory
String home = properties.get("user.dir").toString();
// get Operating System separator
String separator = properties.get("file.separator").toString();
// your directory name
String directoryName = "directoryName";
// your file name
String fileName = "filename";
@azakordonets
azakordonets / RetryStrategy.md
Last active August 29, 2015 14:01
Class for proper handling of retrying things to do

This class gives you nice way of retrying some action several times to make sure that either works or not. Here's an example of the code where this class can be used :

/**
	 * Get value of an element using id
	 * 
	 * @param id				the location of the element
	 * @param attribute			name of an attribute
	 * @return attribute value	value that was in attribute
	 */
	public String getValueFromId(String id, String attribute) throws Exception {
@azakordonets
azakordonets / WaitForPageToLoad.java
Created June 2, 2014 10:17
This method allows you to properly wait for page in load with WebDriver
public void waitForPageToLoad() {
Wait<WebDriver> wait = new WebDriverWait(manager.getDriver(), 30);
wait.until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
System.out.println("Current Window State : "
+ String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState")));
return String
.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
.equals("complete");
}
public int randomIntGenerator(int minimum, int maximum) {
Random rn = new Random();
return rn.nextInt(maximum - minimum) + minimum;
}
public void selectFromDropDownWithHover(String menuTitle, String menuItem, String locatorType) throws Exception {
Actions actions = new Actions(manager.getDriver());
//first we find the menu title on which we will hover mouse cursor
WebElement menuHoverLink = findElementByLocator(locatorType, menuTitle);
//now we hover it with mouse
actions.moveToElement(menuHoverLink).perform();
//let's find our menu item
WebElement menuItemElement = findElementByLocator(locatorType, menuItem);
actions.moveToElement(menuItemElement);
actions.click().build().perform();
/**
* A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
* @param slider webElement
* @param xOffset - horizontal move offset. Positive value means moving to the right, negative - to the left
* @param yOffset - vertical move offset. Positive value means moving up, negative - down
* @throws Exception
*/
public void dragAndDrop(WebElement slider, int xOffset, int yOffset) throws Exception {
Actions moveSlider = new Actions(driver);
Action action = moveSlider.clickAndHold(slider)
public void dragAndDrop2(WebElement element, int xOffset, int yOffset) throws Exception {
Actions builder = new Actions(driver;
Action dragAndDrop = builder.dragAndDropBy(element, xOffset, yOffset) .build();
dragAndDrop.perform();
}