How to login to the application with username and password from database in Selenium?



  • 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)



  • 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: http://en.wikipedia.org/wiki/MD5 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2