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

Unable to align chat messages according to the sender in RecyclerView
Querydsl - Exception in thread “main” java.lang.IllegalArgumentException: No sources given
java.io.FileNotFoundException: (Operation not permitted) error with ./keytool -import on mac osx (el capitan) - Java 6
Eclipse 3.8 to 4.6 Migration Platform.getPlugin() returns null
Unmarshalling “any” type from inbound schema
Android: Fragment is not attached to Activity error
Running job in hadoop- ERROR security.UserGroupInformation
Obtain specific part of URL
Spring - Embdedded H2 Test Db
How would I display an Image, and overlay a button in Java? [duplicate]
How to change textview text from another activity on button click in android studio?
Three-level nesting in elasticsearch
InvalidSelectorException - Selenium Webdriver support to recognize accent letters
Form validation with Spring Security login page
Python - IPython Notebook fails to find the Java executable
JBehave reuse steps from dependent projects

Categories

HOME
winforms
hive
cloud
hook
raspberry-pi
sd-card
getelementsbytagname
tizen-web-app
packages
icloud
callback
pivotal-cloud-foundry
flyway4
pheatmap
facebook-php-sdk
reveal.js
n-gram
fortumo
decimal
iolanguage
serverless-framework
saxon
jtextfield
opencover
ehcache
qhull
opentype
unboundid
bluestacks
iframe-resizer
oracle-fusion-middleware
catel
webtest
caret
mapdb
suricata
stacked
theano.scan
leading-zero
greenrobot-eventbus
texmaker
thin
btrace
avconv
dotcover
chain-builder
alphabet
domain-model
mu
recursive-datastructures
worker-thread
ifs
imanage
query-performance
medium.com
pcf
merge-conflict-resolution
qcustomplot
kcachegrind
p2
graph-api-explorer
facebook-graph-api-v2.4
jsonpickle
network-interface
qcodo
java-metro-framework
mesa
aapt
navigationservice
elliptic-curve
android-2.2-froyo
centos5
dir
jmapviewer
flash-builder4.5
gwt-rpc
plasma
ocunit
free-variable
simba
mhtml
bitsharp
dice
getmessage
qtkit
coda-slider
nsviewanimation
memory-size
uimenucontroller
thread-local-storage
mdac
misv

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