Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.sql.*;
- import jxl.write.WriteException;
- import org.openqa.selenium.By;
- import org.openqa.selenium.NoSuchElementException;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.chrome.ChromeOptions;
- import org.testng.Assert;
- import org.testng.TestNGException;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.Test;
- public class Database {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost:3306/test";
- static final String USER = "root";
- static final String PASS = "";
- // Connection con;
- static Statement smt = null;
- static WebDriver driver;
- ResultSet set;
- @BeforeClass
- public static void setup() throws TestNGException, IOException, WriteException
- {
- System.setProperty("webdriver.chrome.driver", "E:\Selenium Jar\chromedriver.exe");
- ChromeOptions options = new ChromeOptions();
- options.addArguments("--disable-extensions");
- driver = new ChromeDriver(options);
- driver.manage().window().maximize();
- }
- @Test
- public void start() throws SQLException
- {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to a selected database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Connected database successfully...");
- String sql;
- smt= conn.createStatement();
- sql="select * from users";
- ResultSet set=smt.executeQuery(sql);
- while(set.next())
- {
- int id=set.getInt("ID");
- String username=set.getString("user_login");
- String password=set.getString("user_pass");
- System.out.println("ID->"+id);
- System.out.println("username->"+username);
- System.out.println("password->"+password);
- }
- set.close();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- driver.get("http://example.com");
- String uname="ghgfhgj";
- String upass="12Abcd";
- WebElement username = driver.findElement(By.name("log"));
- username.clear();
- username.sendKeys(uname);
- WebElement pass = driver.findElement(By.name("pwd"));
- pass.clear();
- pass.sendKeys(upass);
- WebElement submit = driver.findElement(By.name("Submit"));
- submit.click();
- String newuserquery = "SELECT * From users where users.user_login=ghgfhgj";
- PreparedStatement stat = conn.prepareStatement(newuserquery);
- stat.setString(1, uname);
- ResultSet set1=stat.executeQuery();
- while(set1.next())
- {
- String newusername = set1.getString("user_login");
- System.out.println("newusername->"+newusername);
- Assert.assertEquals(uname, newusername);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement