4

Is there a way to check with Selenium if a text is fully visible? Let's say I have the text

lorum ipsum dolor sit amet

and due to a bad css it reads only

lorem ips

on the page, the rest is under a wrongly placed div. Is there a way to assert that the full text is visible?

d135-1r43
  • 2,485
  • 1
  • 25
  • 33

3 Answers3

1

Here's a simple example using a jsFiddle that I created and Java/Selenium.

The HTML

<p id="1">lorum ipsum dolor sit amet</p>
<p id="2">lorum ipsum <div style="display:none">dolor sit amet</div></p>

The code

String expectedString = "lorum ipsum dolor sit amet";
WebDriver driver = new FirefoxDriver();
driver.get("https://jsfiddle.net/JeffC/t7scm8tg/1/");
driver.switchTo().frame("result");
String actual1 = driver.findElement(By.id("1")).getText().trim();
String actual2 = driver.findElement(By.id("2")).getText().trim();
System.out.println("actual1: " + actual1);
System.out.println("actual2: " + actual2);
System.out.println("PASS: " + expectedString.equals(actual1));
System.out.println("PASS: " + expectedString.equals(actual2));

The output

actual1: lorum ipsum dolor sit amet
actual2: lorum ipsum
PASS: true
PASS: false

Selenium won't return text that isn't visible to the user so all you need to do is compare what you get back with the expected string. If they aren't equal, then text is likely hidden.

JeffC
  • 22,180
  • 5
  • 32
  • 55
-1

Following is one way of doing this if you use Java.

First, locate element with correct locator and do getText() and then compare the string with expected (full text).

driver.findElement(By.cssSelector("")).getText().equals("");

May be something like this:

String actual = driver.findElement(By.cssSelector("")).getText().trim();
assertEquals(actual, expected);
AGill
  • 768
  • 8
  • 17
  • 1
    if you read the OP's question, the text is there. the problem is - they are trying to validate the visuals of the element. all of the appropriate text is there, just not showing correctly – ddavison Feb 05 '16 at 18:10
  • Well, the OP is saying "the rest is under a wrongly placed div". As I said, using the CORRECT LOCATOR will get the text that is under the correct `
    ` tag, not what is under the wrongly placed `
    ` tag. @sircapsalot Would you mind reading his question again? Thank you.
    – AGill Feb 05 '16 at 18:53
  • 1
    OP understands that certain CSS rules could render a full string only partially visible (to a human), something that the basic Selenium rules will not cover, so as @sircapsalot suggests, you're still missing the point. – Andrew Regan Feb 05 '16 at 20:49
  • 1
    @AndrewRegan webdriver is not going to give back the text which is not visible to user. If the real user see only partial text, webdriver will return that partial text only. – AGill Feb 05 '16 at 21:31
  • @AndrewRegan and sircapsalot you both are missing the point. Selenium will not see text (in the general case) that the user does not see. Comparing what Selenium returns to the expected text is the way to do this. If you don't believe it, run the code from my answer and see for yourself. – JeffC Feb 05 '16 at 21:35
  • @sircapsalot see my comment above. (I can't respond to more than one person in a single comment). – JeffC Feb 05 '16 at 21:35
  • When OP said "full visibility" I took that to mean a bit more than "not explicitly hidden", also they referred to "bad CSS", so with that vagueness in mind, only OP can be the judge. It's important to know what `getText()` can/can't do, but that's only clear to OP as a result of this discussion not from original answer. – Andrew Regan Feb 05 '16 at 22:47
-1

Here is the JAVA Code to check if the Text inside the element is visible or not:

public boolean checkForText() {

    boolean isVisible = false;

    try {
        // Start by searching the element first.  You can search by many ways.  eg. css, id, className etc..
        WebElement element = webDriver.findElement(By.id("the elemnts' id"));
        System.out.println("Element found");
        // Check if the found element has the text you want.
        if(element.getText().equals("lorum ipsum dolor sit amet")) {
            System.out.println("Text inside element looks good");
            isVisible = true;
            //additionally, you can perform an action on the element
            //e.g. element.click();
        } else {
            System.out.println("Text does not match");
        }
    } catch (NoSuchElementException e) {
        // the method findElement throws an exception.
        System.out.println("Element not found");
    }

    return isVisible;

}    

This method will return true ONLY if the element is found, and its inside text corresponds the equals criteria i.e. the text passed as parameter - lorum ipsum dolor sit amet

Hope this helps ;)

  • This doesn't even remotely cover the essence of the OP's requirements for "visibility". – Andrew Regan Feb 05 '16 at 20:43
  • 1
    @AndrewRegan can you give an example? – Mario Galea Feb 05 '16 at 21:19
  • I would pass a By (the element locator) and the desired String into your function. – JeffC Feb 05 '16 at 21:28
  • @JeffC I am passing the element located and the string already: ` if(element.getText().equals("lorum ipsum dolor sit amet")) { ` – Mario Galea Feb 05 '16 at 21:42
  • You aren't passing them... you have them hardcoded. – JeffC Feb 05 '16 at 21:43
  • This is just a trivial example to show basic usage. I haven't included assertion to avoid confusion. This is just a demo. Its up to the user to extend my example. – Mario Galea Feb 05 '16 at 21:43
  • @JeffC Its up to the user to modify and extend my example. Certainly not the ideal way how to call a method when writing an automation framework, but this is just an understandable example. Yes I could have done that too by initializing a string with "lorum ipsum dolor sit ametit" and pass it as a parameter – Mario Galea Feb 05 '16 at 21:46
  • I would do this... `public boolean checkForText(By locator, String expectedString) { return driver.findElement(locator).getText().equals(expectedString); }`. Much simpler and more reusable. It also more closely sticks to the function name... checkForText(). It should only do that one thing.. not click elements, etc. – JeffC Feb 05 '16 at 21:47
  • @JeffC The click is commented out. Im just highlighting to the user some extra functionality of the WebElement class. You clearly missing the point, and getting out of context. – Mario Galea Feb 05 '16 at 21:54
  • When OP said "full visibility" I took that to mean a bit more than "not explicitly hidden", also they referred to "bad CSS", so with that vagueness in mind, only OP can be the judge. We shall see. It's important to know what `getText()` can/can't do, but that's only clear to OP as a result of this discussion. Your answer didn't do the question justice IMO. – Andrew Regan Feb 05 '16 at 22:50