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

How to change tooltip text of a JavaFX stage default close (aka 'X') button?
What is the output of this code in Java?
table Expenses has no column named category [duplicate]
How the annotation #ApplicationPath from JAX-RS works side to side with a non-rest resource?
BDD Cucumber reporting issue in Jenkins
In debug mode I can see extra array list elements and are initialized to zero
JavaFx ComboBox valueProperty().addListener(new ChangeListener<String>() progressively repeated
Skip list remove and print method in java
How to start new line after press keyboard key is “ENTER” in SWT table cell?
ECIES encryption/decryption Java
For JSONArray parsing: TRY inside LOOP or LOOP inside TRY?
InstanceAlreadyExistsException coming from kafka consumer
Pong Game in java
Unable to Configure log4j2(v2.5) using dynamically changing configuration read from properties file
PostgreSQL function taking too long to complete execution
Need help positioning in java

Categories

HOME
hp-exstream
hashmap
jsrender
installshield
directx
bs4
tomcat6
spring-xd
dax
php-7.1
phaser
quickfix
jsprit
openrefine
excel-vba-mac
beyondcompare
alpine
autocad-plugin
abi
errorlevel
accessor
minitab
bpel
sql-server-2012-express
caret
asset-pipeline
stacked
dosbox
xcode-extension
lto
multilingual
objectlistview
gzipstream
flickr-api
react-scripts
pubmed
blogengine.net
powercli
slickedit
python-cryptography
zendesk-app
vtigercrm
synchronous
heidisql
radtreelist
py2app
connect-by
disque
markojs
simplewebrtc
react-native-listview
qcustomplot
make-install
sdf
ipconfig
system.management
wso2cloud
jmeter-maven-plugin
kcachegrind
emailrelay
interrupted-exception
method-parameters
processmodel
composite
oberon
directoryservices
javax.mail
c3
qpainter
java.util.concurrent
terminfo
project-planning
openexr
eol
funcunit
braille
imdbpy
dataservice
sabredav
eventlistener
isnullorempty
frameset
enter
tomcat-valve
f#-powerpack
celltable
locate
subviews
suppress
filtered-index
zend-decorators
port-number
zune
msdev
paul-graham
uiq3

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