Selenium: check if the text is full

Is there a way to check with Selenium if the text is fully visible? Say I have a text

lorum ipsum dolor sit amet

and due to bad css it only reads

lorem ips

on the page, the rest is under the incorrectly positioned div. Is there a way to claim that the full text is visible?

+4
source share
3 answers

Here is a simple example using jsFiddle I created and Java / Selenium.

HTML

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

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

Exit

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

Selenium , , , , , . , , , .

+1

, Java.

getText(), ( ).

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

- :

String actual = driver.findElement(By.cssSelector("")).getText().trim();
assertEquals(actual, expected);
-1

JAVA, , :

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;

}    

, , , .. , - lorum ipsum dolor sit amet

, ;)

-1

Source: https://habr.com/ru/post/1627476/


All Articles