我试图通过将 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 中)进行交互时,它没有选择它。感谢您的任何帮助。