繁体   English   中英

Selenium Java 字符串断言失败

[英]Selenium Java String assertion failing

我使用 Selenium IDE 来记录一个测试用例。 目标是断言页面上静态文本的正确性。 我将测试用例导出为 Java/JUnit 4/WebDriver,代码如下所示。

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());

我稍微编辑了代码的格式以匹配其余案例的编码方式:

    // 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.");
    }

测试用例在“stop1”之后失败,所以我不确定如何使这个字符串断言工作。 我尝试使用 String.join,但也失败了。 这是元素在检查时的样子。

如何处理 \\n 以正确断言此文本?

最好的方法就是打印

报告Body1.getText()

并将控制台中显示的文本复制到WordPad 在写字板中,我们可以很容易地准确看到换行符和空格的数量。 这样我们就可以很容易地弄清楚如何在断言中的期望值中使用空格、 /n等编写手段。

如果问题是前导和尾随空格,只需从reportingBody1.getText()修剪字符串即可。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM