看了 http://blog.csdn.net/wwwqjpcom/article/details/51232302 这个的文章,照葫芦画瓢,弄了一个IE版本。
MyIEDriver.java
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import org.openqa.selenium.Capabilities; import org.openqa.selenium.Platform; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.Command; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.DriverCommand; import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.remote.Response; import org.openqa.selenium.remote.internal.WebElementToJsonConverter; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; public class MyIEDriver extends InternetExplorerDriver { public static String sid = "0277ded1-a5fa-40f6-a396-9c192049051c"; public static String url = "http://localhost:38005"; private Capabilities mycapabilities; public MyIEDriver() { mystartClient(url); mystartSession(sid); } @Override protected void startSession(Capabilities desiredCapabilities, Capabilities requiredCapabilities) { } @Override protected void startClient(Capabilities desiredCapabilities, Capabilities req) { } protected void mystartClient(String localserver) { HttpCommandExecutor delegate = null; try { URL driverserver = new URL(localserver); delegate = new HttpCommandExecutor(driverserver); } catch (MalformedURLException e) { e.printStackTrace(); } try { delegate.getAddressOfRemoteServer().openConnection().connect(); super.setCommandExecutor(delegate); System.out.println("Connect to the existing browser"); } catch (IOException e) { System.out.println(e.getMessage()); System.out.println("can not connect" + delegate.getAddressOfRemoteServer() + " and " + delegate); } } protected void mystartSession(String sessionID) { if (!sessionID.isEmpty()) { super.setSessionId(sessionID); } Command command = new Command(super.getSessionId(), DriverCommand.NEW_SESSION); Response response; try { response = super.getCommandExecutor().execute(command); } catch (IOException e1) { e1.printStackTrace(); System.out.println("Can't use this Session"); return; } Map<String, Object> rawCapabilities = (Map<String, Object>) response.getValue(); DesiredCapabilities returnedCapabilities = (DesiredCapabilities) super.getCapabilities(); if (returnedCapabilities == null) { returnedCapabilities = new DesiredCapabilities(); } for (Map.Entry<String, Object> entry : rawCapabilities.entrySet()) { // Handle the platform later if (CapabilityType.PLATFORM.equals(entry.getKey())) { continue; } returnedCapabilities.setCapability(entry.getKey(), entry.getValue()); } String platformString = (String) rawCapabilities.get(CapabilityType.PLATFORM); Platform platform; try { if (platformString == null || "".equals(platformString)) { platform = Platform.ANY; } else { platform = Platform.valueOf(platformString); } } catch (IllegalArgumentException e) { // The server probably responded with a name matching the os.name // system property. Try to recover and parse this. platform = Platform.extractFromSysProperty(platformString); } returnedCapabilities.setPlatform(platform); this.mycapabilities = returnedCapabilities; } @Override public void quit() { try { execute(DriverCommand.QUIT); } catch (Exception e) { e.printStackTrace(); } } public Capabilities getCapabilities() { return mycapabilities; } @Override public Object executeScript(String script, Object... args) { if (!mycapabilities.isJavascriptEnabled()) { throw new UnsupportedOperationException( "You must be using an underlying instance of WebDriver that supports executing javascript"); } // Escape the quote marks script = script.replaceAll("\"", "\\\""); Iterable<Object> convertedArgs = Iterables.transform(Lists.newArrayList(args), new WebElementToJsonConverter()); Map<String, ?> params = ImmutableMap.of("script", script, "args", Lists.newArrayList(convertedArgs)); return execute(DriverCommand.EXECUTE_SCRIPT, params).getValue(); } @Override public Object executeAsyncScript(String script, Object... args) { if (!mycapabilities.isJavascriptEnabled()) { throw new UnsupportedOperationException( "You must be using an underlying instance of " + "WebDriver that supports executing javascript"); } // Escape the quote marks script = script.replaceAll("\"", "\\\""); Iterable<Object> convertedArgs = Iterables.transform(Lists.newArrayList(args), new WebElementToJsonConverter()); Map<String, ?> params = ImmutableMap.of("script", script, "args", Lists.newArrayList(convertedArgs)); return execute(DriverCommand.EXECUTE_ASYNC_SCRIPT, params).getValue(); } }
开启浏览器 :
import java.net.URL; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.HttpCommandExecutor; public class AppTest { public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "D:/auto/public/selenium/webdriver/win64/IEDriverServer.exe"); InternetExplorerDriver driver = new InternetExplorerDriver(); String sessionid = driver.getSessionId().toString(); System.out.println("session ID --->" + sessionid); URL url = ((HttpCommandExecutor)(driver.getCommandExecutor())).getAddressOfRemoteServer(); System.out.println("url --->" + url.toString()); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.get("http://www.google.com"); WebElement key =driver.findElement(By.id("lst-ib")); key.sendKeys("abc"); Set set = driver.getWindowHandles(); // driver.getCommandExecutor().getAddressOfRemoteServer().toString(); } }
不关闭浏览器,另外一段代码复用刚开启的浏览器 :
public class Apptest2 { public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "D:/auto/public/selenium/webdriver/win64/IEDriverServer.exe"); InternetExplorerDriver driver = new MyIEDriver(); // driver.get("http://localhost:38005"); driver.get("http://www.baidu.com"); // driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // WebElement key =driver.findElement(By.id("lst-ib")); // key.sendKeys("g"); } }
相关推荐
Selenium Webdriver使用已打开的浏览器
全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用实例,第2部分实战应用篇基于丰富的实战案例讲解页面元素的定位方法以及...
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
selenium RC与selenium webdriver的区别
关于Selenium WebDriver工作原理的介绍,原理相关描述
seleniumwebdriver(python) (第三版)
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)...
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
Acquire an understanding of the various WebDriver implementations such as Firefox Driver, IE Driver, and Chrome Driver Explore WebDriver EventListeners Design effective automation frameworks using ...
自动化测试:Selenium webdriver学习笔记C#版
selenium2.0自动化
selenium webdriver基于python源码案例,全是案例适合小白入门学习
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
selenium3.0 webdriver+python3.0实例自学参考