【转帖】WebDriver拾级而上(14) – RemoteWebDriver
当本机上没有浏览器,需要远程调用浏览器进行自动化测试时,需要用到RemoteWebDirver.
一、使用RemoteWebDriver
CODE:
<font size="4">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);
}
}</font>
二、SeleniumServer
在使用RemoteDriver时,必须在远程服务器启动一个SeleniumServer:
CODE:
<font size="4">java -jar selenium-server-standalone-2.20.0.jar -port 4446</font>
三、How to setFirefox profile using RemoteWebDriver
CODE:
<font size="4">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");</font>
向楼主致敬!
页:
[1]