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

Java local variable notation: 'textEntered' or 'enteredText'? [closed]
How SLF4J/ JPA / JAX-RS find their implementation?
Java - Creating multiple objects and making them distinguishable
Hibernate starts altering on tables which didn't created
Giving priority to some processes, without starvation for the others [closed]
JaCoCo coverage report not displayed in Sonarqube 5.3
How to change user in hdfs using sparkSubmit in java
What is the difference between Files.walk.filter and Files.find?
Java: Filtering Panels like a JTable
Auto Extracting XPATH of all html elements using JAVA
How to convert em dash in java
Is Java's import keyword for source files or binary files?
PostgreSQL function to insert into tables.. i need it to return specific values on errors and on specific if conditions
Have to click twice to get number, Java program not working as intended
Eclipse Java Compiler Errors
JSPExpression Language reading value from bean

Categories

HOME
wso2-am
blogger
pypi
webpack
drivers
single-sign-on
homebrew
webstorm
razor
spring-cloud-stream
microservices
jpeg
jxls
ravendb
fingerprint
fancybox
adobe-analytics
moonmail
leiningen
angular-ui
propel
flux
oxyplot
amazonsellercentral
chromium-embedded
zurb-foundation-6
devops
wijmo
blazemeter
java-7
android-widget
one-to-many
status
pingfederate
linkerd
web-api-testing
assistant
opentype
gitignore
socialengine
binary-data
key-value-observing
siesta-swift
winrt-xaml-toolkit
calibre
pim
avcapturesession
phpfreechat
y86
environment-modules
swift3.0.2
c11
email-templates
modelmapper
slick-3.0
executenonquery
atomicity
theming
python-c-api
blogengine.net
datastax-startup
magma
sybase-asa
windows-mobile-6.5
flow-control
minimization
static-ip-address
color-picker
passport-google-oauth
asp.net-4.5
mongocsharpdriver
pagedlist
fadeout
wso2cloud
qtableview
intel-fortran
twirl
processmodel
cisco-ios
system32
p2
bigbluebutton
sorl-thumbnail
tween
pick
codeigniter-url
code-access-security
php-5.4
terminfo
openexr
industrial
sitemesh
soundtouch
flash-builder4.5
ril
hungarian-algorithm
f#-powerpack
sql-server-profiler
imac
libs
phonon
project-hosting
scripting-languages
geneva-server

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
Mobile App
Mobile App
Mobile App