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

Passing JSON array - Android
how to connenct remote mysql connection in cpanel
Splash screen depends onClick to move on
Java: variable used in run method, initialized in for loop is not recognized in for loop's statement
JAVA loop moving objects from bottom of applet to top of applet
my button cannot find a method in the MainActivity.java file
toolbar.getChildCount() returns 0
PostgreSQL: going to run out of IDs in integer columns
Method still running after compiler reads it [closed]
how to check JBOSS version using ANT script
How can I reference a super method in a Java class that implements an interface but does not extend another class?
#ElementCollection of Enum in Embeddable
Declaring HiveContext in Spark shows an error
Unable to open the page in chrome using android emulator
How to validate XML after Maven filtering?
WebSocket Connection Established, but Client Can't Receive Messages from Others

Categories

HOME
magnific-popup
pycharm
office365api
alfresco
sqlite-net-extensions
razor
elm
microservices
jsrender
frameworks
dxl
fancybox
in-app-purchase
multiple-records
autotools
node-notifier
cross-validation
windows-10-universal
izpack
zebra-printers
decimal
text-rendering
code-review
alpine
visual-composer
dbext
jtextfield
web-sql
mustache.php
windows-error-reporting
subdomains
madlib
key-value-observing
copying
framemaker
protovis
android-kernel
git-merge
web-mining
libraries
service-discovery
glew
isbn
arena-simulation
avconv
fault
multipeer-connectivity
clean-architecture
darcs
tactic
mako
spring-mongodb
pintos
static-ip-address
android-textview
py2app
qpid
gridview-sorting
pickadate
php-internals
rhino-servicebus
vhd
xna-4.0
sdhc
content-length
rgeo
emailrelay
ios9.1
appfabric-cache
dundas
key-management
e10s
operator-precedence
gwidgets
file-copying
googlemock
code-access-security
imdbpy
mysqltuner
sitemesh
spring-validator
sabredav
trusted
windows-phone-7.1.1
dotnethighcharts
cufon
transactionscope
whoosh
google-friend-connect
getresponsestream
xsdobjectgen
preference
microsoft-virtualization
noscript
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
Mobile App
Mobile App
Mobile App