'Selenium webdriver verifying the text displayed in one row

I encountered a problem when trying to check if some text on a page is displayed in one row, I mean the html does not contain br tag, but because of resolution / font size - the text is wrapped to a second row. Is there a way to know it with selenium?



Solution 1:[1]

It's not a great solution, but might give you some ideas. We have text that is supposed to wrap to a second line when the browser width narrows to a certain point. I started full width driver.manage().window().maximize(); and got the height and width of the element containing the text. Then I set the browser window to a smaller size driver.manage().window().setSize(new Dimension(width, height)); and got the height and width of the same element again, and then could verify that the new height is roughly double the previous height, and the width is less, indicating it may be wrapped. I suppose the element height could grow but the text would not wrap (just be cut off), but this is the best I could think of.

Solution 2:[2]

Short answer: Yes

Long answer:

Suppose we have this element on webpage:

<span id="longtext">Very very long long text yada yada blah blah blah</span>

Then

driver.findElement(By.id("longtext")).getText()

will return you that text inside the span. And as far as I know it should not matter how is it wrapped in the page.

But if you have something like this:

<span id="longtext">Longer text which is <span id="warning">boringly</span> long</span>

Then getting text of this will be little bit harder I suppose.

But generally, displaying on the page should not matter and in most cases the getText() method from WebDriver class should return you the whole thing.

Note the driver variable in above example is expected healthy and living instance of WebDriver class. I am using Java in my own scripts, so the pseudocode is Java-based Webdriver example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JackhammersForWeeks
Solution 2 Pavel Janicek