如何在类之间传递对象

时间:2013-04-22 22:03:50

标签: java selenium

我试图通过将Selenium脚本分解为三个类(Grid,Browser和testCase)来封装它。我能够让浏览器打开,但我似乎错过了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中)进行交互时,它没有将其拾取。谢谢你的帮助。

2 个答案:

答案 0 :(得分: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();
    }
    ...

答案 1 :(得分:0)

我没有看到你在浏览器的setUp(String url)方法中初始化你的Selenium对象。而且这种方法也没有在这里调用。

您还将浏览器的Selenium对象设为公共,因此您无需在TestCase中声明由访问者方法分配的另一个Selenium对象 - 您只需从TestCase的Browser对象中引用它。