How to test if the data entered through registration page of a web application is stored into database or not with Selenium?

I am new to Selenium. I am testing a user registration form in which i have to verify if the data is inserted into database or not. I am using following code to check. Is there any better option than this? I am first checking if registration ID is inserted and it it is then checking other details.

@Test
public void testVerifyListOfRecords() throws InterruptedException {

  String query = "SELECT ID FROM usermaster WHERE RegistrationNumber = '" + REGNO + "' ";
  String ID = DatabaseConnector.executeSQLQuery("QA", query);

  if(ID == ""){
    STATUS = "FALSE";
    System.out.println("REGISTRATION NUMBER IS NOT INSERTED..!! TEST FAILED..!!");
  } else STATUS = "TRUE";

  String sqlQuery = "SELECT FirstName,LastName,SpecialityCode,QualificationCode FROM usermaster WHERE ID = '" + ID + "' ";
  List<String> listOfDBValues = new ArrayList<String>();
  listOfDBValues = DatabaseConnector.executeSQLQuery_List("QA", sqlQuery);
  List<String> Branch = new ArrayList<>();
  Branch.add(listOfDBValues.get(0));

  List<String> list1 = new ArrayList<>(Arrays.asList("testfirstname testlastname ORP DM"));
  Assert.assertEquals(STATUS, "TRUE");
  System.out.println("STATUS IS " + STATUS);
  Assert.assertEquals(Branch, list1);
}

This is not really a Selenium question… it’s more of an automation and/or database question. Selenium only interacts with a browser. If you have no other way to verify if the user was registered and that is a requirement, then querying the database seems like the most straightforward way to verify it.

There are several things I would change to make this function better/more efficient.

  1. You seem to be using a lot of global variables, e.g. STATUS, REGNO. This is generally not a good practice. Pass into the function what it needs.

  2. Unless I’m missing something, you can be more efficient with your variable usage. You have a couple cases in this short function where you have a value already in a variable, store in it another variable, and then assert it’s value. There’s no need to put it into a new variable, just assert it’s value from the initial variable. This minimizes lines of code and gets rid of unneeded variables which makes the code easier to read and understand.

  3. Pick the best data type for each variable. For example, you are storing “TRUE”/”FALSE” in a String when you should be using boolean. Don’t create a List<String> and only add one item… just use a String variable.

  4. Assert early and often. You don’t assert the value of STATUS until the end of the function but you knew the value of STATUS in the first few lines of code and you hit the database afterwards. You should have asserted the value of STATUS right when you got it to fail the test as soon as possible and avoid extra hits to the db, additional code, etc.

  5. You have several sysouts that are redundant with your asserts. There’s no need to sysout the value of STATUS if it’s right after an assert that would fail if it’s anything but “TRUE”.

  6. Asserts have an optional message parameter. Use it. Add a comment on what the assert is testing, e.g. “Verifying ID is not empty” so that when it fails one day you will know from the logs what it was verifying. Make it descriptive and useful so that if all you had was that message, you would know what it was testing and possibly why it might have failed.

Here’s how I would clean this up

@Test
public void testVerifyListOfRecords(String regNo) throws InterruptedException
{
    String query = "SELECT ID FROM usermaster WHERE RegistrationNumber = '" + regNo + "' ";
    String ID = DatabaseConnector.executeSQLQuery("QA", query);
    Assert.assertFalse(ID == "", "Verify ID is not empty");

    String sqlQuery = "SELECT FirstName,LastName,SpecialityCode,QualificationCode FROM usermaster WHERE ID = '" + ID + "' ";
    List<String> listOfDBValues = DatabaseConnector.executeSQLQuery_List("QA", sqlQuery);
    List<String> list1 = new ArrayList<>(Arrays.asList("testfirstname testlastname ORP DM"));
    Assert.assertEquals(listOfDBValues.get(0), list1, "Verify user registration data is correct");
}