user2495926
user2495926

Reputation: 63

Selenium - Get and compare specified length of text from web element

The scenario goes like this...

There's dynamic text within a web element that sometimes contains "..." at the end, due to it's length. e.g The quick brown fox...

I would like to be able to extract all text before the "..." then compare this text to the benchmark text. The benchmark text contains the complete sentence i.e The quick brown fox jumps over the lazy dog. I want to be able to take the text extracted and compare the text in character length with that of the benchmark text just so we are comparing apples with apples. So it would be like this Get The quick brown fox compare to The quick brown fox

Thanks in advance

Upvotes: 1

Views: 2456

Answers (1)

Qwerky
Qwerky

Reputation: 18445

The basic premise is that if the string under test ends with dots, you check that the expected string starts with the string under test (but without the dots).

Code below isn't tested and you might want to think about null string scenarios.

static final String DOTS = "...";
String actual = "The quick brown fox...";
String expected = "The quick brown fox jumps over the lazy dog";

public void testStringWithDots(String actual, String expected) {
  if (actual.endsWith(DOTS)) {
    String prefix = actual.substring(0, actual.length() - DOTS.length());
    Assert.assertTrue("Strings are same up to dots", expected.startsWith(prefix);
  } else {
    Assert.assertEquals("Strings are same", expected, actual);
  }
}

Upvotes: 1

Related Questions