代碼和selenium driver相同 只是 啟動(dòng)環(huán)境方式不同。至少啟動(dòng)一個(gè)hub 一個(gè) node 。如需要多個(gè),可以使用端口進(jìn)行區(qū)分。
java -jar selenium-server-standalone-x.xx.x.jar -role node -port 5555
java -jar selenium-server-standalone-x.xx.x.jar -role node -port 5556
java -jar selenium-server-standalone-x.xx.x.jar -role node -port 5557
代碼如下
WebDriver wd = new RemoteDriver("http://localhost:4444/wd/hub", aDesiredcap);
//test01: 只匹配Windows下的ie來(lái)執(zhí)行此用例,版本不限;多個(gè)版本匹配成功時(shí)優(yōu)先級(jí)暫未知
DesiredCapabilities aDesiredcap = DesiredCapabilities();
aDesiredcap.setBrowserName("internet explorer")
aDesiredcap.setVersion("")
aDesiredcap.setPlatform(Platform.WINDOWS)
WebDriver wd = new RemoteDriver("http://localhost:4444/wd/hub", aDesiredcap);
wd.doSomething()
//test02: 只匹配linix下的firefox的版本為22的瀏覽器執(zhí)行用例;
DesiredCapabilities aDesiredcap = DesiredCapabilities("firefox", "22", Platform.LINUX);
WebDriver wd = new RemoteDriver("http://localhost:4444/wd/hub", aDesiredcap);
wd.doSomething()
//test03: 只匹配MAC下的safari瀏覽器執(zhí)行,版本不限
DesiredCapabilities aDesiredcap = DesiredCapabilities.safari();
aDesiredcap.setPlatform(Platform.MAC)
WebDriver wd = new RemoteDriver("http://localhost:4444/wd/hub", aDesiredcap);
wd.doSomething()
//test04: 只匹配chrome瀏覽器,任意平臺(tái),任意版本
DesiredCapabilities aDesiredcap = DesiredCapabilities.chrome();
aDesiredcap.setPlatform(Platform.ANY)
WebDriver wd = new RemoteDriver("http://localhost:4444/wd/hub", aDesiredcap);
wd.doSomething()