Saturday, March 28, 2015

How to connect to Db and get values from Dabase using selenium

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

No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...