@Override public void checkCheckbox(String checkBoxName, String value) { HtmlCheckBoxInput cb = getCheckbox(checkBoxName, value); if (!cb.isChecked()) { try { cb.click(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("checkCheckbox failed", e); } } }
@Override public void uncheckCheckbox(String checkBoxName, String value) { HtmlCheckBoxInput cb = getCheckbox(checkBoxName, value); if (cb.isChecked()) { try { cb.click(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("uncheckCheckbox failed", e); } } }
/** * Deselect a specified checkbox. If the checkbox is already unchecked then the checkbox will stay unchecked. * * @param checkBoxName name of checkbox to be deselected. */ @Override public void uncheckCheckbox(String checkBoxName) { HtmlCheckBoxInput cb = getCheckbox(checkBoxName); if (cb.isChecked()) { try { cb.click(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("checkCheckbox failed", e); } } }
/** * Select a specified checkbox. If the checkbox is already checked then the checkbox will stay checked. * * @param checkBoxName name of checkbox to be deselected. */ @Override public void checkCheckbox(String checkBoxName) { HtmlCheckBoxInput cb = getCheckbox(checkBoxName); if (!cb.isChecked()) { try { cb.click(); } catch (IOException e) { throw new RuntimeException("checkCheckbox failed", e); } } }
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { // disable caching webClient.getCache().setMaxSize(0); // Get the first page final HtmlPage page1 = webClient.getPage(url); final HtmlForm form = page1.getFormByName(formName); final HtmlTextInput taxCodeTextField = form.getInputByName(taxCodeTextFieldName); HtmlCheckBoxInput checkboxInput = form.getInputByName(checkboxInputName); taxCodeTextField.type(taxCode); checkboxInput.click(); //wait a little Thread.sleep(2000); //get the main page HtmlPage page2 = (HtmlPage) webClient.getTopLevelWindows().get(0).getEnclosedPage(); HtmlSubmitInput confirmButton = page2.getFormByName(formName).getInputByName(confirmButtonName); final HtmlPage page3 = confirmButton.click(); System.out.println(page3.asText()); }
public boolean toggle() { assertElementNotStale(); try { if (element instanceof HtmlCheckBoxInput) { ((HtmlCheckBoxInput) element).click(); return isSelected(); } if (element instanceof HtmlOption) { HtmlOption option = (HtmlOption) element; HtmlSelect select = option.getEnclosingSelect(); if (select.isMultipleSelectEnabled()) { option.setSelected(!option.isSelected()); return isSelected(); } } throw new UnsupportedOperationException("You may only toggle checkboxes or options in a select which allows multiple selections: " + getTagName()); } catch (IOException e) { throw new WebDriverException("Unexpected exception: " + e); } }