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:[email protected]: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:[email protected]: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(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。