你好我刚接触到selenium webdriver.i想要在我的junit test中验证页面标题。但是使用getTitle()我能够在@before中找到标题但是无法在@ Test.can中获得标题任何一个解决这个问题问题。 这是我的代码: 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);
}
}
提前致谢
答案 0 :(得分:1)
删除“extends TestCase”
junit.framework包下的类早于JUnit 4.除非你所在的项目需要使用JUnit3样式的测试,否则你应该坚持使用org.junit下的JUnit类。在同一测试文件中同时使用junit.framework和org.junit类会导致奇怪的行为。