java


Compile java code at runtime


I have a java class that is used to perform login action using selenium. There are currently 10+ different login types and as such there is a lot of if else involved which looks bad and is not efficient.
Eg:
if (logintype == 1 )
{
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("signin")).click();
}
else if (logintype ==2 )
{
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("signin")).click();
}
...........
...........
Other than code not being efficient the new code needs to be written, pushed and the server needs to be restarted every time a new login module is added.
I wanted to see if i can get the logic for login can be stored in db and if it can be compiled at runtime. I found groovy shell but i dont know how to get the results back to my class file. Also running groovy shell would require a lot of code changes. Is it possible in java
public class ExecuteAuth implements Runnable{
private WebDriver driver;
driver = new FirefoxDriver(firefoxBinary, profile, cap);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
//MongoDB code
DBCursor dbObjects = loginCollection.find();
while (dbObjects.hasNext()) {
DBObject dbObject = dbObjects.next();
loginModule.add(new LoginModule((BasicDBObject) dbObject));
String loginType = (String) dbObject.get("loginType")
String script;
if (loginType.equals("1")) {
script = (String) dbObject.get("script")
}
}
GroovyShell shell = new GroovyShell ();
shell.evaluate(script);
RUN REST OF THE LOGIN LOGIC AFTER THE CODE IS EVALUATED
}
I strongly advise against that approach. You are opening a door to bad code be injected in your application. Another way could be upload to your server your new jars and take advantage of class loader to load classes at runtime:
How should I load Jars dynamically at runtime?
Also, you have alternatives to avoid if-else's: usage of interfaces and factory methods are the way to go, imho. And put your login's implementations on different classes implementing a Login interface, for example.
Factory method design pattern:
http://www.oodesign.com/factory-method-pattern.html
http://www.javaworld.com/article/2077386/learn-java/factory-methods.html

Related Links

GUI calculation issue [closed]
jersey rest service with tomcat not working with welcome file configuration in web.xml
Optimum Fragment Code in Android Studio
Checking amount of booleans that are true, and executing differently depending on the amount [closed]
How can I get the newest version of google play services on an AVD?
Enable to run AWS beanstalk project with opencv
Confusing behavior of ShellBolt tasks - Apache Storm
Is it possible to convert UYVY(Y422, UYNV, HDYC) live feed to NV21 format in Android?
Apache Hive 1.2.1 not going to integrate with solr 4.10.3
Printing a string of 'n' characters by repeating a string
Deploy JavaFX application to Mac from Windows
Meaning of get generic method in hibernate
How to run Categorized Junit tests using Gradle?
jsonschema2pojo duplicate classes (not duplicate class names)
Comparing individual elements of two different arraylists?
Send message From Server To client JavaFX

Categories

HOME
mockito
relay
rubygems
alpha
directx
node-notifier
spring-kafka
quicklook
u-sql
timeout
spring-tool-suite
msp430
worldwind
serilog
database-replication
normalizr
code-review
csrf-protection
web-sql
tapestry
kvc
realex-payments-api
fgetcsv
large-file-upload
nat
winrt-xaml-toolkit
android-kernel
espeak
optix
galen
vao
outlook-api
http-redirect
import-from-excel
galleria
revapi
react-scripts
dds
prolog-setof
ionicons
typescript1.8
mplayer
magma
bind9
vtigercrm
pintos
yt-project
visual-c++-2008
firmata
storekit
flash-cs5
moveit
actionbardrawertoggle
whois
fadeout
independentsoft
pyke
historian
python-3.2
jmeter-maven-plugin
simple-framework
dukescript
mono-embedding
unity3d-gui
dia
sorl-thumbnail
qpainter
codeigniter-url
fluentautomation
jubula
stxxl
ccss
quartz-graphics
html4
padarn
mylyn
pushbackinputstream
random-seed
anonymous-methods
gil
mt
nsnetservice
ocx
dotnethighcharts
libc++
psi
datareader
h.323
sproutcore-2
mozilla-prism
modelstate
routedevent
webkit.net
brewmp
sector
putchar
django-notification

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile