0

I am doing automation testing at my work and i'm stuck in a scenario.

My first test consist of creating a new user account which creates a username and a password. P.S. My password is always the same

My second test is basically login as a user with the username and password that I created in the first test.

Also, as the account is created, the database gets updated, and i'm able to connect to the database and execute the query (select user_name from accounts where password = 'pass123' order by created_dttm desc;)

So, since i'm able to execute and get the value from the db, how can I fetch that value and insert it into my username field in order to login as a customer.

Because i'm stuck in this situation, I always have to edit my 2nd test case with a different username that is created in my first tc. I would like my test script to take the username from the db and insert it into the username field automatically.

I'm kinda new to Java and automation, therefore, any kind of help will be highly appreciated. Thanks a ton!

Umair Khan
  • 5
  • 1
  • 6

1 Answers1

1

You need to just pass the username value read from database to the sendKeys function of Webdriver. For example, if you have stored the username value from database in String variable user and if you are using id property to recognize the username field, then you can enter that value in username field using following:

driver.findElement(By.id("u")).sendKeys(user);

In the above code, u is the id of username field. You can move your database access code to separate method as shown below:

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.120.92.150:1602:pppst1","ppp_app","ppp_app_pppst1");
    System.out.println("DB Connected Successfuly");

    Statement stmt = con.createStatement();

    ResultSet result = stmt.executeQuery("select user_name from accounts where password = 'pass123' order by created_dttm desc;");

    String account = null;
    while(result.next()){
        account = result.getString("USER_NAME");

        System.out.println("BAID: " + account);

        }
    con.close();
    return account;
}

Your test case code will become:

String username = getUsernameFromDB();
driver.findElement(By.id("u")).sendKeys(username);
Mahipal
  • 900
  • 1
  • 5
  • 7
  • Thanks for the help. Do I need to do this all in one class or I can have one class for db connection and the other for my tc? – Umair Khan Jan 26 '17 at 23:16
  • Ideally, we should be having a separate generic class which will assist us in performing generic database operations. This generic class may contain functions for executing queries, insert, update and delete statement executions. In this particular case, the generic method, let say, executeQuery will be called from your test case class to get the **username** value from database. Let me know, if you have any further queries. – Mahipal Jan 26 '17 at 23:24
  • Thanks Mahipal for the reply. I have also added the db connection code in the original post. Therefore should my code be driver.findElement(By.id("u")).sendKeys(account); ?? – Umair Khan Jan 27 '17 at 00:29
  • Yes. But, you need to replace **u** with the actual **id** of the username field as well. – Mahipal Jan 27 '17 at 00:34
  • Yes I did that, but for some reason, its not able to recognize my String variable 'account' that I created in the code mentioned in the original post. – Umair Khan Jan 27 '17 at 00:52
  • Because, **account** variable is not visible in your test case class. You can move the database access into separate method and call that method to get the username. Refer to my modified answer. – Mahipal Jan 27 '17 at 01:49
  • Thanks Mahipal.. I got the variable to be visible but then when run the database access code as TestNG test since that's the only option i have, I don't the see the db to connect and execute the query. This is what I see =============================================== Default suite Total tests run: 0, Failures: 0, Skips: 0 =============================================== – Umair Khan Jan 27 '17 at 04:19
  • **main** method will not be invoked, if you run your code as a TestNG test. As per the output you shared, no tests have been executed. Did you annotate your test method with @Test annotation? – Mahipal Jan 27 '17 at 05:00
  • yes I did use test annotations (before, test, after). Please see the revised code with annotations in my original post, and let me know if **account** variable is visible or not, and how can I call that method to get the username. Thanks!! – Umair Khan Jan 27 '17 at 17:09
  • Firstly, we need to find out why test is not running. Since, it is a separate issue, could please raise a new question. This will help better manage the questions. You can just say in your question that TestNG test is not getting invoked. Please share the full class code including import statements in your question. – Mahipal Jan 29 '17 at 23:39
  • Just posted the question http://stackoverflow.com/questions/41947093/testng-test-not-getting-invoked – Umair Khan Jan 30 '17 at 23:13