【发布时间】:2017-04-23 09:46:58
【问题描述】:
所以我有这个基本的 JUnit 程序,在 @beforeclass 我打开浏览器并最大化它,然后打开浏览器导航到一个站点并检查元素。 但是,始终只执行第一种情况。所有后续的测试用例都失败了。任何人都告诉我我在这里犯了什么错误
代码:
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FlockTeamURLDomain {
static WebDriver driver;
String TeamUrl = "http://farzanshaikh.flock.co/";
String TeamName = "<script>farzan</script>";
String TeamDomain = "farzanshaikh.com";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
@Test
public void OpenTheTeamURL() throws InterruptedException {
driver.get(TeamUrl);
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
String Title = driver.getTitle();
System.out.println("The title of the Page is: "+Title);
if(Title.equals("Flock - Team")){
System.out.println("The Title is Correct");
}
else{
System.err.println("The Title is InCorrect");
}
Thread.sleep(2000);
}
@Test
public void CheckTheFooter() {
boolean FlockFooter = driver.findElement(By.cssSelector("//div[@id='team-page']/div[2]/div[5]")).isDisplayed();
System.out.println("Is the Footer Present? "+FlockFooter);
}
@Test
public void CheckAndClickLogo() {
boolean FlockLogo = driver.findElement(By.xpath("//div[@id='team-page']//img")).isDisplayed();
System.out.println("Is Flock Logo Displayed "+FlockLogo);
}
@Test
public void CheckTheHeader() {
boolean FlockHeaderLogo = driver.findElement(By.xpath("//div[@id='step-2-block']/span")).isDisplayed();
System.out.println("Is the Header Element Present? "+FlockHeaderLogo);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
【问题讨论】:
-
失败并出现什么错误?
-
得到了问题,我需要在 @BeforeClass 类中有 driver.get(TeamUrl)。因为它按字母顺序执行测试用例,所以另一个类没有得到 url
-
如果你想要跨测试的这种行为,你需要将你的隐式代码行移动到 BeforeClass 方法
标签: java selenium selenium-webdriver junit