2014-02-18 55 views
0

我已經在堆棧中篩選了一段時間的答案,並且遇到了一些似乎完成此工作的示例或至少接近。如何在Eclipse中的相​​同Webdriver中設置Selenium 2.x測試套件(JUnit4)

所有我希望能夠做的是出口硒測試用例JUnit4文件,將他們帶到蝕,並根據需要進行修改,然後是能夠從測試套件中添加或刪除它。當測試套件運行時,它應該打開一個webdriver窗口並運行每個測試用例,就像它在基本firefox窗口中的selenium IDE中運行的一樣。唯一的問題似乎是webdriver類。我曾嘗試測試用例類,這似乎保持窗口,而不會引發錯誤之間的引用這一點,但第二個測試用例從未運行,喜歡它的死循環,或停在@Before

import static org.junit.Assert.fail; 

import java.util.concurrent.TimeUnit; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 
import org.junit.runners.Suite.SuiteClasses; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

@RunWith(Suite.class) 
@SuiteClasses({TestCase1.class,TestCase2.class}) 
public class RunTestSuite { 

} 

^^^測試套件例如^^^

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class TestCase1 { 
    private WebDriver driver; 
    private String baseUrl; 
    private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 
    driver = new FirefoxDriver(); 
    baseUrl = "http://www.google.com/"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testOpen() throws Exception { 
    driver.get(baseUrl + ""); 
    } 

    private boolean isElementPresent(By by) { 
    try { 
     driver.findElement(by); 
     return true; 
    } catch (NoSuchElementException e) { 
     return false; 
    } 
    } 

    private boolean isAlertPresent() { 
    try { 
     driver.switchTo().alert(); 
     return true; 
    } catch (NoAlertPresentException e) { 
     return false; 
    } 
    } 

    private String closeAlertAndGetItsText() { 
    try { 
     Alert alert = driver.switchTo().alert(); 
     String alertText = alert.getText(); 
     if (acceptNextAlert) { 
     alert.accept(); 
     } else { 
     alert.dismiss(); 
     } 
     return alertText; 
    } finally { 
     acceptNextAlert = true; 
    } 
    } 
} 

^^^測試用例1 ^^^

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class TestCase2 { 
    private WebDriver driver; 
    private String baseUrl; 
    private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 
    if (TestCase1.driver != null) { 
     driver = TestCase1.driver; 
    } else { 
     driver = new FirefoxDriver(); 
    } 
    baseUrl = "http://www.google.com/"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testOpen() throws Exception { 
    driver.get(baseUrl + ""); 
    } 

@After 
    public void tearDown() throws Exception { 
    //driver.quit(); 
    String verificationErrorString = verificationErrors.toString(); 
    if (!"".equals(verificationErrorString)) { 
     fail(verificationErrorString); 
    } 
    } 

    private boolean isElementPresent(By by) { 
    try { 
     driver.findElement(by); 
     return true; 
    } catch (NoSuchElementException e) { 
     return false; 
    } 
    } 

    private boolean isAlertPresent() { 
    try { 
     driver.switchTo().alert(); 
     return true; 
    } catch (NoAlertPresentException e) { 
     return false; 
    } 
    } 

    private String closeAlertAndGetItsText() { 
    try { 
     Alert alert = driver.switchTo().alert(); 
     String alertText = alert.getText(); 
     if (acceptNextAlert) { 
     alert.accept(); 
     } else { 
     alert.dismiss(); 
     } 
     return alertText; 
    } finally { 
     acceptNextAlert = true; 
    } 
    } 
} 

^^^測試案例2 ^^^

感謝您收到的任何幫助!

回答

0

您必須將驅動程序實例的訪問修飾符從private改爲static,以便第二個類可以訪問它。 同時確保兩個類在相同的包。

這是應該如何在TestCase1類

public static WebDriver driver; 

我嘗試過了,它正常工作與我。

相關問題