2017-04-23 35 views
0

所以我有這個基本的JUnit程序,在@beforeclass中打開瀏覽器並使其最大化,然後打開瀏覽器導航到一個站點並檢查元素。 但是隻有第一種情況總是被執行。所有後續的測試用例都失敗了。任何人告訴我,我在這裏做什麼錯誤在我的Junit測試用例中,第一個測試失敗後的所有測試

代碼:

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(); 
    } 



} 
+0

什麼錯誤失敗? –

+0

得到了問題,我需要在@BeforeClass類中有driver.get(TeamUrl)。 因爲它按字母順序執行測試用例,所以其他類沒有得到url –

+1

如果你想在整個測試中使用這種行爲,你需要將隱含的代碼行移動到BeforeClass方法 – Grasshopper

回答

1

我想這是因爲你只在你的第一個測試瀏覽到所需的網址,沒有其他測試瀏覽到所需的URL
嘗試添加導航步驟到setUpBeforeClass方法

嘗試如下:

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); 
     driver.get(TeamUrl); 

    } 

    @Test 
    public void OpenTheTeamURL() throws InterruptedException { 
     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(); 
    } 



} 
0

BeforeClass只運行一次。

嘗試使用@Before代替。該註釋使得該方法在每個測試用例之前運行。

+0

如果我使用「@Before」,它會打開瀏覽器和每次加載該網站,這是非常耗時的。除了打開瀏覽器並在「@BeforeClass」中加載網站之外,還有其他方法可以做到這一點嗎? –

+0

我還不明白你確切的問題。只是說有兩種不同的註釋具有不同的語義。 – GhostCat

0

如果我理解正確你想測試要執行什麼被前面的測試權離開,那麼你需要確保執行順序是正確的,但恕我直言,這不是試驗應如何創建

我想還是把它放在序列

def smoke_test_flock_team_domain(): 
""" test to check completeness of the page """ 
     open the page 
     assert title 
     assert footer 
     assert header 
     assert logo 
     click logo and assert result