Selenium Grid 多浏览器操作代码
以下是Selenium Grid 操作多浏览器的具体实现代码。
1 import java.net.URL; 2 import java.util.HashMap; 3 import java.util.Iterator; 4 import org.openqa.selenium.By; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.remote.DesiredCapabilities; 7 import org.openqa.selenium.remote.RemoteWebDriver; 8 public class Grid04 { 9 private static WebDriver driver; 10 private static DesiredCapabilities br; 11 public static void runBaiduTest(WebDriver driver){ 12 driver.get("https://www.baidu.com"); 13 driver.findElement(By.id("kw")).sendKeys("selenium"); 14 driver.findElement(By.id("su")).click(); 15 driver.quit(); 16 } 17 public static void main(String[] args) throws Exception { 18 HashMap<String, String> config = new HashMap<String, String>(); 19 config.put("ff", "http://127.0.0.1:4444/wd/hub"); 20 config.put("ie", "http://127.0.0.1:5555/wd/hub"); 21 config.put("chrome", "http://127.0.0.1:6666/wd/hub"); 22 //采用Iterator 遍历HashMap 23 Iterator<String> it = config.keySet().iterator(); 24 while(it.hasNext()) { 25 //获得字典的key(浏览器) 26 String browser = (String)it.next(); 27 System.out.println(browser); 28 //获得字典的value(节点) 29 String nodeip = config.get(browser); 30 System.out.println(nodeip); 31 if(browser.equals("ie")) br = DesiredCapabilities.internetExplorer(); 32 else if(browser.equals("ff")) br = DesiredCapabilities.firefox(); 33 else if(browser.equals("chrome")) br = DesiredCapabilities.chrome(); 34 else System.out.println("browser 参数有误,只能为ie、ff、chrome"); 35 driver = new RemoteWebDriver(new URL(nodeip),br); 36 runBaiduTest(driver); 37 } 38 } 39 }