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("token")).clear();
                 driver.findElement(By.id("token")).sendKeys(token);
 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 based on login type I can use the corresponding login module ? Also since they are different login module they have different flow so in some case i need to just enter username and password but in other case i need a token. So i just can save variables in db and then use them because of different logic.

// Setting webdriver Selenium
        String Xport = System.getProperty("lmportal.xvfb.id", ":"+Bootstrap.prop.getProperty("SELENIUMDISPLAYPORT"));
        final File firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", Bootstrap.prop.getProperty("FIREFOX_LOCATION")));
        FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
        firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference(FirefoxProfile.ALLOWED_HOSTS_PREFERENCE, "localhost.localdomain");

        String PROXY = "";

        PROXY = "localhost:" + proxyPort;

        org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
        proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY);
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(CapabilityType.PROXY, proxy);

        // Start Firefox driver
        driver = new FirefoxDriver(firefoxBinary, profile, cap);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        // Login
        try {

                driver.get(url);
                utils = new Utils(driver);
                if (loginType == 1){
                     driver.findElement(By.id("token")).clear();
                     driver.findElement(By.id("token")).sendKeys(token);
                     driver.findElement(By.id("signin")).click();
                     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();            
                } 
             .................
...................................
  • 1
    Do you need a permission to implement it the way you describe? Here it is. Do you want us to implement it for you? Unlikely. – Peter M. May 18 '17 at 18:57
1

I'll admit I don't see much difference between your two samples.

I'd approach this with a Page Object Model pattern where the Login object has different login methods.

That would make my login code a little more like this:

loginPage loginObject = new loginObject();
// login type uses token - might need several of these if the expected next Url changes
currentUrl = loginObject.loginWithToken(tokenValue, usernameValue, passwordValue);
// login type without token
currentUrl = loginObject.loginBasic(usernameValue, passwordValue);

In your login page object you would have private variables representing the fields you're interacting with to define them just once. These get defined in the constructor.

Ideally your loginPage class inherits from a base Page class that handles defining the driver and has the find methods so instead of needing the lengthy calls to driver.findElement(By.id("magicstring"))doSomething(variable); your page method can simply call a base method like parent.SetText(identifier, value);

A psuedo-cody sample is below.

public class loginPage
{
    public loginPage loginPage()
    {
          string tokenid = "token";
          string usernameid = "username";
          // etc...
    }

    public Url loginWithToken(string token, string user, string pass)
    {
       this.parent.setText(tokenid, token);
       this.parent.setText(usernameid, user);
       // etc...
       this.parent.clickButton(loginbuttonid);
       this.parent.waitForLoad(expectedNextUrl);
       return this.parent.currentUrl();
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.