Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #119

Merged
merged 1 commit into from
Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class EventType
public static final String KEY_UP = "keyup";
public static final String KEY_DOWN = "keydown";
public static final String MOUSE_DOWN = "mousedown";
public static final String MOUSE_UP = "mouseup";
public static final String XHR = "xhr";
public static final String KEY_PRESS = "keypress";
public static final String HASH_CHANGE = "hashchange";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void applyStep(UserScenario scenario, SeleniumDriver seleniumDriver, int
LOG.info("Current step eventId " + event.get(EventConstants.EVENT_ID).toString() + " URL: {}",
event.get(EventConstants.URL));

WebDriver theWebDriver = null;
WebDriver webDriver = null;
boolean error = false;
CommonConfiguration commonConfiguration = scenario.getConfiguration().getCommonConfiguration();
try
Expand Down Expand Up @@ -205,18 +205,18 @@ public void applyStep(UserScenario scenario, SeleniumDriver seleniumDriver, int
.setGetWebDriverPidScript(scriptsConfiguration.getGetWebDriverPidScript())
.setKeepBrowserXpath(commonConfiguration.getFormOrDialogXpath());

theWebDriver = getWebDriver(scenario, seleniumDriver, event);
if (theWebDriver == null)
webDriver = getWebDriver(scenario, seleniumDriver, event);
if (webDriver == null)
{
throw new NullPointerException("getWebDriver return null");
}
seleniumDriver.openEventUrl(theWebDriver, event);
seleniumDriver.openEventUrl(webDriver, event);

LOG.info("Event {}. Display {}", position, seleniumDriver.getDriverDisplay(theWebDriver));
LOG.info("Event {}. Display {}", position, seleniumDriver.getDriverDisplay(webDriver));

seleniumDriver.waitWhileAsyncRequestsWillCompletedWithRefresh(theWebDriver, event);
seleniumDriver.waitWhileAsyncRequestsWillCompletedWithRefresh(webDriver, event);

FrameSwitcher.switchToWorkingFrame(theWebDriver, event);
FrameSwitcher.switchToWorkingFrame(webDriver, event);

try
{
Expand All @@ -225,30 +225,30 @@ public void applyStep(UserScenario scenario, SeleniumDriver seleniumDriver, int
switch (type)
{
case EventType.MOUSE_WHEEL:
seleniumDriver.processMouseWheel(theWebDriver, event, target);
seleniumDriver.processMouseWheel(webDriver, event, target);
break;
case EventType.SCROLL_EMULATION:
seleniumDriver.processScroll(theWebDriver, event, target);
seleniumDriver.processScroll(webDriver, event, target);
break;
case EventType.MOUSE_DOWN:
case EventType.CLICK:
seleniumDriver.processMouseEvent(theWebDriver, event);
seleniumDriver.processMouseEvent(webDriver, event);
break;
case EventType.KEY_UP:
case EventType.KEY_DOWN:
seleniumDriver.processKeyDownKeyUpEvents(theWebDriver, event);
seleniumDriver.processKeyDownKeyUpEvents(webDriver, event);
break;
case EventType.KEY_PRESS:
seleniumDriver.processKeyPressEvent(theWebDriver, event);
seleniumDriver.processKeyPressEvent(webDriver, event);
break;
default:
break;
}

seleniumDriver.waitWhileAsyncRequestsWillCompletedWithRefresh(theWebDriver, event);
new PlayerScriptProcessor(scenario).doWaitAfterEvent(seleniumDriver, theWebDriver, event);
seleniumDriver.waitWhileAsyncRequestsWillCompletedWithRefresh(webDriver, event);
new PlayerScriptProcessor(scenario).doWaitAfterEvent(seleniumDriver, webDriver, event);

throwIfBrowserHaveAnError(scenario, theWebDriver);
throwIfBrowserHaveAnError(scenario, webDriver);
}
catch (Exception e)
{
Expand All @@ -264,15 +264,15 @@ public void applyStep(UserScenario scenario, SeleniumDriver seleniumDriver, int
finally
{
//webdriver can stay null if event is ignored or bad, thus can`t be postprocessed
if (theWebDriver != null)
if (webDriver != null)
{
if (!error)
{
scenario.updateEvent(event);
new PlayerScriptProcessor(scenario).runStepPrePostScript(event, position, false);
}
makeAShot(scenario, seleniumDriver, theWebDriver, position, error);
seleniumDriver.releaseBrowser(theWebDriver, event);
makeAShot(scenario, seleniumDriver, webDriver, position, error);
seleniumDriver.releaseBrowser(webDriver, event);
}
else
{
Expand Down Expand Up @@ -304,6 +304,7 @@ public void play(UserScenario scenario, SeleniumDriver seleniumDriver, int start
{
while (scenario.getPosition() != maxPosition)
{
LOG.info("=========================================================================================");
LOG.info("Step " + scenario.getPosition());
applyStep(scenario, seleniumDriver, scenario.getPosition());
scenario.moveToNextStep();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.focusit.jsflight.player.script;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Nullable;

import com.focusit.jsflight.player.constants.EventConstants;
import com.focusit.jsflight.player.scenario.UserScenario;
import com.focusit.jsflight.player.webdriver.SeleniumDriver;
import com.focusit.jsflight.script.ScriptEngine;
import com.focusit.jsflight.script.constants.ScriptBindingConstants;
import groovy.lang.Binding;
import groovy.lang.Script;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
Expand All @@ -18,16 +16,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.focusit.jsflight.player.constants.EventConstants;
import com.focusit.jsflight.player.scenario.UserScenario;
import com.focusit.jsflight.player.webdriver.SeleniumDriver;
import com.focusit.jsflight.script.ScriptEngine;
import com.focusit.jsflight.script.constants.ScriptBindingConstants;

import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import groovy.text.GStringTemplateEngine;
import javax.annotation.Nullable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* PlayerScriptProcessor that runs groovy scripts or GString templates
Expand All @@ -36,13 +31,10 @@
*/
public class PlayerScriptProcessor
{
private static final GStringTemplateEngine templateEngine = new GStringTemplateEngine(
new GroovyClassLoader(ScriptEngine.getClassLoader()));
private static final Logger LOG = LoggerFactory.getLogger(PlayerScriptProcessor.class);

static
{
System.setProperty("groovy.GStringTemplateEngine.reuseClassLoader", "true");
Velocity.init();
}

Expand Down Expand Up @@ -109,7 +101,7 @@ public boolean executeDuplicateHandlerScript(String script, JSONObject currentEv
}
catch (Throwable e)
{
LOG.warn("Failed to create duplicateHandler script. Default value is false", e);
LOG.warn("duplicateHandler script execution failed. Default value is false", e);
return false;
}
}
Expand All @@ -133,14 +125,7 @@ public void preProcessScenario(String script, List<JSONObject> events)
Map<String, Object> binding = getEmptyBindingsMap();
binding.put(ScriptBindingConstants.EVENTS, events);

try
{
executeGroovyScript(script, binding);
}
catch (Throwable e)
{
LOG.error(e.getMessage(), e);
}
executeGroovyScript(script, binding);
}

public void runStepPrePostScript(JSONObject event, int step, boolean pre)
Expand All @@ -159,13 +144,7 @@ public void runStepPrePostScript(JSONObject event, int step, boolean pre)
binding.put(ScriptBindingConstants.PRE, pre);
binding.put(ScriptBindingConstants.POST, !pre);

try
{
executeGroovyScript(script, binding);
}
catch (Throwable ignored)
{
}
executeGroovyScript(script, binding);
}

public JSONObject runStepTemplating(UserScenario scenario, JSONObject step)
Expand Down Expand Up @@ -251,6 +230,11 @@ public <T> T executeGroovyScript(String scriptBody, Map<String, Object> bindings
LOG.error(ex.getMessage(), ex);
return null;
}
catch (Throwable ex)
{
LOG.error("Script execution failed", ex);
throw ex;
}
}

private void addBasicBindings(Binding binding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private void clickInternal()
{
throw e;
}
LOG.warn(e.toString(), e);
LOG.warn("Click via interface failed. Clicking using JavaScript");
((JavascriptExecutor)driver).executeScript("arguments[0].click()", delegate);
}
}
Expand Down
Loading