WebDriver拾级而上·之十四 RemoteWebDriver


当本机上没有浏览器,需要远程调用浏览器进行自动化测试时,需要用到RemoteWebDirver.

一、使用RemoteWebDriver

import java.io.File; 
import java.net.URL;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
    public void myTest()throws Exception {
        WebDriver driver = newRemoteWebDriver(
                               new URL("http://localhost:4446/wd/hub"),
                               DesiredCapabilities.firefox());
       driver.get("http://www.google.com");

        // RemoteWebDriverdoes not implement the TakesScreenshot class
        // if the driver doeshave the Capabilities to take a screenshot
        // then Augmenter willadd the TakesScreenshot methods to the instance
        WebDriveraugmentedDriver = new Augmenter().augment(driver);
        File screenshot =((TakesScreenshot)augmentedDriver).
                            getScreenshotAs(OutputType.FILE);
    }
}

二、SeleniumServer

在使用RemoteDriver时,必须在远程服务器启动一个SeleniumServer:

java -jar selenium-server-standalone-2.20.0.jar -port 4446

三、How to setFirefox profile using RemoteWebDriver

profile = new FirefoxProfile();
profile.setPreference("general.useragent.override",testData.getUserAgent());
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("firefox_profile", profile);
driver = new RemoteWebDriver(new URL(“http://localhost:4446/wd/hub”),capabilities);
driverWait = new WebDriverWait(driver,TestConstant.WAIT_ELEMENT_TO_LOAD);
driver.get("http://www.google.com");