Skip to content

Instantly share code, notes, and snippets.

@daluu
Created September 30, 2013 02:53
Show Gist options
  • Save daluu/6758854 to your computer and use it in GitHub Desktop.
Save daluu/6758854 to your computer and use it in GitHub Desktop.
SafariDriver alert handling possible workaround using HTTP requests & responses. This may or may not work for you depending on what your application under test does with the alerts.
/* Safari workaround below simply constructs the required HTTP POST data
* as key/value pairs (e.g. param1=value1&param2=value2, etc.)
* then makes an HTTP POST request with that data, then check the received
* HTTP response returned (e.g. status code 200 OK, or response body content contains what you expect)
* Example below currently skips checking response as that part is handled by the CustomHttpClient
* (implementation not shown). Making actual HTTP POST request is same thing. For details on that,
* see this for reference:
* http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests
* http://stackoverflow.com/questions/9129766/urlconnection-is-not-allowing-me-to-access-data-on-http-errors-404-500-etc
*
* You'll have to spy on what your web application does when alert is accepted to see if you can do something similar.
* Spy using HttpFox, LiveHttpHeaders, ieHttpHeaders, Firebug & similar browser developer tools to profile network
* requests, or Wireshark, etc.
*/
public void removeItemsFromCart(String currentSite) throws Exception {
if (browser.browserName != BrowserName.SF) { //for non Safari browsers
ArrayList<WebElement> itemsInCart = (ArrayList<WebElement>) driver.findElements(By.xpath(REMOVE_ITEM_LNK));
int nItem = itemsInCart.size();
for (int i=0; i<nItem; i++) {
itemsInCart.get(i).click();
Alert alert = driver.switchTo().alert();
//String AlertText = alert.getText(); //return this if you needed it...
alert.accept();
Thread.sleep(1000);
driver.navigate().refresh();
}
}
else{ //Safari workaround: Cannot handle confirmation
String url = currentSite;
//setup objects for removing cart items
ArrayList<WebElement> cartItems = (ArrayList<WebElement>) driver.findElements(By.xpath(REMOVE_ITEM_LNK));
CustomHttpClient httpClient = new CustomHttpClient();
httpClient.cookies.put("PHPSESSID", driver.manage().getCookieNamed("PHPSESSID").getValue());
httpClient.queryParams.put("method","removeItem");
//for each cart item "ID", make HTTP POST to web server for removing cart item
//(to mimic what browser client side code would do after accepting alert)
for (int i = 0; i < cartItems.size(); i++) {
//note that a put() will overwrite previous value, so no need to remove beforehand
httpClient.queryParams.put("cartItemId",cartItems.get(i).getAttribute("id"));
httpClient.postFormData(url + "/cartItemRemovalApiPath");
//for test optimization, we refresh page after removing all items (externally)
}
driver.navigate().refresh();
}
if(!isTextPresent("Your shopping cart is empty")){
throw new Exception("Cart item removal failed, cart not empty.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment