2013-04-22 229 views
0

我試圖通過將它分解爲三個類(Grid,Browser和testCase)來封裝Selenium腳本。我能夠打開瀏覽器,但我似乎錯過了testCase類插入其命令的連接。如何在類之間傳遞對象

Grid.java

package com.autotrader.grid; 

import org.junit.After; 
import org.junit.Test; 

public class Grid { 
    Browser browser = new Browser(); 
    TestCase testCase = new TestCase(); 

    public Grid() { 
     browser.setUp("http://pbskids.org"); 
    } 

    @Test 
    public void main() { 
     testCase.runCase(); 
    } 

    @After 
    public void tearDown() throws Exception { 
     browser.stop(); 
    } 
} 

Browser.java

package com.autotrader.grid; 

import com.thoughtworks.selenium.Selenium; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebDriverBackedSelenium; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 

import java.util.concurrent.TimeUnit; 

public class Browser { 

    private String baseUrl; 
    private String driverNamespace; 
    private String driverLocation; 

    private DesiredCapabilities capabilities; 
    private WebDriver driver; 
    public Selenium selenium; 

    // constructor 
    public Browser() { 

    } 

    public DesiredCapabilities getCapabilities() { 
     return this.capabilities; 
    } 

    public String getDriverLocation() { 
     return this.driverLocation; 
    } 

    public String getDriverNamespace() { 
     return this.driverNamespace; 
    } 

    public Selenium getSelenium(){ 
     return selenium; 
    } 

    public void open (String url) { 
     this.selenium.open(url); 
    } 

    public void setBaseUrl(String url) { 
     this.baseUrl = url; 
    } 

    public void setCapabilities() { 
     this.capabilities = DesiredCapabilities.firefox(); 
     this.driver = new FirefoxDriver(capabilities); 
    } 

    public void setDriverLocation(String location) { 
     this.driverLocation = location; 
    } 

    public void setDriverNamespace(String namespace) { 
     this.driverNamespace = namespace; 
    } 

    public void setSpeed(String speed){ 
     this.selenium.setSpeed(speed); 
    } 

    public void setUp(String url){ 
     setDriverNamespace("webdriver.firefox.driver"); 
     setDriverLocation(System.getenv("ProgramFiles(x86)") + "\\Mozilla Firefox\\firefox.exe"); 
     System.setProperty(driverNamespace,driverLocation); 

     setCapabilities(); 
     setBaseUrl(url); 

     this.selenium = new WebDriverBackedSelenium(driver, url); 
     this.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    public void stop(){ 
     this.selenium.stop(); 
    } 
} 

TestCase.java

package com.autotrader.grid; 

import com.thoughtworks.selenium.Selenium; 

public class TestCase { 

    Browser browser = new Browser(); 
    Selenium selenium; 

    // constructor 
    public TestCase(){ 
     selenium = browser.getSelenium(); 
    } 

    public void runCase(){ 
     selenium.open("/privacy/termsofuse.html?campaign=fkhp_tou"); 
     selenium.setSpeed("1000"); 
    } 
} 

左右;然後我使用Selenium對象(在Browser.java中)打開驅動程序,但是當我嘗試與Selenium對象(在TestCase.java中)進行交互時,它沒有選擇它。感謝您的任何幫助。

回答

0

相反的:

public class Grid { 
    Browser browser = new Browser(); 
    TestCase testCase = new TestCase(); 

    public Grid() { 
    ... 

嘗試:

public class Grid { 
    Browser browser = new Browser(); 
    TestCase testCase = new TestCase(browser); // <-- this line changed 

    public Grid() { 
    ... 

而且在這裏:

public class TestCase { 

    Browser browser = new Browser(); 
    Selenium selenium; 

    // constructor 
    public TestCase(){ 
     selenium = browser.getSelenium(); 
    } 
    ... 

這樣做:

public class TestCase { 

    Browser browser = new Browser();    // <-- this line changed 
    Selenium selenium; 

    // constructor 
    public TestCase(Browser browser){   // <-- this line changed 
     this.browser = browser;     // <-- this line was added 
     selenium = browser.getSelenium(); 
    } 
    ... 
+0

謝謝。那就是訣竅。我做的唯一不同的是在Grid.class的主函數中聲明瞭TestCase。我注意到,如果我在外面聲明它,那麼瀏覽器對象不會將所有值都附加到setUp()。 – fergatron 2013-04-24 16:44:32

0

我沒有看到您在初始化您的Selenium對象,除了在瀏覽器的setUp(String url)方法中。而且這個方法在這裏也沒有被調用。

您還公開了您的瀏覽器的Selenium對象,因此您無需在TestCase中聲明另一個由訪問器方法分配的Selenium對象 - 您可以從TestCase的Browser對象中引用它。

+0

本來我公開只是在課堂上使用它,但我從未想出如何正確應用它。我將其重新設置爲私人。 – fergatron 2013-04-24 16:45:44