Example usage for org.openqa.selenium By name

List of usage examples for org.openqa.selenium By name

Introduction

In this page you can find the example usage for org.openqa.selenium By name.

Prototype

public static By name(String name) 

Source Link

Usage

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w ww .java 2 s  . c  o  m*/
 */
@Test
public void contentTypeHeader() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeHeaderUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get("http://localhost:" + PORT + "/upload1");
    String path = getClass().getClassLoader().getResource("realm.properties").toExternalForm();
    if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) {
        path = path.substring(path.indexOf('/') + 1).replace('/', '\\');
    }
    driver.findElement(By.name("myInput")).sendKeys(path);
    driver.findElement(By.id("mySubmit")).click();
    final String source = driver.getPageSource();
    assertTrue(source.contains("CONTENT_TYPE:"));
    assertFalse(source.contains("charset"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//from   w w  w  . ja va2 s  .  c o m
 */
@Test
@Alerts(DEFAULT = "Content-Disposition: form-data; name=\"myInput\"; filename=\"realm.properties\"", IE = "Content-Disposition: form-data; name=\"myInput\";"
        + " filename=\".*test-classes[\\\\/]realm\\.properties\"")
public void realFile() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", PrintRequestServlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get("http://localhost:" + PORT + "/upload1");
    String path = getClass().getClassLoader().getResource("realm.properties").toExternalForm();
    if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) {
        path = path.substring(path.indexOf('/') + 1).replace('/', '\\');
    }
    driver.findElement(By.name("myInput")).sendKeys(path);
    driver.findElement(By.id("mySubmit")).click();

    String pageSource = driver.getPageSource();
    // hack for selenium
    int count = 0;
    while (count < 100 && StringUtils.isEmpty(pageSource)) {
        pageSource = driver.getPageSource();
        count++;
    }

    final Matcher matcher = Pattern.compile(getExpectedAlerts()[0]).matcher(pageSource);
    assertTrue(pageSource, matcher.find());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

License:Apache License

private void contentType(final String extension) throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "upload1");

    final File tmpFile = File.createTempFile("htmlunit-test", "." + extension);
    try {/*from  w  ww  .j  a  va2  s  .c o m*/
        String path = tmpFile.getAbsolutePath();
        if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) {
            path = path.substring(path.indexOf('/') + 1).replace('/', '\\');
        }
        driver.findElement(By.name("myInput")).sendKeys(path);
        driver.findElement(By.id("mySubmit")).click();
    } finally {
        tmpFile.delete();
    }

    final String pageSource = driver.getPageSource();
    assertTrue(pageSource, pageSource.contains(getExpectedAlerts()[0]));
    assertFalse(pageSource, pageSource.contains(getExpectedAlerts()[1]));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from   ww w .  j  a  v a  2 s.  c  om
 */
@Test
public void contentTypeHeader() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeHeaderUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "upload1");
    String path = getClass().getClassLoader().getResource("realm.properties").toExternalForm();
    if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) {
        path = path.substring(path.indexOf('/') + 1).replace('/', '\\');
    }
    driver.findElement(By.name("myInput")).sendKeys(path);
    driver.findElement(By.id("mySubmit")).click();
    final String source = driver.getPageSource();
    assertTrue(source.contains("CONTENT_TYPE:"));
    assertFalse(source.contains("charset"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

License:Apache License

/**
 * @throws Exception if an error occurs// w  ww. j av a 2s  .  co m
 */
@Test
@Alerts(DEFAULT = "Content-Disposition: form-data; name=\"myInput\"; filename=\"realm.properties\"", IE = "Content-Disposition: form-data; name=\"myInput\";"
        + " filename=\".*test-classes[\\\\/]realm\\.properties\"")
public void realFile() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", PrintRequestServlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "upload1");
    String path = getClass().getClassLoader().getResource("realm.properties").toExternalForm();
    if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) {
        path = path.substring(path.indexOf('/') + 1).replace('/', '\\');
    }
    driver.findElement(By.name("myInput")).sendKeys(path);
    driver.findElement(By.id("mySubmit")).click();

    String pageSource = driver.getPageSource();
    // hack for selenium
    int count = 0;
    while (count < 100 && StringUtils.isEmpty(pageSource)) {
        pageSource = driver.getPageSource();
        count++;
    }

    final Matcher matcher = Pattern.compile(getExpectedAlerts()[0]).matcher(pageSource);
    assertTrue(pageSource, matcher.find());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlImageInput2Test.java

License:Apache License

/**
 * @throws Exception if the test fails/*from   w  w w . j av a  2s. co m*/
 */
@Test
@Alerts(DEFAULT = "URL?button.x=0&button.y=0", CHROME = "URL?button.x=9&button.y=7&button=foo", IE = "URL?button.x=14&button.y=15")
@NotYetImplemented({ CHROME, IE })
public void click_NoPosition() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<input type='image' name='aButton' value='foo'/>\n"
            + "<input type='image' name='button' value='foo'/>\n"
            + "<input type='image' name='anotherButton' value='foo'/>\n" + "</form></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.name("button")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlImageInput2Test.java

License:Apache License

/**
 * @throws Exception if the test fails/*from   w ww  . ja  v a2s  .c  om*/
 */
@Test
@Alerts(DEFAULT = "URL?button.x=0&button.y=0", CHROME = "URL?button.x=22&button.y=7", IE = "URL?button.x=14&button.y=15")
@NotYetImplemented({ CHROME, IE })
public void click_NoPosition_NoValue() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<input type='image' name='button'>\n" + "</form></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.name("button")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlImageInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from  www  . j av  a2s .  co  m*/
 */
@Test
@Alerts(DEFAULT = "URL?button.x=0&button.y=0", CHROME = "URL?button.x=9&button.y=7&button=foo", IE = "URL?button.x=14&button.y=15")
@NotYetImplemented({ CHROME, IE })
public void click_NoPosition() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='image' name='aButton' value='foo'/>\n"
            + "  <input type='image' name='button' value='foo'/>\n"
            + "  <input type='image' name='anotherButton' value='foo'/>\n" + "</form></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.name("button")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlImageInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   ww  w.  j a  va2s .c  o m
 */
@Test
@Alerts(DEFAULT = "URL?button.x=0&button.y=0", CHROME = "URL?button.x=22&button.y=7", IE = "URL?button.x=14&button.y=15")
@NotYetImplemented({ CHROME, IE })
public void click_NoPosition_NoValue() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='image' name='button'>\n" + "</form></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.name("button")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlPage3Test.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w  ww .j  ava2  s.co  m
 */
@Test
public void getInputByName() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "<p>hello world</p>\n"
            + "<form id='form1' action='/formSubmit' method='post'>\n"
            + "  <input type='text' NAME='textInput1' value='textInput1'/>\n"
            + "  <input type='text' name='textInput2' value='textInput2'/>\n"
            + "  <input type='hidden' name='hidden1' value='hidden1'/>\n"
            + "  <input type='submit' name='submitInput1' value='push me'/>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final WebElement form = driver.findElement(By.id("form1"));
    final WebElement input = form.findElement(By.name("textInput1"));
    assertEquals("name", "textInput1", input.getAttribute("name"));

    assertEquals("value", "textInput1", input.getAttribute("value"));
    assertEquals("type", "text", input.getAttribute("type"));
}