org.openqa.selenium.By.ById Java Examples

The following examples show how to use org.openqa.selenium.By.ById. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void submitParams(final String controls) throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
        + "<form id='form1' method='post'>\n"
        + controls
        + "</form></body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ById("mySubmit")).click();

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    Collections.sort(requestedParams, Comparator.comparing(NameValuePair::getName));

    assertEquals(getExpectedAlerts().length, requestedParams.size());

    for (int i = 0; i < requestedParams.size(); i++) {
        assertEquals(getExpectedAlerts()[i],
                requestedParams.get(i).getName() + '#' + requestedParams.get(i).getValue());
    }
}
 
Example #2
Source File: ElementFactory.java    From aquality-selenium-java with Apache License 2.0 5 votes vote down vote up
private static Map<Class<? extends By>, String> getLocatorToXPathTemplateMap() {
    Map<Class<? extends By>, String> locatorToXPathTemplateMap = new HashMap<>();
    locatorToXPathTemplateMap.put(ByClassName.class, "//*[contains(@class,'%s')]");
    locatorToXPathTemplateMap.put(ByName.class, "//*[@name='%s']");
    locatorToXPathTemplateMap.put(ById.class, "//*[@id='%s']");
    locatorToXPathTemplateMap.put(ByIdOrName.class, "//*[@id='%1$s' or @name='%1$s']");
    return locatorToXPathTemplateMap;
}
 
Example #3
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"§§URL§§?par%F6m=Hello+G%FCnter", "par\u00F6m", "Hello G\u00FCnter"})
public void encodingSubmit() throws Exception {
    stopWebServers();
    final String html =
        "<html>\n"
        + "<head>\n"
        + "  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <form>\n"
        + "    <input name='par\u00F6m' value='Hello G\u00FCnter'>\n"
        + "    <input id='mySubmit' type='submit' value='Submit'>\n"
        + "  </form>\n"
        + "</body></html>";

    expandExpectedAlertsVariables(URL_FIRST);
    final WebDriver driver = loadPage2(html, URL_FIRST, "text/html;charset=ISO-8859-1", ISO_8859_1, ISO_8859_1);
    driver.findElement(new ById("mySubmit")).click();

    assertEquals(getExpectedAlerts()[0], driver.getCurrentUrl());

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    assertEquals(1, requestedParams.size());
    assertEquals(getExpectedAlerts()[1], requestedParams.get(0).getName());
    assertEquals(getExpectedAlerts()[2], requestedParams.get(0).getValue());
}
 
Example #4
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the 'Origin' and 'Referer' HTTP header.
 * @throws Exception on test failure
 */
@Test
@Alerts({"null", "§§URL§§path?query"})
public void originRefererHeaderGet() throws Exception {
    final String firstHtml
        = "<html>\n"
        + "<head></head>\n"
        + "<body>\n"
        + "  <form method='get' action='" + URL_SECOND + "'>\n"
        + "    <input id='mySubmit' type='submit' value='Submit'>\n"
        + "  </form>\n"
        + "</body>\n"
        + "</html>";
    final String secondHtml = "<html><body></body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    final URL requestUrl = new URL(URL_FIRST, "/path?query");
    webConnection.setResponse(URL_SECOND, secondHtml);

    expandExpectedAlertsVariables(URL_FIRST);
    final WebDriver driver = loadPage2(firstHtml, requestUrl);

    driver.findElement(new ById("mySubmit")).click();

    final Map<String, String> lastAdditionalHeaders = webConnection.getLastAdditionalHeaders();
    assertEquals(getExpectedAlerts()[0], "" + lastAdditionalHeaders.get(HttpHeader.ORIGIN));
    assertEquals(getExpectedAlerts()[1], "" + lastAdditionalHeaders.get(HttpHeader.REFERER));
}
 
Example #5
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the 'Origin' HTTP header.
 * @throws Exception on test failure
 */
@Test
@Alerts(DEFAULT = {"null", "§§URL§§/path?query"},
        CHROME = {"§§URL§§", "§§URL§§/path?query"},
        FF = {"§§URL§§", "§§URL§§/path?query"})
public void originRefererHeaderPost() throws Exception {
    final String firstHtml
        = "<html>\n"
        + "<head></head>\n"
        + "<body>\n"
        + "  <form method='post' action='" + URL_SECOND + "'>\n"
        + "    <input id='mySubmit' type='submit' value='Submit'>\n"
        + "  </form>\n"
        + "</body>\n"
        + "</html>";
    final String secondHtml = "<html><body></body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    final URL requestUrl = new URL(URL_FIRST, "/path?query");
    webConnection.setResponse(URL_SECOND, secondHtml);

    String url = URL_FIRST.toExternalForm();
    url = url.substring(0, url.length() - 1);
    expandExpectedAlertsVariables(url);
    final WebDriver driver = loadPage2(firstHtml, requestUrl);

    driver.findElement(new ById("mySubmit")).click();

    final Map<String, String> lastAdditionalHeaders = webConnection.getLastAdditionalHeaders();
    assertEquals(getExpectedAlerts()[0], "" + lastAdditionalHeaders.get(HttpHeader.ORIGIN));
    assertEquals(getExpectedAlerts()[1], "" + lastAdditionalHeaders.get(HttpHeader.REFERER));
}
 
Example #6
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "textParam#textValue",
        IE = {})
public void submitUsingFormAttributeElementsDeeplyNested() throws Exception {
    final String html =
        "<!DOCTYPE html>\n"
        + "<html>\n"
        + "<head>\n"
        + "</head>\n"
        + "<body>\n"

        + "  <form id='formId'>\n"
        + "  </form>\n"

        + "  <div><div><div><div>\n"
        + "    <input form='formId' type='text' name='textParam' value='textValue'>\n"
        + "  </div></div></div></div>\n"

        + "  <input form='formId' id='mySubmit' type='submit' value='Submit'>\n"

        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ById("mySubmit")).click();

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    Collections.sort(requestedParams, Comparator.comparing(NameValuePair::getName));

    assertEquals(getExpectedAlerts().length, requestedParams.size());

    for (int i = 0; i < requestedParams.size(); i++) {
        assertEquals(getExpectedAlerts()[i],
                requestedParams.get(i).getName() + '#' + requestedParams.get(i).getValue());
    }
}
 
Example #7
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "hiddenParam#form1",
        IE = "hiddenParam#form2")
public void submitFromInsideAnother() throws Exception {
    final String html =
        "<!DOCTYPE html>\n"
        + "<html>\n"
        + "<head>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <form id='formId'>\n"
        + "    <input type='hidden' name='hiddenParam' value='form1'>\n"
        + "  </form>\n"

        + "  <form id='formId2'>\n"
        + "    <input type='hidden' name='hiddenParam' value='form2'>\n"
        + "    <input form='formId' id='mySubmit' type='submit' value='Submit'>\n"
        + "  </form>\n"

        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ById("mySubmit")).click();

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    Collections.sort(requestedParams, Comparator.comparing(NameValuePair::getName));

    assertEquals(getExpectedAlerts().length, requestedParams.size());

    for (int i = 0; i < requestedParams.size(); i++) {
        assertEquals(getExpectedAlerts()[i],
                requestedParams.get(i).getName() + '#' + requestedParams.get(i).getValue());
    }
}
 
Example #8
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {},
        IE = "hiddenParam#form2")
public void submitFromInsideAnotherInvalidFormRef() throws Exception {
    final String html =
        "<!DOCTYPE html>\n"
        + "<html>\n"
        + "<head>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <form id='formId'>\n"
        + "    <input type='hidden' name='hiddenParam' value='form1'>\n"
        + "  </form>\n"

        + "  <form id='formId2'>\n"
        + "    <input type='hidden' name='hiddenParam' value='form2'>\n"
        + "    <input form='formIdInvalid' id='mySubmit' type='submit' value='Submit'>\n"
        + "  </form>\n"

        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ById("mySubmit")).click();

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    Collections.sort(requestedParams, Comparator.comparing(NameValuePair::getName));

    assertEquals(getExpectedAlerts().length, requestedParams.size());

    for (int i = 0; i < requestedParams.size(); i++) {
        assertEquals(getExpectedAlerts()[i],
                requestedParams.get(i).getName() + '#' + requestedParams.get(i).getValue());
    }
}
 
Example #9
Source File: LocatorUtilTest.java    From qaf with MIT License 5 votes vote down vote up
@DataProvider(name = "locatorDP")
public static Iterator<Object[]> testData() {
	ArrayList<Object[]> data = new ArrayList<Object[]>();

	data.add(new Object[]{"id=eleId", ById.class});
	data.add(new Object[]{"name=eleName", ByName.class});
	data.add(new Object[]{"css=#eleId.className", ByCssSelector.class});
	data.add(new Object[]{"tagName=div", ByTagName.class});
	data.add(new Object[]{"link=Link Text", ByLinkText.class});
	data.add(new Object[]{"partialLink=Link Text", ByPartialLinkText.class});
	data.add(new Object[]{"['css=#qa','name=eleName']", ByAny.class});

	// self descriptive
	data.add(new Object[]{"{'locator' : 'id=eleId'; 'desc' : 'locate element by id'}",
			ById.class});
	data.add(new Object[]{
			"{'locator' : 'name=eleName'; 'desc' : 'locate element by name'}",
			ByName.class});
	data.add(new Object[]{
			"{'locator' : 'css=#eleId.className'; 'desc' : 'locate element by css'}",
			ByCssSelector.class});

	data.add(new Object[]{
			"{'locator' : ['css=#qa','name=eleName']; 'desc' : 'locate element by css'}",
			ByAny.class});

	data.add(new Object[]{"xpath=//*[text()='Albany-Rensselaer, NY (ALB)']",
			ByXPath.class});
	return data.iterator();
}
 
Example #10
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {"radioParam2#radioValue2", "selectParam#selectValue", "textParam#textValue",
    "textareaParam#textarea value"},
        IE = {})
public void submitUsingFormAttribute() throws Exception {
    final String html =
        "<!DOCTYPE html>\n"
        + "<html>\n"
        + "<head>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <form id='formId'>\n"
        + "  </form>\n"

        + "  <input form='formId' type='text' name='textParam' value='textValue'>\n"

        + "  <fieldset form='formId'>\n"
        + "    <input type='hidden' name='hiddenParam' value='hiddenValue'>\n"
        + "    <input type='text' name='fieldsetTextParam' value='fieldsetTextValue'>\n"
        + "  </fieldset>\n"

        + "  <label for='radioId' form='formId'>Male</label>\n"
        + "  <input id='radioId' type='radio' name='radioParam' value='radioValue'>\n"

        + "  <input form='formId' type='radio' name='radioParam2' value='radioValue2' checked='checked'>\n"

        + "  <select form='formId' name='selectParam'>\n"
        + "    <option value='selectValue' selected='selected'>selected</option>\n"
        + "  </select>\n"

        + "  <textarea form='formId' name='textareaParam'>textarea value</textarea>\n"

        + "  <input form='formId' id='mySubmit' type='submit' value='Submit'>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ById("mySubmit")).click();

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    Collections.sort(requestedParams, Comparator.comparing(NameValuePair::getName));

    assertEquals(getExpectedAlerts().length, requestedParams.size());

    for (int i = 0; i < requestedParams.size(); i++) {
        assertEquals(getExpectedAlerts()[i],
                requestedParams.get(i).getName() + '#' + requestedParams.get(i).getValue());
    }
}
 
Example #11
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {"radioParam2#radioValue2", "selectParam#selectValue", "textParam#textValue",
    "textareaParam#textarea value"},
        IE = {})
public void submitUsingFormAttributeElementsDeclaredBeforeForm() throws Exception {
    final String html =
        "<!DOCTYPE html>\n"
        + "<html>\n"
        + "<head>\n"
        + "</head>\n"
        + "<body>\n"

        + "  <input form='formId' type='text' name='textParam' value='textValue'>\n"

        + "  <fieldset form='formId'>\n"
        + "    <input type='hidden' name='hiddenParam' value='hiddenValue'>\n"
        + "    <input type='text' name='fieldsetTextParam' value='fieldsetTextValue'>\n"
        + "  </fieldset>\n"

        + "  <label for='radioId' form='formId'>Male</label>\n"
        + "  <input id='radioId' type='radio' name='radioParam' value='radioValue'>\n"

        + "  <input form='formId' type='radio' name='radioParam2' value='radioValue2' checked='checked'>\n"

        + "  <select form='formId' name='selectParam'>\n"
        + "    <option value='selectValue' selected='selected'>selected</option>\n"
        + "  </select>\n"

        + "  <textarea form='formId' name='textareaParam'>textarea value</textarea>\n"

        + "  <input form='formId' id='mySubmit' type='submit' value='Submit'>\n"

        + "  <form id='formId'>\n"
        + "  </form>\n"

        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ById("mySubmit")).click();

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest().getRequestParameters();
    Collections.sort(requestedParams, Comparator.comparing(NameValuePair::getName));

    assertEquals(getExpectedAlerts().length, requestedParams.size());

    for (int i = 0; i < requestedParams.size(); i++) {
        assertEquals(getExpectedAlerts()[i],
                requestedParams.get(i).getName() + '#' + requestedParams.get(i).getValue());
    }
}