下面列出了怎么用org.openqa.selenium.NoAlertPresentException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Accepts or declines prompt with sending message
*
* @param alertAction accept or decline
* @param text message to send
*/
public void handlePromptAlert(AlertActions alertAction, String text) {
try {
Alert alert = getDriver().switchTo().alert();
if (text != null && !text.isEmpty()) {
getDriver().switchTo().alert().sendKeys(text);
}
if (alertAction.equals(AlertActions.ACCEPT)) {
alert.accept();
} else {
alert.dismiss();
}
} catch (NoAlertPresentException exception) {
localizedLogger.fatal("loc.browser.alert.fail", exception);
throw exception;
}
}
@Test
public void shouldTryToCloseAlertWhenNotMobileDuringCleanup() {
//given
setUp(IS_MAXIMIZED, IS_REUSABLE, NOT_MOBILE);
when(webDriver.manage()).thenReturn(options);
when(webDriver.manage().window()).thenReturn(window);
when(webDriver.switchTo()).thenReturn(bobcatTargetLocator);
when(webDriver.switchTo().alert()).thenReturn(alert);
doThrow(new NoAlertPresentException()).when(alert).accept();
//when
testedObject.quit();
//then
verify(alert).accept();
}
/**
* 获取对话框文本
* @return 返回String
*/
public String getAlertText()
{
Alert alert=driver.switchTo().alert();
try {
String text=alert.getText().toString();
log.info("获取对话框文本:"+text);
return text;
} catch (NoAlertPresentException notFindAlert) {
// TODO: handle exception
log.error("找不到对话框");
//return "找不到对话框";
throw notFindAlert;
}
}
public static ExpectedCondition<Alert> alertIsPresent() {
return new ExpectedCondition<Alert>() {
@Override
public Alert apply(WebDriver driver) {
try {
return driver.switchTo().alert();
} catch (NoAlertPresentException e) {
return null;
}
}
@Override
public String toString() {
return "alert to be present";
}
};
}
@Test(groups = "wso2.es.common", description = "Test XSS in Gadgets Page")
public void testListGadgetsXSSTestCase() throws Exception {
String alertMessage = "XSS";
String url = baseUrl + "/store/assets/gadget/list?" +
"sortBy=overview_name&sort=}</script><script>alert('" + alertMessage + "')</script>";
driver.get(url);
boolean scriptInjected = false;
try {
String actualAlertMessage = closeAlertAndGetItsText(driver, false);
if (actualAlertMessage.equals(alertMessage))
scriptInjected = true;
} catch (NoAlertPresentException ex) {
}
assertFalse(scriptInjected, "Script injected via the query string");
}
private void testWaitForPageLoadSleepForTimeout()
{
Mockito.lenient().when(targetLocator.alert()).thenThrow(new NoAlertPresentException());
mockDescriptiveWait(ChronoUnit.DAYS);
when(javascriptActions.executeScript(SCRIPT_READY_STATE)).thenReturn(COMPLETE);
spy.waitForPageLoad();
verify(spy).sleepForTimeout(TIMEOUT_MILLIS);
}
@Test
void testIsAlertPresentNoAlertPresentException()
{
when(webDriver.switchTo()).thenReturn(targetLocator);
doThrow(NoAlertPresentException.class).when(targetLocator).alert();
boolean answer = alertActions.isAlertPresent(webDriver);
assertFalse(answer);
}
/**
* Gets the alerts collected by the driver.
* Note: it currently works only if no new page has been loaded in the window
* @param maxWaitTime the maximum time to wait to get the alerts (in millis)
* @param driver the driver
* @param alertsLength the expected length of Alerts
* @return the collected alerts
* @throws Exception in case of problem
*/
protected List<String> getCollectedAlerts(final long maxWaitTime, final WebDriver driver, final int alertsLength)
throws Exception {
final List<String> collectedAlerts = new ArrayList<>();
long maxWait = System.currentTimeMillis() + maxWaitTime;
while (collectedAlerts.size() < alertsLength && System.currentTimeMillis() < maxWait) {
try {
final Alert alert = driver.switchTo().alert();
final String text = alert.getText();
collectedAlerts.add(text);
alert.accept();
// handling of alerts requires some time
// at least for tests with many alerts we have to take this into account
maxWait += 100;
if (useRealBrowser()) {
if (getBrowserVersion().isIE()) {
// alerts for real IE are really slow
maxWait += 5000;
}
}
}
catch (final NoAlertPresentException e) {
Thread.sleep(10);
}
}
return collectedAlerts;
}
/**
*
* @return {@link Alert} object representing HTML alert, prompt or confirmation modal dialog
*/
private Alert getAlert() {
try {
return this.webDriver.switchTo().alert();
} catch (NoAlertPresentException e) {
throw new ElementNotFoundException(e);
}
}
/**
*
* @return {@link Alert} object representing HTML alert, prompt or confirmation modal dialog
*/
private Alert getAlert() {
try {
return this.webDriver.switchTo().alert();
} catch (NoAlertPresentException e) {
return null;
}
}
private boolean isAlertPresent(WebDriver Driver) {
try {
Driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
Logger.getLogger(this.getClass().getName()).log(Level.OFF, null, e);
return false;
}
}
private boolean isAlertPresent(WebDriver Driver) {
try {
Driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
Logger.getLogger(this.getClass().getName()).log(Level.OFF, e.getMessage(), e);
return false;
}
}
public boolean isAlertPresent() {
try {
Driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
Logger.getLogger(this.getClass().getName()).log(Level.OFF, null, e);
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private void cleanDriver() {
manage().deleteAllCookies();
get(BLANK_PAGE);
if (!mobile) {
try {
switchTo().alert().accept();
} catch (NoAlertPresentException e) {
LOG.debug("No alert was present when returnDriver was executed", e);
}
}
if (maximize) {
manage().window().maximize();
}
}
/**
* Accept any displayed alert message. If no alert is displayed, an exception will be thrown.
* <p>
* Fires {@link AcceptedAlertEvent} in case a alert was successfully accepted.
*
* @throws NoAlertPresentException in case no alert is present
* @see Alert#accept()
* @since 2.0
*/
public void accept() throws NoAlertPresentException {
StringBuilder builder = new StringBuilder();
ActionTemplate.browser(browser()).execute(browser -> {
Alert alert = webDriver().switchTo().alert();
builder.append(alert.getText());
alert.accept();
}).fireEvent(browser -> new AcceptedAlertEvent(builder.toString()));
log.debug("alert was accepted");
}
/**
* Declines any displayed alert message. If no alert is displayed, an exception will be thrown.
* <p>
* Fires {@link DeclinedAlertEvent} in case a alert was successfully accepted.
*
* @throws NoAlertPresentException in case no alert is present
* @see Alert#dismiss()
* @since 2.0
*/
public void decline() throws NoAlertPresentException {
StringBuilder builder = new StringBuilder();
ActionTemplate.browser(browser()).execute(browser -> {
Alert alert = webDriver().switchTo().alert();
builder.append(alert.getText());
alert.dismiss();
}).fireEvent(browser -> new DeclinedAlertEvent(builder.toString()));
log.debug("alert was declined");
}
/**
* 点击确认按钮
*/
public void alertConfirm()
{
Alert alert=driver.switchTo().alert();
try {
alert.accept();
log.info("点击确认按钮");
} catch (NoAlertPresentException notFindAlert) {
// TODO: handle exception
//throw notFindAlert;
log.error("找不到确认按钮");
throw notFindAlert;
}
}
/**
* 点击取消按钮
*/
public void alertDismiss()
{
Alert alert= driver.switchTo().alert();
try {
alert.dismiss();
log.info("点击取消按钮");
} catch (NoAlertPresentException notFindAlert) {
// TODO: handle exception
//throw notFindAlert;
log.error("找不到取消按钮");
throw notFindAlert;
}
}
public boolean isAlertPresent() {
try {
driver.switchTo().alert();
oLog.info("true");
return true;
} catch (NoAlertPresentException e) {
// Ignore
oLog.info("false");
return false;
}
}
@Test(groups = "wso2.cep", description = "Verifying XSS Vulnerability in event data sources - description field")
public void testXSSVenerabilityDescriptionField() throws Exception {
boolean isVulnerable = false;
// Login
driver.get(getLoginURL());
driver.findElement(By.id("txtUserName")).clear();
driver.findElement(By.id("txtUserName")).sendKeys(cepServer.getContextTenant().getContextUser().getUserName());
driver.findElement(By.id("txtPassword")).clear();
driver.findElement(By.id("txtPassword")).sendKeys(cepServer.getContextTenant().getContextUser().getPassword());
driver.findElement(By.cssSelector("input.button")).click();
// Sending request to even-tracer admin service
String url = backendURL.substring(0, 22) + "/carbon/ndatasource/newdatasource.jsp?";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("description", "RiskScoringDB\"><script>alert(1)</script><example attr=\""));
params.add(new BasicNameValuePair("edit", "true"));
url += URLEncodedUtils.format(params, "UTF-8");
driver.get(url);
try {
// Alert appears if vulnerable to XSS attack.
Alert alert = driver.switchTo().alert();
alert.accept();
isVulnerable = true;
} catch (NoAlertPresentException e) {
// XSS vulnerability is not there
}
Assert.assertFalse(isVulnerable);
driver.close();
}
@Test(groups = "wso2.cep", description = "Verifying XSS Vulnerability in event data sources - driver field")
public void testXSSVenerabilityDriverField() throws Exception {
boolean isVulnerable = false;
// Login
driver.get(getLoginURL());
driver.findElement(By.id("txtUserName")).clear();
driver.findElement(By.id("txtUserName")).sendKeys(cepServer.getContextTenant().getContextUser().getUserName());
driver.findElement(By.id("txtPassword")).clear();
driver.findElement(By.id("txtPassword")).sendKeys(cepServer.getContextTenant().getContextUser().getPassword());
driver.findElement(By.cssSelector("input.button")).click();
// Sending request to even-tracer admin service
String url = backendURL.substring(0, 22) + "/carbon/ndatasource/validateconnection-ajaxprocessor.jsp?";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("dsName", "John"));
params.add(new BasicNameValuePair("driver", "<script>alert(1)</script>"));
params.add(new BasicNameValuePair("url", "http://abc.com"));
params.add(new BasicNameValuePair("username", "John"));
params.add(new BasicNameValuePair("dsType", "RDBMS"));
params.add(new BasicNameValuePair("dsProviderType", "default"));
url += URLEncodedUtils.format(params, "UTF-8");
driver.get(url);
try {
// Alert appears if vulnerable to XSS attack.
Alert alert = driver.switchTo().alert();
alert.accept();
isVulnerable = true;
} catch (NoAlertPresentException e) {
// XSS vulnerability is not there
}
Assert.assertFalse(isVulnerable);
driver.close();
}
@Test(groups = "wso2.cep", description = "Verifying XSS Vulnerability in event data sources - data source name field")
public void testXSSVenerabilityNameField() throws Exception {
boolean isVulnerable = false;
// Login
driver.get(getLoginURL());
driver.findElement(By.id("txtUserName")).clear();
driver.findElement(By.id("txtUserName")).sendKeys(cepServer.getContextTenant().getContextUser().getUserName());
driver.findElement(By.id("txtPassword")).clear();
driver.findElement(By.id("txtPassword")).sendKeys(cepServer.getContextTenant().getContextUser().getPassword());
driver.findElement(By.cssSelector("input.button")).click();
// Sending request to even-tracer admin service
String url = backendURL.substring(0, 22) + "/carbon/ndatasource/newdatasource.jsp?";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("dsName", "RiskScoringDB\"><script>alert(1)</script><example attr=\""));
params.add(new BasicNameValuePair("edit", "true"));
url += URLEncodedUtils.format(params, "UTF-8");
driver.get(url);
try {
// Alert appears if vulnerable to XSS attack.
Alert alert = driver.switchTo().alert();
alert.accept();
isVulnerable = true;
} catch (NoAlertPresentException e) {
// XSS vulnerability is not there
}
Assert.assertFalse(isVulnerable);
driver.close();
}