`

Selenium Webdriver重新使用已打开的浏览器实例(IE 版)

阅读更多

看了  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");

	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics