Created
August 30, 2012 16:43
-
-
Save djangofan/3532777 to your computer and use it in GitHub Desktop.
Method to find newer window handle with WebDriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ts.test; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.MalformedURLException; | |
import java.net.Socket; | |
import java.net.URL; | |
import org.openqa.selenium.Capabilities; | |
//import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebDriverException; | |
import org.openqa.selenium.firefox.ExtensionConnection; | |
import org.openqa.selenium.firefox.FirefoxBinary; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.firefox.FirefoxProfile; | |
import org.openqa.selenium.internal.SocketLock; | |
import org.openqa.selenium.logging.LocalLogs; | |
import org.openqa.selenium.net.NetworkUtils; | |
import org.openqa.selenium.remote.Command; | |
import org.openqa.selenium.remote.HttpCommandExecutor; | |
import org.openqa.selenium.remote.Response; | |
public class ReusableFirefoxDriver extends FirefoxDriver { | |
private HttpCommandExecutor httpClient; | |
@Override | |
protected void startSession(Capabilities desiredCapabilities) { | |
if(localServerURL!=null){ | |
httpClient = new HttpCommandExecutor(localServerURL); | |
} | |
super.startSession(desiredCapabilities); | |
} | |
@Override | |
protected ExtensionConnection connectTo(FirefoxBinary binary, FirefoxProfile profile, String host) { | |
localServerURL = getURLofExistingLocalServer(); | |
if(localServerURL!=null){ | |
return new ExtensionConnection() { | |
@Override | |
public Response execute(Command command) throws IOException { | |
return httpClient.execute(command); | |
} | |
@Override | |
public void start() throws IOException { | |
//NOOP | |
} | |
@Override | |
public void quit() { | |
//NOOP | |
} | |
@Override | |
public boolean isConnected() { | |
try { | |
httpClient.getAddressOfRemoteServer().openConnection().connect(); | |
return true; | |
} catch (IOException e) { | |
return false; | |
} | |
} | |
@Override | |
public void setLocalLogs(LocalLogs logs) { | |
// TODO Auto-generated method stub | |
} | |
}; | |
} | |
return super.connectTo(binary, profile, host); | |
} | |
private URL localServerURL = null; | |
private URL getURLofExistingLocalServer() { | |
Socket socket = new Socket(); | |
try { | |
socket.bind(new InetSocketAddress("localhost", SocketLock.DEFAULT_PORT)); | |
return null; //Able to connect on default port (Assuming that default FF driver is not running) | |
} catch (IOException e) { | |
}finally{ | |
try { | |
socket.close(); | |
} catch (IOException e) { | |
} | |
} | |
try { | |
return new URL("http",new NetworkUtils().obtainLoopbackIp4Address(), SocketLock.DEFAULT_PORT,"/hub"); | |
} catch (MalformedURLException e) { | |
throw new WebDriverException(e); | |
} | |
} | |
/* | |
public static void main(String...a) throws Exception { | |
WebDriver driver = new ReusableFirefoxDriver(); | |
driver.get("http://www.google.com"); | |
System.out.println("Start second driver (Press enter)?"); | |
System.in.read(); | |
//The below code could be elsewhere in a different JVM | |
driver = new ReusableFirefoxDriver(); | |
driver.get("http://www.bing.com"); | |
} */ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String findNewerWindowHandle(WebDriver driver, Set<String> baselineHandles, int timeout) | |
{ | |
String foundHandle = ""; | |
long t = System.currentTimeMillis(); | |
long endTime = t + timeout * 1000; | |
while ( foundHandle.isEmpty() && System.currentTimeMillis() < endTime ) | |
{ | |
Set<String> currentHandles = driver.getWindowHandles(); | |
if (currentHandles.size() != baselineHandles.size() ) | |
{ | |
for ( String currentHandle : currentHandles ) | |
{ | |
if ( !baselineHandles.contains( currentHandle ) ) | |
{ | |
foundHandle = currentHandle; | |
break; | |
} | |
} | |
} | |
if ( foundHandle.isEmpty() ) | |
{ | |
try { | |
Thread.sleep(250); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
// Note: could optionally check for handle found here and throw | |
// an exception if no window was found. | |
return foundHandle; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testLogin() | |
{ | |
System.out.println("Starting test run..."); | |
loginToPage(); | |
TaskListPage adefault = new TaskListPage(driver); | |
adefault.clickNewFormTab(); | |
String popupHandle = FormUtils.findNewerWindowHandle(driver, blHandles, 30); | |
if ( !popupHandle.isEmpty() ) | |
{ | |
driver.switchTo().window( popupHandle ); | |
formHandle = driver.getWindowHandle(); | |
} | |
driver.switchTo().window( aHandle ).close() ; | |
driver.switchTo().window( formHandle ); | |
driver.manage().window().setPosition(new Point(220, 20)); | |
driver.manage().window().setSize(new Dimension(1200, 1000)); | |
FormUtils.waitSeconds(5); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void loginToPage() | |
{ | |
driver = new ReusableFirefoxDriver(); | |
System.out.println("Preparing browser with session id " + driver.getSessionId() ); | |
System.out.println("Preparing URL: " + baseProto + "://" + baseURL + ":" + basePort + "/" + afmURI); | |
driver.manage().window().setPosition(new Point(200, 10)); | |
driver.manage().window().setSize(new Dimension(1200, 1000)); | |
driver.navigate().to(baseProto + "://" + baseURL + ":" + basePort + "/" + afmURI); | |
blHandles = driver.getWindowHandles(); | |
aHandle = driver.getWindowHandle(); | |
if ( !driver.findElement( By.id("gwt-uid-5") ).isDisplayed() ) { | |
System.out.println("Not logged in yet."); | |
LoginPage alp = new LoginPage(driver); | |
alp.setLoginName("me"); | |
alp.setPassword("password"); | |
alp.setAgency(agName); | |
alp.clickLogin(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Thank You for sharing this grt info. I am actually getting stuck in setting up the grid 2. Can you please send me any related article. In selenium official site, its not clear to me.