-2

I have an element that contains text with breaks. Using Java, how can I verify that the element's text is what I'm expecting?

I am using the below code, but it always fails. The report that gets printed shows the actual and expected text values to be the same, so I don't know why it fails.

<span id="ctl00_cphMainContent_lblWelcomeText">
Dear Tester, 
<br>
<br>
Line 1
<br>
<br>
Line 2
<br>
<br>
Line 3
<br>
<br>
Line 4

and my java:

public static final String PAGETEXT = "css=ctl00_cphMainContent_lblWelcomeText";

protected void verifyText(PAGETEXT, "Dear Tester,\r\n\r\nLine 1\r\n\r\nLine 2\r\n\r\nLine 3\r\n\r\nLine 4"){
        //verify that the expected text is present
        String actualtext = driver.getSingleElement(PAGETEXT).getText();
        Assert.assertEquals(actualtext, expectedtext);
ddavison
  • 28,221
  • 15
  • 85
  • 110
TestRaptor
  • 1,305
  • 8
  • 24
  • 42
  • question (code) is incomplete use coding indentation – Mitesh Pathak Dec 18 '13 at 21:25
  • 1
    At least put some effort in formatting your code and thoroughly describe your problem (code, output, expected output, etc) if you expect an answer... – Josh M Dec 18 '13 at 21:26
  • Have you printed the text you actually get to the console and compared to what you compare it with? `
    ` and `\n` or `\r\n` might not convert as you expected.
    – zapl Dec 18 '13 at 21:31
  • When you want others' help and people are trying to help you at least you can pay some attention to them. Even if you solved your problem yourself it would be decent to notify here. – AbhinavRanjan Dec 20 '13 at 16:21

3 Answers3

2

You can use something like this.

String expectedtext="Dear Tester, \n\nLine 1\n\nLine 2\n\nLine 3\n\nLine 4";

public void verifyText(){
  String actualtext = driver.getSingleElement(PAGETEXT).getText();
  Assert.assertEquals(actualtext, expectedtext);
}

basically keep your spaces as it is in the actual text. replace
with a \n

debugger89
  • 2,108
  • 2
  • 14
  • 16
1

I'd honestly break this into seperate assertions.

verifyTextContains(PAGETEXT, "Dear Tester,");
verifyTextContains(PAGETEXT, "Line 1");
verifyTextContains(PAGETEXT, "Line 2");
verifyTextContains(PAGETEXT, "Line 3");
verifyTextContains(PAGETEXT, "Line 4");

This is of course, assuming that your Acceptance Criteria does not include "make sure that breaks appear between these lines"

ddavison
  • 28,221
  • 15
  • 85
  • 110
0
  1. Split to lines (Split Java String by New Line)
  2. Assert lines count is equal
  3. Assert every line equality
Community
  • 1
  • 1
Ruslan Ostafiichuk
  • 4,422
  • 6
  • 30
  • 35