Search in sources :

Example 6 with UnhandledAlertException

use of org.openqa.selenium.UnhandledAlertException in project selenium_java by sergueik.

the class XMLHttpRequestAsyncTest method sendJSONArgDebugTest.

@Test(enabled = false)
public void sendJSONArgDebugTest() {
    String response = null;
    int country_index = 3;
    WebElement countryListElement = driver.findElement(By.xpath("//select[@id='country-list']"));
    try {
        Select select = new Select(countryListElement);
        select.selectByIndex(country_index);
    } catch (Exception e) {
        e.printStackTrace();
    }
    countryListElement.click();
    sleep(100);
    // https://stackoverflow.com/questions/15466802/how-can-i-auto-hide-alert-box-after-it-showing-it
    String inlineScript = "var callback = arguments[arguments.length - 1];\n" + "var option_data = (arguments.length > 1 )? arguments[arguments.length - 2]:'{\"option_name\": \"country_id\", \"option_index\": 3}';\n" + "var obj = JSON.parse(option_data);\n" + "var http = new XMLHttpRequest();\n" + "var url = 'get_state.php';\n" + "var params = obj.option_name + '=' + obj.option_index;\n" + "http.open('POST', url, true);\n" + "http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');\n" + "http.setRequestHeader('Content-length', params.length);\n" + "http.setRequestHeader('Connection', 'close');\n" + "http.onreadystatechange = function() {\n" + "    if (http.readyState == 4) {\n" + "        alert(http.responseText); callback(http.responseText);\n" + "    };\n" + "};\n" + "http.send(params);";
    try {
        String jsonArgs = String.format("{\"option_name\": \"%s\", \"option_index\": %d}", "country_id", country_index);
        System.out.println("sendJSONArgTest script: " + inlineScript);
        System.out.println("sendJSONArgTest args: " + jsonArgs);
        // error is likely here
        response = (String) js.executeAsyncScript(inlineScript, jsonArgs);
    } catch (UnhandledAlertException e) {
        System.err.println("Error executing XMLHttpRequest");
    }
    System.out.println("sendJSONArgTest response: " + response);
    // Assert
    assertThat(response, org.hamcrest.CoreMatchers.containsString("<option value=\"13\">Picardie</option>"));
    try {
        // https://stackoverflow.com/questions/8923398/regex-doesnt-work-in-string-matches
        assertTrue(response.replace("\n", "").matches(".*<option value=\"\\d+\">Picardie</option>.*"));
    } catch (AssertionError e) {
        System.err.println("Exception (ignored) " + e.toString());
    }
    String stateName = "Picardie";
    Pattern pattern = Pattern.compile(String.format(".*<option value=\"(\\d+)\">%s</option>.*", stateName), Pattern.CASE_INSENSITIVE);
    // ".*<option value=\"(\\d+)\">(["<>]+)</option>.*",
    Matcher matcher = pattern.matcher(response);
    int stateIndex = 0;
    assertTrue(matcher.find());
    stateIndex = Integer.parseInt(matcher.group(1).toString());
    // stateName = matcher.group(2).toString();
    assertTrue(stateIndex > 0);
    assertThat(stateName, notNullValue());
    System.err.println("Selected state index: " + stateIndex);
    WebElement stateListElement = driver.findElement(By.xpath("//select[@id='state-list']"));
    try {
        Select select = new Select(stateListElement);
        select.selectByVisibleText(stateName);
    } catch (Exception e) {
        e.printStackTrace();
    }
    stateListElement.click();
    sleep(100);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UnhandledAlertException(org.openqa.selenium.UnhandledAlertException) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) UnhandledAlertException(org.openqa.selenium.UnhandledAlertException) Test(org.testng.annotations.Test)

Aggregations

UnhandledAlertException (org.openqa.selenium.UnhandledAlertException)6 WebDriverException (org.openqa.selenium.WebDriverException)6 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 WebElement (org.openqa.selenium.WebElement)5 Select (org.openqa.selenium.support.ui.Select)5 Test (org.testng.annotations.Test)5 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 java.util.concurrent (java.util.concurrent)1 JavascriptException (org.openqa.selenium.JavascriptException)1 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)1 NoSuchElementException (org.openqa.selenium.NoSuchElementException)1 NoSuchSessionException (org.openqa.selenium.NoSuchSessionException)1 NoSuchWindowException (org.openqa.selenium.NoSuchWindowException)1 ScriptTimeoutException (org.openqa.selenium.ScriptTimeoutException)1 TimeoutException (org.openqa.selenium.TimeoutException)1 WebDriver (org.openqa.selenium.WebDriver)1 JsonException (org.openqa.selenium.json.JsonException)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1