1

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
}
je2tdam
  • 177
  • 1
  • 3
  • 15
  • If you're going this route, I'd look at [Geb](http://gebish.org) and particularly Page Objects. – chrylis -cautiouslyoptimistic- May 15 '17 at 21:00
  • Just to clarify: you want to re-compile an existing class fo your java-program and reload it, all while the program is running? – Turing85 May 15 '17 at 21:00
  • Why not make the login process a service that is independent of the rest of the application? Then it could be modified and re-deployed independently of the main application – Alex May 15 '17 at 21:51

1 Answers1

1

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:

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:

Community
  • 1
  • 1
Arthur Landim
  • 384
  • 2
  • 9