sasmita
sasmita

Reputation: 1

getTitle() is not able to fetch the title of my page inside junit @test anotation

hello i m new to selenium webdriver.i want to verify the page title in my junit test.but using getTitle() i m able to find title in @before but not able to get title inside @Test.can any one solve this issue. here is my code: package junitTestCase;

    import java.util.concurrent.TimeUnit;

    import junit.framework.TestCase;

    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.ErrorCollector;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;


   public class TestCase1 extends TestCase
    {

 public static  WebDriver driver;

@Rule
    public ErrorCollector er=new ErrorCollector();

@BeforeClass
  public static void testDraiverConection()
  {

    driver=new FirefoxDriver();
}

@Before
   public void testLogintoApp()
{
    driver.get("http://127.0.0.1/login.do");
    driver.findElement(By.name("username")).sendKeys("admin");
    driver.findElement(By.name("pwd")).sendKeys("manager");
    driver.findElement(By.xpath("//input[@type='submit']")).click();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);


}
@Test
   public void testTc1()
{

    System.out.println("title is:" + driver.getTitle());
    String expectedResult="actiTIME - Open Tasks";
    String actualResult=driver.getTitle();
    try
    {
    Assert.assertEquals(expectedResult,actualResult);   
    System.out.println("PASS  "+expectedResult+"="+actualResult);
    }
    catch (Exception t)
    {
        er.addError(t);
        System.out.println(t);
    }

}
@Test
    public void testTc2()
{
    driver.findElement(By.xpath("//a[text()='Projects & Customers']")).click();
    try{
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    catch(NoSuchElementException e){
        er.addError(e);
    }
    String expectedResult="actiTIME - Active Projects & Customers";
    String actualResult=driver.getTitle();
    try
    {
        Assert.assertEquals(expectedResult,actualResult);
        System.out.println("PASS  "+expectedResult+"="+actualResult);
    }
    catch (Throwable t)
    {
        er.addError(t);
        //System.out.print(e.getMessage());
    }
}
@After
 public void testLogout()
{

    driver.findElement(By.xpath("//a/img[@alt='Logout']")).click();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

    }

Thanks in advance

Upvotes: 0

Views: 2061

Answers (1)

NamshubWriter
NamshubWriter

Reputation: 24286

Remove "extends TestCase"

The classes under the junit.framework package predate JUnit 4. Unless you are in a project where you are required to use JUnit3-style tests, you should stick to the JUnit classes under org.junit. Using both junit.framework and org.junit classes in the same test file can lead to bizarre behavior.

Upvotes: 1

Related Questions