Skip to content

Commit

Permalink
feat(WebDriverSampler): set sampler start Time (#37)
Browse files Browse the repository at this point in the history
* feat(WebDriverSampler): set sampler start Time

set sampler startTime and endTime

* feat(WebDriverSampler): format code and simpler catch exception
  • Loading branch information
linvaux authored Aug 11, 2024
1 parent d6a5be8 commit 00bc3c1
Showing 1 changed file with 10 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public WebDriverSampler() {
try {
srClass = (Class<SampleResult>) Class.forName(className);
} catch (ClassNotFoundException e) {
LOGGER.warn("Class " + className + " not found, defaulted to " + SampleResult.class.getCanonicalName(), e);
LOGGER.warn("Class {} not found, defaulted to {}", className, SampleResult.class.getCanonicalName(), e);
srClass = SampleResult.class;
}
sampleResultClass = srClass;
Expand All @@ -65,45 +65,30 @@ public SampleResult sample(Entry e) {
throw new IllegalArgumentException("Browser has not been configured. Please ensure at least 1 WebDriverConfig is created for a ThreadGroup.");
}

SampleResult res = null;
SampleResult res;
try {
res = sampleResultClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e1) {
LOGGER.warn("Class {} failed to instantiate, defaulted to {}", sampleResultClass, SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
} catch (IllegalAccessException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
} catch (IllegalArgumentException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
} catch (InvocationTargetException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
} catch (NoSuchMethodException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
} catch (SecurityException e1) {
LOGGER.warn("Class " + sampleResultClass + " failed to instantiate, defaulted to " + SampleResult.class.getCanonicalName(), e1);
res = new SampleResult();
}
}
res.setSampleLabel(getName());
res.setSamplerData(toString());
res.setDataType(SampleResult.TEXT);
res.setContentType("text/plain");
res.setDataEncoding("UTF-8");
res.setSuccessful(true);

LOGGER.debug("Current thread name: '" + getThreadName() + "', has browser: '" + getWebDriver() + "'");

LOGGER.debug("Current thread name: '{}', has browser: '{}'", getThreadName(), getWebDriver());
res.sampleStart();
try {
final ScriptEngine scriptEngine = createScriptEngineWith(res);
scriptEngine.eval(getScript());

// setup the data in the SampleResult
res.setResponseData(getWebDriver().getPageSource(), null);
res.setURL(new URL(getWebDriver().getCurrentUrl()));
if(StringUtils.isEmpty(res.getResponseCode())) {
if (StringUtils.isEmpty(res.getResponseCode())) {
res.setResponseCode(res.isSuccessful() ? "200" : "500");
}
if (res.isSuccessful()) {
Expand All @@ -115,13 +100,8 @@ public SampleResult sample(Entry e) {
res.setResponseData((ex.toString() + "\r\n" + JMeterPluginsUtils.getStackTrace(ex)).getBytes());
res.setResponseCode("500");
res.setSuccessful(false);
if (res.getStartTime() == 0) {
res.sampleStart();
}

if (res.getEndTime() == 0) {
res.sampleEnd();
}
} finally {
res.sampleEnd();
}

return res;
Expand Down

0 comments on commit 00bc3c1

Please sign in to comment.