0

我试图通过将 Selenium 脚本分成三个类(Grid、Browser 和 testCase)来封装它。我能够打开浏览器,但我似乎缺少 testCase 类插入其命令的连接。

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

浏览器.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();
    }
}

测试用例.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 中)进行交互时,它没有选择它。感谢您的任何帮助。

4

2 回答 2

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();
    }
    ...
于 2013-04-22T22:15:39.587 回答
0

setUp(String url)除了浏览器的方法外,我没有看到您曾经初始化过 Selenium 对象。并且这里也没有调用该方法。

您还公开了浏览器的 Selenium 对象,因此您无需在 TestCase 中声明另一个由访问器方法分配的 Selenium 对象——您只需从 TestCase 的 Browser 对象中引用它即可。

于 2013-04-22T22:23:52.193 回答