7

I have a page that I know contains a certain text at a certain xpath. In firefox I use the following code to assert that the text is present:

assertEquals("specific text", driver.findElement(By.xpath("xpath)).getText());

I'm asserting step 2 in a form and confirming that a certain attachment has been added to the form. However, when I use the same code in Chrome the displayed output is different but does contain the specific text. I get the following error:

org.junit.ComparisonFailure: expected:<[]specific text> but was:<[C:\fakepath\]specific text>

Instead of asserting something is true (exactly what I'm looking for) I'd like to write something like:

assert**Contains**("specific text", driver.findElement(By.xpath("xpath)).getText());

The code above does not work obviously but I can't find how to get this done.

Using Eclipse, Selenium WebDriver and Java

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Hugo
  • 313
  • 2
  • 6
  • 19

4 Answers4

18

Use:

String actualString = driver.findElement(By.xpath("xpath")).getText();
assertTrue(actualString.contains("specific text"));

You can also use the following approach, using assertEquals:

String s = "PREFIXspecific text";
assertEquals("specific text", s.substring(s.length()-"specific text".length()));

to ignore the unwanted prefix from the string.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 2
    Thank you! I used the following and that worked fine: assertTrue(driver.findElement(By.xpath("xpath")).getText().contains("specific text")); – Hugo Oct 01 '15 at 12:41
  • 1
    You can optionally add a second parameter, with a custom error message for if it fails. For example: `assertTrue(s.contains("specific text"), "String did not contain the required text.");` – Andrio Oct 01 '15 at 12:49
2

You can also use this code:

String actualString = driver.findElement(By.xpath("xpath")).getText();
Assert.assertTrue(actualString.contains("specific text"));
Gryu
  • 2,102
  • 2
  • 16
  • 29
Vaibhav
  • 2,516
  • 1
  • 13
  • 8
2

Two Methods assertEquals and assertTrue could be used. Here is the usage

String actualString = driver.findElement(By.xpath("xpath")).getText();

String expectedString = "ExpectedString";

assertTrue(actualString.contains(expectedString));
Meenakshi Rana
  • 505
  • 5
  • 8
0

It's not directly and assert however and other way is to use a wait.until and ExpectedCondition, then if condition is not satisfied the test will fail:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    @FindBy(xpath = "xpath")
    private WebElement xpathElementToCheckText;
      
    public void checkElementText() {
       WebDriverWait wait = new WebDriverWait(driver, 10); // timeout in seconds
wait.until(ExpectedConditions.textToBePresentInElement(xpathElementToCheckText,"specific text"));
    }
albciff
  • 18,112
  • 4
  • 64
  • 89