WebDriver中在多个浏览器窗口切换

package webdriver;

import java.util.logging.Logger;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * 
 * Change window in multi browser windows
 *
 */
public class TestWebDriver2 {
	private static final Logger logger = Logger.getLogger(TestWebDriver2.class.getName());
	private WebDriver webDriver;
	
	@BeforeClass
	public void beforeClass(){
		webDriver = new FirefoxDriver();
	}
	
	@Test
	public void f(){
		//open the first window
		//we have basic authentication, so specify username/password in the url
		webDriver.get("http://leizhen:passw0rd@****:2001/HTTPAdmin");
		logger.info(webDriver.getTitle());
		String currentWindow = webDriver.getWindowHandle();
		
		//open a new window
		Actions action = new Actions(webDriver);
		action.keyDown(Keys.CONTROL).sendKeys("n").perform();
		
		//change to the new opened window
		for(String window : webDriver.getWindowHandles()){
			if(!window.equals(currentWindow)){
				webDriver.switchTo().window(window);
				break;
			}
		}
		webDriver.get("http://leizhen:passw0rd@****:2001/HTTPAdmin");
		logger.info(webDriver.getTitle());
		
		//change back to the first window
		webDriver.switchTo().window(currentWindow);
		logger.info(webDriver.getTitle());
	}
	
	@AfterClass
	public void afterClass(){
		webDriver.quit();
	}
}


你可能感兴趣的:(webdriver)