Selenium Java String断言失败

时间:2016-05-20 17:02:11

标签: java selenium selenium-webdriver selenium-ide

我使用Selenium IDE来记录测试用例。目标是在页面上断言静态文本的正确性。我将测试用例导出为Java / JUnit 4 / WebDriver,代码看起来像这样。

assertEquals(“请为您尝试上传的数据选择评级年份和文件类型。\ n \ n评分年份:提交/收集数据的年份。”,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,但也失败了。 Here is what the element looks likes upon inspection.

如何处理\ n以正确断言此文本?任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:0)

这里最好的方法就是打印

  

reportingBody1.getText()

并将控制台中显示的文本复制到wordpad。在wordpad中,我们可以很容易地看到断线和空间的数量。这样我们就可以很容易地弄清楚如何在断言中用空格,/ n等写出预期值的方法。

谢谢你, 穆拉利

答案 1 :(得分:0)

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

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