0

I used the Selenium IDE to record a test case. The goal was to assert the correctness of the static text on a page. I exported the test case as Java / JUnit 4 / WebDriver and the code looked like this.

assertEquals("Please select the ratings year and file type for the data that you are attempting to upload. \n\n Ratings Year: The year in which the data was submitted/collected.", driver.findElement(By.cssSelector("p")).getText());

I edited the format of the code a bit to match how the rest of the case is coded:

    // Expected text for each part of the reporting year page
    String rMainHeading = "Upload Data Files";
    String rHeading = "Select Ratings Year and File Type for Clinical Measures Data";
    String rBody1 = "Please select the ratings year and file type for the data that you are attempting to upload. \n\n Ratings Year: The year in which the data was submitted/collected.";

    // Reporting year page text locations
    WebElement reportingMainH = driver.findElement(By.cssSelector("h1"));
    WebElement reportingHeader = driver.findElement(By.cssSelector("h2"));
    WebElement reportingBody1 = driver.findElement(By.cssSelector("p"));

    System.out.println("stopA");
    // Checking the Reporting Year page text is displayed as expected
    try {
        assertEquals ((reportingMainH.getText()),rMainHeading);
        assertEquals ((reportingHeader.getText()),rHeading);
        System.out.println("stop1");
        assertEquals ((reportingBody1.getText()),rBody1);
        System.out.println("stop2");

        WebElement reportingWords = driver.findElement(By.id("submissionForm"));
        String reportingYearText = reportingWords.getText();
        System.out.println("4) The static text on the Reporting Year page has been confirmed.");
        //System.out.println(reportingYearText);
        setupScript.screenshot(driver);

    }
    catch (Exception e) {
        System.out.println("*******The text on the page does not match the expect results of XXX-1938. This case has failed.");
    }

The test case is failing after "stop1" so the I'm not sure how to make this string assertion work. I tried using a String.join, but that also failed. Here is what the element looks likes upon inspection.

How can I deal with the \n to assert this text correctly?

3
  • print out reportingBody1.getText() and copy and paste it to the variable rbody1 Commented May 20, 2016 at 17:18
  • @LINGS it prints out with a blank line in between the two statements, so I'm still not sure how to code that since using \n doesn't seem to be working (or I'm using it incorrectly). Commented May 20, 2016 at 18:33
  • I figured this out. Removing the spaces before and after \n\n fixed it! So it looked like this: String rBody1 = "Please select the ratings year and file type for the data that you are attempting to upload.\n\nRatings Year: The year in which the data was submitted/collected."; Commented Nov 8, 2016 at 18:10

2 Answers 2

0

If the problem is leading and trailing spaces, simply trimming the String from reportingBody1.getText() would work.

assertEquals(reportingBody1.getText().trim(), rBody1);

Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately they reverted the code back to a previous version and I won't be able to test this for another week or so. I will give this a try when they redeploy and let you know. Thanks for the help!
No problem! :) Let me know if it works when you test this.
That's great! Thanks for testing it and letting me know!
0

The best way to do here is just print

reportingBody1.getText()

and copy the text which is displaying in console to WordPad. In WordPad we can easily see number of line breaks and space exactly. So that we can easily figure it out how to write means with spaces, /n, etc. in expected value in assertions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.