Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package DatabaseTest;
  2.  
  3. import java.sql.*;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.By;
  7.  
  8. public class SeleniumDatabaseTest {
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         //Test Data
  13.         String user_name = "test1";
  14.         String user_passwd = "test1";
  15.        
  16.         //Selenium
  17.         WebDriver driver = new FirefoxDriver();
  18.         driver.get("http://127.0.0.1:81/projautomatyzacja");
  19.         //driver.findElement(By.xpath("html/body/form/input[1]")).sendKeys(user_name);
  20.         //driver.findElement(By.xpath("html/body/form/input[2]")).sendKeys(user_passwd);
  21.         //driver.findElement(By.xpath("html/body/form/input[3]")).click();
  22.        
  23.         //DB Connection
  24.         Connection conn = null;
  25.        
  26.         String url = "jdbc:mysql://localhost:3306/sys";
  27.         String username = "root";
  28.         String password = "root";
  29.        
  30.         try{
  31.             Class.forName("com.mysql.jdbc.Driver");
  32.             conn = DriverManager.getConnection(url, username, password);
  33.            
  34.             String sqlQuery = "Select * from databasetest";
  35.             Statement statement = conn.createStatement();
  36.             ResultSet result = statement.executeQuery(sqlQuery);
  37.            
  38.             if(!result.next()){
  39.                 System.out.println("No Data Found");  
  40.             }
  41.            
  42.             else do {
  43.                 result.next();
  44.                 String login = result.getString("user_name");
  45.                 String haslo = result.getString("user_passwd");
  46.                
  47.                 driver.findElement(By.xpath("html/body/form/input[1]")).sendKeys(login);
  48.                 driver.findElement(By.xpath("html/body/form/input[2]")).sendKeys(haslo);
  49.                 driver.findElement(By.xpath("html/body/form/input[3]")).click();
  50.            
  51.             if(driver.findElement(By.className("poprawne")) != null)
  52.                 System.out.println(result.getString("user_name"));
  53.                 System.out.println("Wynik testu logowania pozytywny");
  54.                
  55.             } while (result.next());
  56.            
  57.            
  58.         //  System.out.println(result.getString("user_name"));
  59.         //  System.out.println(result.getString("user_passwd"));
  60.            
  61.             //Database Test
  62.         //  if(!result.getString("user_name").equals(user_name))
  63.         //      System.out.println("Wrong username fetched from DB");
  64.         //  if(!result.getString("user_passwd").equals(user_passwd))
  65.         //      System.out.println("Wrong password fetched from DB");
  66.            
  67.            
  68.                
  69.         }
  70.         catch(Exception e){
  71.             System.out.println(e);
  72.         }
  73.         finally{
  74.             if(conn!=null){
  75.                 conn=null;//Close the DB Connection
  76.             }
  77.         }
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement