Java getTitle()无法在junit@test Anotion内获取我的页面标题
您好,我是selenium webdriver的新手。我想在junit测试中验证页面标题。但是使用getTitle()我可以在@before中找到标题,但无法在@test中找到标题。有人能解决这个问题吗。 这是我的密码: 包junitTestCaseJava getTitle()无法在junit@test Anotion内获取我的页面标题,java,selenium,webdriver,junit4,Java,Selenium,Webdriver,Junit4,您好,我是selenium webdriver的新手。我想在junit测试中验证页面标题。但是使用getTitle()我可以在@before中找到标题,但无法在@test中找到标题。有人能解决这个问题吗。 这是我的密码: 包junitTestCase import java.util.concurrent.TimeUnit; import junit.framework.TestCase; import org.junit.After; import org.
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);
}
}
提前感谢删除“扩展测试用例”
junit.framework包下的类早于JUnit4。除非您所在的项目需要使用JUnit3样式的测试,否则您应该坚持使用org.JUnit下的JUnit类。在同一个测试文件中同时使用junit.framework和org.junit类可能会导致奇怪的行为。会出现什么错误?getTitle字符串是空的还是有错误?你在@Before中使用的获取标题的代码是什么?除了隐式等待,你是否尝试过使用其他等待?那么您是否确保下一个页面已完全加载?@nrbafna:-对于testTc1(),失败跟踪是:org.junit.ComparisonFailure:预期:但是是:您混合了JUnit3样式和JUnit4样式语法。删除“扩展测试用例”