-2

I am doing automation in MTM.

UI:

As Low As: $12,983.30 $108.19 120 5.43%

  1. I need to validate that number 12,983.30 starts with $
  2. I need to validate that number 5.43 ends with %

Code: for 1st test

So I was able to validate that element $12,983.30 exist and has $:

"xpath" "//*[+id='-student-loan-finder-form']/div/div[17]/div[2]/div[2]/div[2][text()[contains(., '$')]]" "Total Cost has $" "true" 

But I wasn't successful to validate that the number starts with $:

"xpath" "//*[+id='-student-loan-finder-form']/div/div[17]/div[2]/div[2]/div[2][text()[starts-with(., '$')]]" "Total Cost starts with $" "true"

What did I do wrong?

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Welcome to Stack Overflow! Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Jun 05 '18 at 21:23
  • The $ isn't the first character. You are pulling the `DIV` which starts with "Total Cost:..." so "T" is the first character and that's what is compared when considering `starts-with()`. – JeffC Jun 05 '18 at 21:25
  • Total Cost: $12,983.30 – Juliana Po Jun 06 '18 at 13:44
  • So how can I confirm that $ goes before the digits? – Juliana Po Jun 06 '18 at 13:45

1 Answers1

0

The $ isn't the first character. You are pulling the DIV which starts with "Total Cost:..." so "T" is the first character and that's what is compared when considering starts-with().

There are any number of ways to do this. Probably the simplest way is to split the returned text by ":" and then Trim() and look at the 2nd part.

Given the HTML you posted

<div class="col-md-2 col-sm-12 totalCostLow">
    <span class="mobile-label">Total Cost: </span>
    $12,983.30
</div>

getting the text from the outer DIV would return something like (whitespace may differ)

"Total Cost:  $12,983.30 "

You can split that string using ":"

"Total Cost", "  $12,983.30 "

grab the second string and Trim() it

"$12,983.30"

and then make sure the first character is "$"

That code would look something like

string priceString = <get the price string>; // e.g. "Total Cost:  $12,983.30 "
string price = priceString .Split(':')[1].Trim();

Now you can validate that price starts with a "$".

JeffC
  • 22,180
  • 5
  • 32
  • 55