你好,我是 selenium webdriver 的新手。我想在我的 junit 测试中验证页面标题。但是使用 getTitle() 我能够在 @before 中找到标题,但无法在 @Test 中找到标题。任何人都可以解决这个问题。这是我的代码:包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);
}
}
提前致谢