use of org.openqa.selenium.UnhandledAlertException in project selenium_java by sergueik.
the class XMLHttpRequestAsyncTest method sendJSONArgBasicTest.
@Test(enabled = false)
public // asynchronous script timeout: result was not received in 30 seconds(..)
void sendJSONArgBasicTest() {
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);
String inlineScript = "var callback = function(data){/* console.log(data); */ return data;}\n" + "var option_data = '{\"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" + " callback(http.responseText);\n" + " };\n" + "};\n" + "http.send(params);\n";
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);
}
use of org.openqa.selenium.UnhandledAlertException in project selenium_java by sergueik.
the class XMLHttpRequestAsyncTest method sendJSONArgTest.
@Test(enabled = false)
public void sendJSONArgTest() {
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(1000);
// Injecting a XMLHttpRequest, passing JSON object with the data and waiting
// for the result
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" + " 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);
sleep(10000);
// 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(1000);
}
use of org.openqa.selenium.UnhandledAlertException in project carina by qaprosoft.
the class DriverHelper method openURL.
/**
* Open URL.
*
* @param url
* to open.
* @param timeout
* long
*/
public void openURL(String url, long timeout) {
final String decryptedURL = getEnvArgURL(cryptoTool.decryptByPattern(url, CRYPTO_PATTERN));
this.pageURL = decryptedURL;
WebDriver drv = getDriver();
DriverListener.setMessages(Messager.OPENED_URL.getMessage(url), Messager.NOT_OPENED_URL.getMessage(url));
// [VD] there is no sense to use fluent wait here as selenium just don't return something until page is ready!
// explicitly limit time for the openURL operation
Future<?> future = Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
public Void call() {
try {
Messager.OPENING_URL.info(url);
drv.get(decryptedURL);
} catch (UnhandledAlertException e) {
drv.switchTo().alert().accept();
}
return null;
}
});
try {
LOGGER.debug("starting driver.get call...");
future.get(timeout, TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException e) {
String message = "Unable to open url during " + timeout + "sec!";
LOGGER.error(message);
Assert.fail(message, e);
} catch (InterruptedException e) {
String message = "Unable to open url during " + timeout + "sec!";
LOGGER.error(message);
Assert.fail(message, e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
String message = "ExecutionException error on open url: " + e.getMessage();
LOGGER.error(message);
Assert.fail(message, e);
} catch (Exception e) {
String message = "Undefined error on open url detected: " + e.getMessage();
LOGGER.error(message);
Assert.fail(message, e);
} finally {
LOGGER.debug("finished driver.get call.");
}
}
use of org.openqa.selenium.UnhandledAlertException in project selenium_java by sergueik.
the class XMLHttpRequestAsyncTest method sendJSONArgTrackingTest.
@Test(enabled = false)
public void sendJSONArgTrackingTest() {
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);
// Injecting a XMLHttpRequest, passing JSON object with the data and waiting
// for the result
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" + "var tempAlert = function (msg,duration) {\n" + "var el = document.createElement('div');\n" + "el.setAttribute('style','position:absolute;top:40%;left:20%;background-color:white;');\n" + "el.innerHTML = msg;\n" + "setTimeout(function(){\n" + "el.parentNode.removeChild(el);\n" + "},duration);\n" + "document.body.appendChild(el);\n" + "};\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" + " /* tempAlert('close',100); */ \n" + " 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);
}
use of org.openqa.selenium.UnhandledAlertException in project selenium_java by sergueik.
the class XMLHttpRequestAsyncTest method sendArgTest.
@Test(enabled = true)
public void sendArgTest() {
// Arrange
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);
// Injecting a XMLHttpRequest and waiting for the result
// https://stackoverflow.com/questions/13452822/webdriver-executeasyncscript-vs-executescript
// https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html#executeAsyncScript-java.lang.String-java.lang.Object...-
String inlineScript = "var callback = arguments[arguments.length - 1];\n" + "var option_index = (arguments.length > 1 )? arguments[arguments.length - 2]:1;\n" + "var http = new XMLHttpRequest();\n" + "var url = 'get_state.php';\n" + "var params = 'country_id=' + 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" + " callback(http.responseText);\n" + " };\n" + "};\n" + "http.send(params);";
try {
response = (String) js.executeAsyncScript(inlineScript, country_index);
} catch (UnhandledAlertException e) {
System.err.println("Error executing XMLHttpRequest");
}
System.out.println("sendXMLHTTPRequestArgTest response: " + response);
// Assert that returned cities are related with chosen country
// 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