0

I am trying to verify a line of text is present on a loaded page using webdriver. I created a function - isTextPresent, then invoked the function within the same method.

Eclipse prompted me the error: the method IsTrue(boolean) is undefined for the type Assert.

  1. Please tell me why it doesn't work and how should I fix it.
  2. whatis the best approach to verify the text presentation on a web page?

    2a. is it possible to verify the text presentation within the first @Test code fragment?

    2b. which type## Heading ## of method shall I use in this case (public, private or protected)?

My code fragment:

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class selftechyTestng 
{
    private WebDriver driver;
    private String baseUrl;

    @Before
    public void setUp() throws Exception
    {
        driver = new FirefoxDriver();
        baseUrl = "http://selftechy.com/";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

       //First Test Method 
       @Test
    public void searchElements() throws Exception{
        driver.get(baseUrl);
        driver.findElement(By.xpath("//a[@title='Selenium']")).click();

    }



    @Test
    public boolean isTextPresent(String txtValue){
         try{
             boolean b = driver.getPageSource().contains(txtValue);
             return b;
         }
         catch (Exception e){
             return false;
         }
         Assert.IsTrue(isTextPresent("TestNG (Next Generation Testing Framework) – Understanding Annotations")); 


    }



}

Modification I have done to make the call to the function isElementPresent work Adding assertTrue() method within searchElements() methods

 assertTrue(isTextPresent(txtValue));

Method isElementPresent

public boolean isTextPresent(String str1)
    {
         try
         {
             driver.get(baseUrl);
             driver.findElement(By.xpath("//a[@title='Selenium']")).click();
             b = driver.getPageSource().contains(str1);

             if(b){
                 System.out.println("text presented on the page");
             }
             else{
                 System.out.println("text did not present on the page");
             }
             return b;
         }
         catch (Exception e)
         {
             System.out.println(e.getMessage());
             return b;
         }

         //return b;
     }
user2061466
  • 485
  • 9
  • 17
  • 27
  • @user1177636 Thanks for the reply. I did use assertTrue() method in my code with the function call. It worked as expected. – user2061466 May 23 '13 at 11:52

3 Answers3

2

or you can do this. First, remove the @Test annotation in the method and remove the Assert at the end:

public boolean isTextPresent(String txtValue){
         boolean b = false;
     try{
         b = driver.getPageSource().contains(txtValue);
         return b;
     }
     catch (Exception e){
         System.out.println(e.getMessage());
     }     
     finally{
      return b;
     }
}

Then your test will look like this

 @Test
public void searchElements() throws Exception{
    driver.get(baseUrl);
    driver.findElement(By.xpath("//a[@title='Selenium']")).click();
    Assert.IsTrue(isTextPresent("TestNG (Next Generation Testing Framework) – Understanding Annotations"));
}
Michal
  • 3,218
  • 2
  • 25
  • 44
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • Thanks for detailed reply. I modified my code using **assertTrue()** method to invoke the function **isElementPresent** and it worked. Thanks for your help! – user2061466 May 23 '13 at 11:54
1

I am just giving you the clue...You can assert text in like this.

assertEquals(txtValue, "TestNG (Next Generation Testing Framework) – Understanding Annotations");

Refer this link for more information on Assertions: http://testng.org/javadoc/org/testng/Assert.html

You can use assertEquals (String1, String2). Refer this link: Java: Is assertEquals(String, String) reliable?

-Vikram

Community
  • 1
  • 1
vkrams
  • 7,267
  • 17
  • 79
  • 129
  • Thanks for the reply. I used `boolean b = driver.getPageSource().contains("TestNG (Next Generation Testing Framework) – Understanding Annotations");` then with an if...else statement to print a message to show if expected text is present on the page. I am not quite sure how to use assertEquals correctly in my case. – user2061466 May 23 '13 at 03:08
-2

Example for C#

String OrgName = driver.FindElement(By.Id("")).GetAttribute("")
Assert.True(driver.FindElement(By.XPath("")).Text.Contains(OrgName));
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339