Compare form values with db

63 views
Skip to first unread message

testingzeal

unread,
Aug 3, 2016, 1:32:39 PM8/3/16
to webdriver
Hello Friends,


I have some webdriver tests that submits a ticket form and have to compare the form values with db and the form size varies based on the first field which is department. 
(As i have several departments and other critria to submit a ticket i have different tests for each dept ticket submission, however i have to validate db)


I could only think of If conditions / Assert statements and both the approaches have lot of redundant code and i feel probably there is a better way to do this.

I started with softAsserts and have a posted a question in testng forum to get ideas of redundant code




I would like to know how others are comparing the form/db values. Can anyone share their knowledge?

Thanks

darrell grainger

unread,
Aug 4, 2016, 12:27:15 PM8/4/16
to webdriver
Not really a Selenium question but I can talk about how I would use Selenium to approach it. Normally if I had a form I might do the following in Selenium:

  • Get the first field using findElement
  • Compare it to the appropriate field in the DB
  • Get the second field using findElement
  • Compare it to the appropriate field in the DB
  • Repeat for each field in the form
This works fine if the size of the form is always the same. However, if the size of the form dynamically changes it gets a little harder. What I might see is if there is a why I can get all the elements in the form using a single locator. Something like a CSS selector, e.g. "form input" or if all the elements are of class='foobar' then I could use "form .foobar". Once I have a locator which works on all the possible forms (remember to find something which works regardless of which department you selected). Now I can use findElements to get a List<WebElement> of items.

Once I have a List<WebElement> I can iterate over the list of fields in the form. I'd use StringBuilder to build a comma delimited string. E.g. "department,field1,field2,field3," I don't even have to worry about an extra comma at the end of the string. Next I'd write a routine which will pull all the fields out of the database and build a comma delimited string. Finally, the assert statement would compare the string from the WebElements to the string from the database fields. If they don't match, fail.

testingzeal

unread,
Aug 5, 2016, 5:48:48 PM8/5/16
to webdriver
Thanks Darell.I didn't mean to come up with other questions in webdriver forum.

I am trying to understand the other approaches/ideas and get the effecient solution. Since other's would also do similar tests i am trying to get some help here! 


I liked your idea of storing it in string builder and comparing and so did the same and below is code if you could look.

Unfortunately when i say a form , it is not a simple one form and it is an actual work flow for submitting a ticket.


per say i fill in 3 fields and submit and then enter ticket details and submit and when ticket comes in to an agent work on the ticket by entering more details and submit.

i am storing the ticket values in the string builder while filling it and doing the same with db by storing values in string builder and comparing them.

In the helper class i created getFormValues() and fetched the values to sb and calling it before submitting the first form and the same with 2nd form created another getFormValues() in its helper class and fetching the values as webelements change from form to form.


Could you look and suggest any changes here?

If you feel this question is not relevant in the forum, please excuse me and ignore.


Ticket.java --helper class
============================================================================

public String create(String departmentName, String regionName,String pcr) throws Exception
{
newTicket();
department(departmentName);
region(regionName);
primaryContactReason(pcr);
String formValues = getFormValues();
submitTicket();
return formValues;
}

// append form values to string builder
private String getFormValues()
{
StringBuilder builder = new StringBuilder();
CommonUtils.waitFor();

   CommonUtils.AddField(builder, new Select(SelectDepartment).getFirstSelectedOption().getText());
   CommonUtils.AddField(builder, new Select(SelectRegion).getFirstSelectedOption().getText());
   CommonUtils.AddField(builder, new Select(SelectPrimaryContactReason).getFirstSelectedOption().getText());

   return builder.toString();

 }


//Add the field values to the string builder
public static void AddField(StringBuilder builder, String value){
if (value != null) {
        builder.append(value).append(",");
}
=========================================actual test=====

StringBuilder formValues = new StringBuilder();
StringBuilder dbValues = new StringBuilder();
driver.get(baseUrl);
HomePage homePage = loginPage.loginAs(LogInConstants.USERNAME,LogInConstants.PASSWORD);
softAssert.assertTrue(homePage.isUserLoggedIn(),"User failed to login");
formValues.append(homePage.ticket().create(DepartmentConstants.CREDIT_OPERATIONS, RegionConstants.NA, PCRConstants.COLLECTIONS));//appending values to string builder while filling the form
formValues.append(homePage.ticket().details().escalationDetails(TicketConstants.CASEID,TicketConstants.ACCNO,EscalationReasonConstants.DISPUTES, ResolutionConstants.MERCHANT_CREDIT, EscalationSourceConstants.EMAIL, CountryConstants.CANADA, LanguageConstants.ENGLISH, TicketConstants.DETAILS));////appending values to string builder while filling the form
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Thanks for the help!

testingzeal

unread,
Aug 5, 2016, 5:49:37 PM8/5/16
to webdriver
Reply all
Reply to author
Forward
0 new messages