Java Code Examples for org.openqa.selenium.Alert#dismiss()

The following examples show how to use org.openqa.selenium.Alert#dismiss() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Browser.java    From aquality-selenium-java with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 2
Source File: WebDriverEventListenerCompatibilityTest.java    From java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void alertEventTest() {
    try {
        Alert alert = driver.switchTo().alert();
        alert.accept();
        alert.dismiss();
        alert.sendKeys("Keys");

        assertThat(listener.messages,
                hasItems(WEBDRIVER_EVENT_LISTENER + "Attempt to accept alert",
                        WEBDRIVER_EVENT_LISTENER + "The alert was accepted",
                        WEBDRIVER_EVENT_LISTENER + "Attempt to dismiss alert",
                        WEBDRIVER_EVENT_LISTENER + "The alert was dismissed"));
    } finally {
        listener.messages.clear();
    }
}
 
Example 3
Source File: Screenshoter.java    From teasy with MIT License 6 votes vote down vote up
public String takeScreenshot(final String errorMessage, final String testName) {
    try {
        BufferedImage image;
        if (Configuration.browser.equals("chrome")) {
            image = ImageIO.read(new ChromeScreenshoter().getFullScreenshotAs(OutputType.FILE));
        } else {
            image = ImageIO.read(((TakesScreenshot)DriverHolder.getDriver()).getScreenshotAs(OutputType.FILE));
        }

        printStrings(image, removeNL(testName, errorMessage));

        final String pathName = getFilenameFor(testName);
        final File screenShotWithProjectPath = new File(pathName);
        ImageIO.write(image, "png", screenShotWithProjectPath);
        attachScreenShotToAllure(errorMessage, testName, screenShotWithProjectPath);
        return screenShotWithProjectPath.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    } catch (UnhandledAlertException alertException) {
        Alert alert = DriverHolder.getDriver().switchTo().alert();
        alert.dismiss();
        return takeScreenshot(errorMessage, testName);
    }
}
 
Example 4
Source File: DocSearchToAnotherViewAft.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Test
    /**
     * test that after doc search, navigating to people flow maintenance view does not cause Javascript errors
     * and therefore interfere with JS functionality like validation
     */
	public void testDocSearchToAnotherViewBookmark() throws Exception {
		waitAndClick("img[alt=\"doc search\"]");
		waitForPageToLoad();
		selectFrame("iframeportlet");
		waitAndClick("td.infoline > input[name=\"methodToCall.search\"]");
	//	selectFrame("relative=top");
		driver.switchTo().defaultContent();
		waitAndClickByLinkText("Main Menu");
		waitAndClickByLinkText("People Flow");
		waitForPageToLoad();
		selectFrame("iframeportlet");
		waitAndClickByLinkText("Create New");
		waitForPageToLoad();
		fireEvent("document.documentHeader.documentDescription", "focus");
		waitAndTypeByName("document.documentHeader.documentDescription", "sample description");
		fireEvent("document.documentHeader.explanation", "focus");
		waitAndTypeByName("document.documentHeader.explanation", "sample explanation");		
//		((JavascriptExecutor)driver).executeScript("document.getElementById(\"uif-cancel\").focus();");
        jGrowl("Click Cancel");
        waitAndClickByXpath("//div[@class='uif-footer clearfix']/button[contains(text(), 'Cancel')]");
		Thread.sleep(5000);
		final String text = "Form has unsaved data. Do you want to leave anyway?";
		Alert a=driver.switchTo().alert();
		assertTrue(a.getText().equals(text));
		a.dismiss();
        passed();
	}
 
Example 5
Source File: BrowserTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
@WaitUntil
public boolean dismissAlert() {
    Alert alert = getAlert();
    boolean result = false;
    if (alert != null) {
        alert.dismiss();
        onAlertHandled(false);
        result = true;
    }
    return result;
}
 
Example 6
Source File: ElementAction.java    From PatatiumWebUi with Apache License 2.0 5 votes vote down vote up
/**
 * 点击取消按钮
 */
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;
	}
}
 
Example 7
Source File: ITRenameBucketDuplicate.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 8
Source File: ESStoreAssetOverrideExistingPageTestCase.java    From product-es with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 9
Source File: WebDriverITBase.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Dismiss the javascript alert (clicking Cancel)
 *
*/
protected void dismissAlert()
{
    Alert alert = driver.switchTo().alert();
    //update is executed
    alert.dismiss();
}
 
Example 10
Source File: ITCreateBucketCancel.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 11
Source File: ESPublisherNewGlobalPageTestCase.java    From product-es with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 12
Source File: ITCreateDuplicateBucket.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 13
Source File: ESPublisherAssetOverrideRendererTestCase.java    From product-es with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 14
Source File: ESStoreAddedAssetTestCase.java    From product-es with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 15
Source File: WebDriverWebController.java    From stevia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void promptInputPressCancel(String inputMessage) {
	Alert alert = waitForAlert();
	alert.sendKeys(inputMessage);
	alert.dismiss();

}
 
Example 16
Source File: ESPublisherAssetOverridePartialTestCase.java    From product-es with Apache License 2.0 5 votes vote down vote up
private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
 
Example 17
Source File: AlertActions.java    From vividus with Apache License 2.0 4 votes vote down vote up
@Override
void process(Alert alert, IWebDriverManager webDriverManager)
{
    alert.dismiss();
}
 
Example 18
Source File: HandleModal.java    From opentest with MIT License 4 votes vote down vote up
@Override
public void run() {

    super.run();

    String perform = this.readStringArgument("perform");

    this.waitForAsyncCallsToFinish();

    WebDriverWait wait = new WebDriverWait(this.driver, this.getExplicitWaitSec());
    wait.until(ExpectedConditions.alertIsPresent());
    
    Alert alert = driver.switchTo().alert();
    
    String text;

    switch (perform.toLowerCase()) {
        case "accept":
            alert.accept();
            break;
        case "dismiss":
            alert.dismiss();
            break;
        case "gettext":
            text = alert.getText();
            this.writeOutput("text", text);
            break;
        case "sendkeys":
            text = this.readStringArgument("text", null);
            String key = this.readStringArgument("key", null);

            if (text != null) {
                alert.sendKeys(text);
            } else if (key != null) {
                String keyString = Keys.valueOf(key.toUpperCase()).toString();
                alert.sendKeys(keyString);
            } else {
                throw new RuntimeException("Neither the \"text\" argument, nor the \"key\" argument were provided.");
            }

            break;
        default:
            throw new RuntimeException(
                    "The \"perform\" argument must have one of the following values: accept, "
                    + "authenticate, dismiss, getText, sendKeys, setCredentials.");
    }
}
 
Example 19
Source File: KWSAlert.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean _executeStep( Page pageObject, WebDriver webDriver, Map<String, Object> contextMap, Map<String, PageData> dataMap, Map<String, Page> pageMap, SuiteContainer sC, ExecutionContextTest executionContext )
{
	try
	{
		WebDriverWait alertWait = new WebDriverWait( webDriver, 5 );
		Alert currentAlert = alertWait.until( new Function<WebDriver,Alert>(){
		    @Override
		    public Alert apply( WebDriver t )
		    {
		        return ExpectedConditions.alertIsPresent().apply( t );
		        
		    }
		} );
   		
   		if ( getContext() != null && !getContext().isEmpty() )
   		    addContext( getContext(), currentAlert.getText(), contextMap, executionContext );
   		
   		switch( ALERT_TYPE.valueOf( getName() ) )
   		{
   		    case ACCEPT:
   		        currentAlert.accept();
   		        break;
   		        		        
   		    case DISMISS:
   		        currentAlert.dismiss();
   		        break;
   		        
   		    case SEND_KEYS:
   		        currentAlert.sendKeys( getParameterValue( getParameterList().get( 0 ), contextMap, dataMap, executionContext.getxFID() ) + "" );
   		        currentAlert.accept();
   		        break;

   		    default:
   		        log.warn( "Unhandled Alert Type: " + getName() );
   		            
   		}
	}
	catch( NoAlertPresentException e )
	{
	    return false;
	}
	
	
	return true;
}
 
Example 20
Source File: WebDriverUtils.java    From rice with Educational Community License v2.0 2 votes vote down vote up
/**
 * <p>
 * Dismiss the javascript alert (clicking Cancel).
 * </p>
 *
 * @param driver WebDriver to dismiss alert on
 */
public static void alertDismiss(WebDriver driver) {
    Alert alert = driver.switchTo().alert();
    jGrowl(driver, "AFT Step", false, "AFT Step: Dismiss Alert " + WebDriverUtils.alertText(driver));
    alert.dismiss();
}