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

doClick(), and Simon: All buttons will unpress at the same time instead of individually
Java Algorithms: comparing each *thing* to every other [closed]
Whats causing this NullPointerException in AWT-EventQueue-0?
Gradle + Dropwizard + Shadow -> Could not find or load main class
Android - Try to send fake sms to myself without mobile network usage
Selenium, open new window
Using Android TCP Client app to interface with sensor
Normal distribution function:determine probability of a given point in Java
Set object reference to null or call the finalize() method?
Are there alternatives to String.format that use cached formatting? [closed]
Can't register portlets on Liferay
Could not find or load main class SDK -jar
Change Eclipse's tooltip/hint/sugestions/overlay window color
Get GZIPped file attributes (like 'gzip -l', basically compression ratio)
Possible Interview Java .equals [duplicate]
No selenium tests run during mvn integration test

Categories

HOME
cakephp
hive
cloud
reflection
raspberry-pi
bookshelf.js
elm
sharepoint-designer
rubygems
jxls
uber-api
bs4
adfs
applepay
google-translate
vifm
node-pdfkit
mapserver
correlation
messages
msp430
jsprit
transformation
propel
beyondcompare
lucene.net
reverse-proxy
jasonette
firebase-crash-reporting
crosstab
java-7
apache-commons-io
status
virtualdub
nhapi
fluentvalidation
gitignore
libssl
quote
repo
overriding
service-discovery
c11
unspecified
unobtrusive-validation
cookiecutter-django
turbogears
heightmap
reactive-cocoa-5
galleria
smartcontracts
errordocument
gabor-filter
colorama
magma
pdfclown
flutterwave
smart-table
objective-c-swift-bridge
parallel-data-warehouse
android-fonts
grails-tomcat-plugin
home-directory
merge-conflict-resolution
storekit
dlna
dataview
oauth2client
rhino-servicebus
freedesktop.org
jfugue
vhd
reactive-banana
lemon
eclipse-clp
php-ci
nsight
qcodo
gulp-less
html-helper
neolane
castle
kgdb
viadeo
floating-point-precision
listings
soundtouch
seed
browser-detection
jspinclude
hirefire
coercion
celltable
android-sdk-2.1
lang
asp.net-mvc-areas
xsdobjectgen
perfect-hash
ncqrs
libs
phonon
pascal-fc

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html