2013-12-13 21 views
0

我有以下兩種方法在我所有的類:如何在Selenium 2的類中共享@Before和@Test?

@Before 
public void setUp() throws Exception {  
    WebDriver driver = new FirefoxDriver(); 
    String baseUrl = "http://trn-test-web.fon.com/"; 
    selenium = new WebDriverBackedSelenium(driver, baseUrl); 
} 

@Test 
public void firstTest() throws Exception{ 
    selenium.open("/opencase/login.seam"); 
    selenium.type("//input[contains(@id, ':username')]", "admin"); 
    selenium.type("//input[contains(@id,':mypassword')]", "admin"); 
    selenium.click("//a[contains(@id,'loginForm:')]/span"); 
    selenium.waitForPageToLoad("60000");   
    _wait("Home", "id=oc-title-id"); 
} 

我如何可以共享一個項目的@Before@Test方法與其他類,因此他們不會公開自己的登錄頁面,並使用相同baseUrl並在類之間登錄?

回答

1

要做到這一點,最好的方法是將@Before方法放在抽象類中,例如AbstractUITest,並讓所有測試從該類繼承,這樣每次測試運行時,@Before方法都是從抽象類執行,所有這些都可以使用baseUrl。

3

可以使類(TestBase),將你的測試來延長

public class TestBase { 

    protected WebDriver driver; 
    protected Selenium selenium 

@Before 
public void setUp() throws Exception {  
    driver = new FirefoxDriver(); 
    String baseUrl = "http://trn-test-web.fon.com/"; 
    selenium = new WebDriverBackedSelenium(driver, baseUrl); 
} 

@Test 
public void firstTest() throws Exception{ 
    selenium.open("/opencase/login.seam"); 
    selenium.type("//input[contains(@id, ':username')]", "admin"); 
    selenium.type("//input[contains(@id,':mypassword')]", "admin"); 
    selenium.click("//a[contains(@id,'loginForm:')]/span"); 
    selenium.waitForPageToLoad("60000");   
    _wait("Home", "id=oc-title-id"); 
} 

這裏使用它如何

public class ExampleTest extends TestBase { 

@Test 
public void secondTest() throws Exception { 
    selenium.open("http://stackoverflow.com/"); 
} 

現在每次運行類ExampleTest時,它將運行firstTest and secondTest

相關問題