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);
}
Aggregations