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();
}
.................
...................................