Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of isTextPresent of Selenium 1 (Selenium RC) in Selenium 2 (WebDriver)

There's no isTextPresent in Selenium 2 (WebDriver)

What is the correct way to assert the existence of some text on a page with WebDriver?

like image 969
Thomas Vervik Avatar asked Oct 09 '11 21:10

Thomas Vervik


People also ask

What is the difference between Selenium RC and WebDriver?

Object OrientedSelenium WebDriver is purely object oriented API, whereas Selenium RC is less object oriented API. WebDriver is entirely based on object oriented programming languages like Java, C#, etc.

What do we mean by Selenium 1 Selenium 2 and selenium 3?

Selenium 2. Selenium 1 = Selenium Remote Control. Selenium 2 = Selenium 1 + WebDriver. Depends on the core libraries which could run on any browser which would support JavaScript. It runs JavaScript natively and test cases created can be run on different browsers.

Which feature is additional in Selenium 2?

The primary new feature of Selenium 2 is the integration of WebDriver, a rival web application testing framework to Selenium 1 (a.k.a. Selenium RC). While Selenium RC runs a JavaScript application within the browser, WebDriver controls the browser directly using native browser support or browser extensions.

Why Selenium RC is deprecated?

Selenium RC comprises an additional layer of JavaScript known as the core which makes it slower. Selenium RC has complicated and redundant APIs. Selenium RC is not compatible with the HTMLUnit browser (required for headless execution). Selenium RC has in-built HTML report generation features for test results.


2 Answers

I normally do something along the lines of:

assertEquals(driver.getPageSource().contains("sometext"), true);

assertTrue(driver.getPageSource().contains("sometext"));
like image 103
Thomp Avatar answered Nov 02 '22 04:11

Thomp


Page source contains HTML tags which might break your search text and result in false negatives. I found this solution works much like Selenium RC's isTextPresent API.

WebDriver driver = new FirefoxDriver(); //or some other driver
driver.findElement(By.tagName("body")).getText().contains("Some text to search")

doing getText and then contains does have a performance trade-off. You might want to narrow down search tree by using a more specific WebElement.

like image 32
Ashwin Prabhu Avatar answered Nov 02 '22 06:11

Ashwin Prabhu