RemoteWebDriver管理

时间:2020-12-19 13:27:25

直接贴代码:

    @Parameters({"BrowserType","NodeIP","NodePort"})
public void beforeTest(String sBrowserType,
String nodeIP,String nodePort) throws MalformedURLException{
String remoteUrl = String.format("http://%s:%s/wd/hub", nodeIP,
nodePort); sBrowserType = sBrowserType.toUpperCase();
switch(sBrowserType){
case "FIREFOX":
ProfilesIni ini = new ProfilesIni();
FirefoxProfile profile = ini.getProfile("default");
DesiredCapabilities ffcaps = DesiredCapabilities.firefox();
ffcaps.setCapability(FirefoxDriver.PROFILE,profile.toString());
driver = new RemoteWebDriver(new URL(remoteUrl), ffcaps);
break;
case "CHROME":
driver = new RemoteWebDriver(new URL(remoteUrl),DesiredCapabilities.chrome());
break;
case "IE":
driver = new RemoteWebDriver(new URL(remoteUrl),DesiredCapabilities.internetExplorer());
default:break;
}
driver.manage().window().maximize();
}

相应的在testng中加入参数:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="classes">
<parameter name="BrowserType" value="Chrome"></parameter>
<parameter name="BrowserVersion" value="30"></parameter>
<parameter name="NodeIP" value="192.168.1.2"></parameter>
<parameter name="NodePort" value="55555"></parameter>
<test name="大中国">
<classes>
<class name="test.ApplyPartnerTest"/>
</classes>
</test>
<!--监听接口 -->
 <listeners>
     <listener class-name = "org.uncommons.reportng.HTMLReporter" />
    <listener class-name = "org.uncommons.reportng.JUnitXMLReporter" />
    </listeners>
</suite>