2

I am using selenium webdriver with Java jdbc. Below are my queries.

First: I am trying to login to the application with username and password from database (Using SQL Query). I am getting the username and password, but password is in encrypted format. So how to decrypt the password? and use it for login?.

Second: Suppose, if I want to login with all the username and password from the database then how to write the code or query.

Below is the code sample:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class Test123 {
     WebDriver driver;
     String url ="";
     @BeforeTest
public void setUp() throws Exception{
     driver = new FirefoxDriver();
     url = "localhost:12345/login.do";
     driver.get(url);
}
     @Test
     public void CreateDB() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
           String url1 ="jdbc:mysql://123.0.0.1:12345/actitime";
           String dbClass = "com.mysql.jdbc.Driver";
           Class.forName(dbClass).newInstance();
           Connection con = DriverManager.getConnection(url1,"test", "test1");
           Statement stmt = (Statement) con.createStatement();
           ResultSet result = (ResultSet) stmt.executeQuery("SELECT * FROM at_user where username='s'");
           if(result.next())
           {
                String id = result.getString(2);
                String info = result.getString(4);
                driver.getCurrentUrl();
                WebElement a = driver.findElement(By.id("username"));
                a.sendKeys(id);
                WebElement b = driver.findElement(By.xpath(".//*[@id='loginFormContainer']/tbody/tr[1]/td/table/tbody/tr[2]/td/input"));
                b.sendKeys(info);
                WebElement btnclick = driver.findElement(By.xpath(".//*[@id='loginButton']/div"));
                btnclick.click();
                System.out.print("Passed");
           }
     }
     @AfterTest
public void tearDown(){
     driver.close();
}
}

Username and Password are

s (Username)

03c7c0ace395d80182db07ae2c30f034 (Password)

1 Answer 1

1

First: You have md5 encrypted passwords. When it is theoretically no way of decrypting md5 encrypted password there is a way to decrypt a MD5 hash, using a dictionary populated with strings and their MD5 counterpart. As most users use very simple passwords (like "123456", "password", "abc123", or in your case 's'), MD5 dictionaries make them very easy to retrieve. However if it is the case - it is major security issue and should be fixed by combining hash algorithms or adding "salt".

For more details consider looking into following pages:

  1. http://en.wikipedia.org/wiki/MD5
  2. http://md5.gromweb.com/

Second: The best way to handle multiple users within code is to create separate class for users and use a list of such objects, or, if you only need user names and passwords, to use dictionary where username is a key and password is a value with simple iteration based on that. The best way is not to use application database directly (because on secure system you will not be able to access user accounts anyway) but to create several test accounts with predefined usernames and passwords for your tests.

3
  • MD5 is not an encryption algorithm; it is a hash algorithm. Encryption is a reversible process (otherwise there is no point in encrypting); hashing is not reversible.
    – user246
    Aug 12, 2014 at 12:06
  • You're right indeed, however I just wanted to explain about "encryption" in terms of question Aug 12, 2014 at 14:20
  • 1
    The Semantics of MD5 being a Hash is beyond the scope of the question and the post mentions there is no way to correctly 'decrypt' an MD5 hash. Upvoted because of the suggestion for the dictionary. Using test specific accounts will be the best option IMO.
    – Paul Muir
    Aug 13, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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