forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HtmlUnitDriver: basic Alert implementation
This adds basic implementation of Alert, further steps are needed at HtmlUnit and HtmlUnitDriver sides to fully implement it. Signed-off-by: Luke Inman-Semerau <luke.semerau@gmail.com>
- Loading branch information
Showing
7 changed files
with
159 additions
and
29 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
java/client/src/org/openqa/selenium/htmlunit/HtmlUnitAlert.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.htmlunit; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
|
||
import org.apache.http.auth.Credentials; | ||
import org.openqa.selenium.Alert; | ||
import org.openqa.selenium.ElementNotVisibleException; | ||
import org.openqa.selenium.NoAlertPresentException; | ||
|
||
import com.gargoylesoftware.htmlunit.AlertHandler; | ||
import com.gargoylesoftware.htmlunit.Page; | ||
|
||
class HtmlUnitAlert implements Alert, AlertHandler { | ||
|
||
private HtmlUnitDriver driver; | ||
private Map<Page, Queue<String>> queues = new HashMap<>(); | ||
|
||
HtmlUnitAlert(HtmlUnitDriver driver) { | ||
this.driver = driver; | ||
driver.getWebClient().setAlertHandler(this); | ||
} | ||
|
||
@Override | ||
public void dismiss() { | ||
accept(); | ||
} | ||
|
||
@Override | ||
public void accept() { | ||
Queue<String> queue = getCurrentQueue(); | ||
if (queue == null || queue.poll() == null) { | ||
throw new NoAlertPresentException(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getText() { | ||
Queue<String> queue = getCurrentQueue(); | ||
if (queue != null) { | ||
String text = queue.peek(); | ||
if (text != null) { | ||
return text; | ||
} | ||
} | ||
throw new NoAlertPresentException(); | ||
} | ||
|
||
@Override | ||
public void sendKeys(String keysToSend) { | ||
throw new ElementNotVisibleException("alert is not visible"); | ||
} | ||
|
||
@Override | ||
public void authenticateUsing(Credentials credentials) { | ||
} | ||
|
||
@Override | ||
public void handleAlert(Page page, String message) { | ||
Queue<String> queue = queues.get(page); | ||
if (queue == null) { | ||
queue = new LinkedList<String>(); | ||
queues.put(page, queue); | ||
} | ||
queue.add(message); | ||
} | ||
|
||
Queue<String> getCurrentQueue() { | ||
return queues.get(driver.getCurrentWindow().getEnclosedPage()); | ||
} | ||
|
||
/** | ||
* Closes the current window. | ||
*/ | ||
void close() { | ||
queues.remove(driver.getCurrentWindow()); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.