-1

I'm trying to connect my db and then run a query to get the result through a testNG test.

Umair Khan
  • 5
  • 1
  • 6

1 Answers1

0

Please delete all the junit imports from the class, if you are planning to use TestNG for your execution. I would suggest you to use following approach:

package regressionTesting;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

import javax.naming.spi.DirStateFactory.Result;

import org.openqa.selenium.*;
import org.openqa.selenium.By.ByXPath;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestDBConnect {

public static String username = null;

    @BeforeClass
    public void setUp() {
        username = getUsernameFromDB();
    }

    @Test
    public void testUI() {

        //Some code for navigating to the required page
        driver.findElement(By.id("u")).sendKeys(username);
        //Code for executing remaining steps 

    }

    public static String getUsernameFromDB() throws ClassNotFoundException, SQLException  {
            //Accessing driver from the JAR file 
            Class.forName("oracle.jdbc.OracleDriver");
            System.out.println("Oracle JDBC driver loaded ok.");

            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@151.117.87.205:1602:epwfst1","epwf_app","epwf_app_epwfst1");
            System.out.println("DB Connected Successfuly");

            Statement stmt = con.createStatement();

            ResultSet result = stmt.executeQuery("select billing_application_accnt_id from epwf.payment where created_user_nm = '1600STOUTST10001'");

            String account = null;       
            while(result.next()){
                account = result.getString("BILLING_APPLICATION_ACCNT_ID");
                System.out.println("BAID: " + account);
            }
            con.close();
            return account;
         }
   @AfterClass
   public void cleanUp() {
          //Code for clean-up activities like closing browser and releasing resources.
   }
}

In the above code, I assumed that u is the id of text field in which you want to enter the value read from database. You can replace it with the actual id attribute value, or use other property as well for recognizing the username text box.

Mahipal
  • 900
  • 1
  • 5
  • 7
  • Using the above code and removing extra imports actually did solve the problem... but when i'm calling the method to get the username and insert into sendkeys, its not working. Below is the code – Umair Khan Jan 31 '17 at 00:24
  • `String username = getUsernameFromDB(); driver.findElement(By.id("baid")).sendKeys(username);` I can see that the db connect method is being called and the query is being executed as well, but the input field is not filling up with the result. ??!? – Umair Khan Jan 31 '17 at 00:28
  • no exception... the input field is just empty and my test just times out... `Oracle JDBC driver loaded ok. DB Connected Successfuly BAID: PPB11007406 [Utils] Attempting to create C:\Users\Umair\Default test.xml [Utils] Directory C:\Users\Umair\Default suite exists: true PASSED: testUI FAILED: testChangeOrder` its getting failed because it times out. – Umair Khan Jan 31 '17 at 00:34
  • Actually its working now.... the problem was that I was not getting the id for that input field right.. once I got that. It worked for!! Thanks you so much @Mahipal for your help!! – Umair Khan Jan 31 '17 at 00:42