Forum Discussion

gdave's avatar
gdave
Regular Contributor
3 years ago

Checkpoint

Hi all

 

Kindly refer to the below screenshot. I simply want to prove that 'value entered (£2500)' Less 'total estimated adviser payment (£75)' = Total net deposit amount (£2425)

Not sure if this can be achieved via code snippet ? If yes, can someone please advise the code for the same as my coding skills aren't that great.

 

Your help is much appreciated.

 

Thanks

 

 

  • Sorry for running you around in circles.  Just do this in your keyword test and you won't need a code snippet

     

    the if condition is checking for C = A - B

  • Kitt's avatar
    Kitt
    Regular Contributor

    This depends on how you are referencing or mapping these objects, but a high-level example (assuming you are indeed scripting your tests and not using keyword tests) would be to just create a few variables for all of the object's innerHTML, innerTextcontentText or textContent property values. And example would be something like:

     

    // xPath is referencing the 1st row (/tr), with a table header of 'Value Entered' (/th) and cell value that contains '£' (/td)
    var deposit = Sys.Browser("*").Page("*").FindElement("//tbody/tr[1]/th[contains(text(), 'Value Entered')]/td[contains(text(), '£')]");
    Log.Message("Deposit Amount = " + deposit.contentText);
    // will print "Deposit Amount = £2,500" to the log

     

    Again, this depends on how these objects are referenced in your project, but you can object spy the areas of the screenshot you provided to find how TestComplete is finding these objects and show you what properties, values, and methods you can use within your scripts.

     

    An example would be something like the screenshot below, which is using name mapping, but you can see TC is attempting to find the object based on a generic xPath reference to a table cell, or <td> with 'Active' as a value, IE: //td[.='Active']. This wouldn't work bc there are several rows in the table which have cells containing a value of 'Active', which is why you may need to tweak your xPath or CSS reference similarly to how I did above:

    OR add extra property selectors in your NameMapping file to help TC find a unique table cell (if you use name mapping).

     

    Once you know how to find these objects and extract their values as variables in your test, the math/validation is pretty straight-forward. Something like:

     

    // set your object's as variables
    var deposit = Sys.Browser("*").Page("*").FindElement("//tbody/tr[1]/th[contains(text(), 'Value Entered')]/td[contains(text(), '£')]");
    var payment = Sys.Browser("*").Page("*").FindElement("//tbody/tr[contains(text(), 'Total Estimated Cost of Transactions')]/td[contains(text(), '£')]");
    var netAmount = Sys.Browser("*").Page("*").FindElement("//tbody/tr[contains(text(), 'Total Net Deposit Amount')]/td[contains(text(), '£')]");
    
    // log for reference
    Log.Message("Deposit Amount = " + deposit.contentText); 
    Log.Message("Payment Amount = " + payment.contentText);
    Log.Message("Net Amount = " + netAmount.contentText);
    
    // remove symbol and convert to Int may be necessary
    var depositInt = aqConvert.StrToInt(aqString.Replace(deposit.contentText, "£", ""));
    var paymentInt = aqConvert.StrToInt(aqString.Replace(payment.contentText, "£", ""));
    var netAmountInt = aqConvert.StrToInt(aqString.Replace(netAmount.contentText, "£", ""));
    
    // verify math is correct - if deposit minus payment does not equal netAmount, throw an error
    if (!equal((depositInt - paymentInt), netAmount)) {
      Log.Error("ERROR: deposit " + depositInt + " - payment " + paymentInt + " != netAmount " + netAmountInt);
    }
    else {
      Log.Checkpoint("PASS: math is fun");
    }

     

     

    • gdave's avatar
      gdave
      Regular Contributor

      Thanks so much for the detailed explanation.

       

      I should have mentioned that we use keyword test and not scripting. Is here a way this can be achieved thru keyword testing ?

       

      Thanks

  • if ((myvalueentered - myadviserpayment) = mytotalnetdeposit) then log.message("test passed") else log.message("test failed")

     

    You can do the calculations in the snippet or you can have the values in variables and calculate on the variables. I would do the variables if I need those values again somewhere else.

     

     

    • gdave's avatar
      gdave
      Regular Contributor

      Hi Marsha

       

      Sorry but I am not sure how this is going to work. I want TC to calculate that the on screen value of 'total net deposit' is the direct result of 'value entered' less 'adviser payment' throuh keyword test. I am little confuse how TC will calculate this using If Then statement ?

      • Marsha_R's avatar
        Marsha_R
        Moderator
        You can put the If in a code snippet.

        Try this
        Get each of the three values from the objects and store them in variables, say A , B, C

        Then your code snippet will be something like
        If ((B-C) = A) then log.message("Pass") else log.message("fail)

        That will all work in a snippet. Try it and let me know if that's what you want.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    It can key done in Keyword Testing, but it's a pain. You'll have more control in scripting the scenario you want. 

    Though my example shows The property value "4782.4" does not equal "4782.4"!

    • gdave's avatar
      gdave
      Regular Contributor

      Cheers, but that didn't work either.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Assume you're scripting in JavaScript, then the code should be,

     

    if ((KeywordTests.Sample_Browser.Variables.Value_Entered - KeywordTests.Sample_Browser.Variables.Adviser_Payment) == KeywordTests.Sample_Browser.Variables.Net_Deposit) {
        Log.Message("Pass");
    } else {
        Log.Message("Fail");
    }

     

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I don't think Code Snippet likes the if-statement as it keeps throwing an exception!

    Marsha_R example is a lot easier to use.