use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class HtmlPasswordInputTest method maxLengthValidationInvalid.
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts({ "abcde", "§§URL§§second/", "2" })
public void maxLengthValidationInvalid() throws Exception {
final String html = "<!DOCTYPE html>\n" + "<html><head></head>\n" + "<body>\n" + " <form id='myForm' action='" + URL_SECOND + "' method='" + HttpMethod.POST + "'>\n" + " <input type='password' maxlength='5' id='foo'>\n" + " <button id='myButton' type='submit'>Submit</button>\n" + " </form>\n" + "</body></html>";
final String secondContent = "<html><head><title>second</title></head><body>\n" + " <p>hello world</p>\n" + "</body></html>";
getMockWebConnection().setResponse(URL_SECOND, secondContent);
expandExpectedAlertsVariables(URL_FIRST);
final WebDriver driver = loadPage2(html, URL_FIRST);
final WebElement foo = driver.findElement(By.id("foo"));
foo.sendKeys("abcdefghi");
assertEquals(getExpectedAlerts()[0], foo.getAttribute("value"));
// valid data
driver.findElement(By.id("myButton")).click();
assertEquals(getExpectedAlerts()[1], getMockWebConnection().getLastWebRequest().getUrl());
assertEquals(Integer.parseInt(getExpectedAlerts()[2]), getMockWebConnection().getRequestCount());
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class HtmlPasswordInputTest method getVisibleText.
/**
* Verifies getVisibleText().
* @throws Exception if the test fails
*/
@Test
@Alerts("")
public void getVisibleText() throws Exception {
final String htmlContent = "<html>\n" + "<head></head>\n" + "<body>\n" + "<form id='form1'>\n" + " <input type='password' name='tester' id='tester' value='bla'>\n" + "</form>\n" + "</body></html>";
final WebDriver driver = loadPage2(htmlContent);
final String text = driver.findElement(By.id("tester")).getText();
assertEquals(getExpectedAlerts()[0], text);
if (driver instanceof HtmlUnitDriver) {
final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
assertEquals(getExpectedAlerts()[0], page.getBody().getVisibleText());
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class HtmlOrderedList2Test method getVisibleText.
/**
* Verifies getVisibleText().
* @throws Exception if the test fails
*/
@Test
@Alerts("first item\nsecond item\nsomething without li node\nthird item")
public void getVisibleText() throws Exception {
final String htmlContent = "<html>\n" + "<head></head>\n" + "<body>\n" + " <ol id='tester'>\n" + " <li>first item</li>\n" + " <li>second item</li>\n" + "something without li node\n" + " <li>third item</li>\n" + " </ol>\n" + "</body></html>";
final WebDriver driver = loadPage2(htmlContent);
final String text = driver.findElement(By.id("tester")).getText();
assertEquals(getExpectedAlerts()[0], text);
if (driver instanceof HtmlUnitDriver) {
final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
assertEquals(getExpectedAlerts()[0], page.getBody().getVisibleText());
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class HtmlPage2Test method save.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("Hello there")
public void save() throws Exception {
final String html = "<html><head><script src='" + URL_SECOND + "'>\n</script></head></html>";
final String js = "alert('Hello there')";
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
webConnection.setResponse(URL_SECOND, js);
webClient.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = webClient.getPage(URL_FIRST);
assertEquals(getExpectedAlerts(), collectedAlerts);
final HtmlScript sript = page.getFirstByXPath("//script");
assertEquals(URL_SECOND.toString(), sript.getSrcAttribute());
final File tmpFolder = tmpFolderProvider_.newFolder("hu");
final File file = new File(tmpFolder, "hu_HtmlPageTest_save.html");
page.save(file);
assertTrue(file.exists());
assertTrue(file.isFile());
final String content = FileUtils.readFileToString(file, ISO_8859_1);
assertFalse(content.contains("<script"));
assertEquals(URL_SECOND.toString(), sript.getSrcAttribute());
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class HtmlPage3Test method shouldBeAbleToFindElementByXPathInXmlDocument.
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "error", CHROME = "Something", EDGE = "Something")
@NotYetImplemented({ IE, FF, FF_ESR })
public void shouldBeAbleToFindElementByXPathInXmlDocument() throws Exception {
final String html = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<html xmlns='http://www.w3.org/1999/xhtml'\n" + " xmlns:svg='http://www.w3.org/2000/svg'\n" + " xmlns:xlink='http://www.w3.org/1999/xlink'>\n" + "<body>\n" + " <svg:svg id='chart_container' height='220' width='400'>\n" + " <svg:text y='16' x='200' text-anchor='middle'>Something</svg:text>\n" + " </svg:svg>\n" + "</body>\n" + "</html>\n";
final WebDriver driver = loadPage2(html, URL_FIRST, "application/xhtml+xml", ISO_8859_1, null);
String actual;
try {
final WebElement element = driver.findElement(By.xpath("//svg:svg//svg:text"));
actual = element.getText();
} catch (final NoSuchElementException e) {
actual = "error";
}
assertEquals(getExpectedAlerts()[0], actual);
}
Aggregations