List of usage examples for org.openqa.selenium WebElement getTagName
String getTagName();
From source file:io.tourniquet.pageobjects.PageObjectsInjectorTest.java
License:Apache License
@Test public void testAbstractGroup_SubmitForm() throws Exception { //prepare//from ww w . ja v a 2s. c o m new SeleniumContext(() -> driver).init(); WebElement element = mock(WebElement.class); when(driver.findElement(By.id("someForm"))).thenReturn(element); when(element.isDisplayed()).thenReturn(true); when(element.getTagName()).thenReturn("form"); GroupWithAbstractSubgroup group = new GroupWithAbstractSubgroup(); //act PageObjectsInjector.injectFields(group); //assert assertNotNull(group.subgroup); group.subgroup.submitForm(); verify(element).submit(); assertEquals("testpage", group.subgroup.toString()); }
From source file:javax.portlet.tck.driver.TCKLiferayTestDriver.java
License:Apache License
protected void click(WebElement wel) { boolean sennaJS = true; String url = null;//from w ww . j a v a 2 s.c o m if (wel != null) { String tagName = wel.getTagName(); if ("a".equals(tagName)) { url = wel.getAttribute("href"); sennaJS = !url.contains("v3headerportlettests") && !url.contains("v3portlethubtests"); } } if (sennaJS) { wel.click(); } else { driver.get(url); } }
From source file:javax.portlet.tck.driver.TCKSimpleTestDriver.java
License:Apache License
/** * Looks for a link or button that can be clicked for the TC and clicks it if found. * /* w w w .j av a 2 s . c om*/ * First looks for a test case setup link or button and clicks it if found. Then it * looks for a test case execution link and clicks it if found. * * @return web element list containing the test case results. * @throws Exception */ @SuppressWarnings("unused") protected List<WebElement> processClickable(List<WebElement> wels) throws Exception { String setupId = tcName + Constants.SETUP_ID; String actionId = tcName + Constants.CLICK_ID; String resultId = tcName + Constants.RESULT_ID; String detailId = tcName + Constants.DETAIL_ID; String asyncId = tcName + Constants.ASYNC_ID; String notreadyId = tcName + Constants.NOTREADY_ID; List<WebElement> tcels = null; for (WebElement wel : wels) { tcels = wel.findElements(By.id(setupId)); if (!tcels.isEmpty()) break; } debugLines.add(" Setup link found: " + ((tcels != null) && !tcels.isEmpty())); // If were dealing with async, make sure the JavaScript is initialized List<WebElement> acels = driver.findElements(By.id(asyncId)); debugLines.add(" Async elements found: " + ((acels != null) && !acels.isEmpty())); if (acels != null && !acels.isEmpty()) { WebDriverWait wdw = new WebDriverWait(driver, timeout); wdw.until(ExpectedConditions.invisibilityOfElementLocated(By.id(notreadyId))); debugLines.add(" Async elements are now ready."); } // Click setup link if found if ((tcels != null) && !tcels.isEmpty()) { WebElement wel = tcels.get(0); if (scroll) { JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; javascriptExecutor.executeScript( "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));", wel); } wel.click(); debugLines.add(" Clicked setup link."); WebDriverWait wdw = new WebDriverWait(driver, timeout); String expr = "//*[@id='" + resultId + "'] | //*[@id='" + actionId + "']"; debugLines.add(" xpath string: ===" + expr + "==="); wdw.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(expr))); wels = driver.findElements(By.name(tcName)); debugLines.add(" Found elements: " + (!wels.isEmpty())); List<WebElement> xels = driver.findElements(By.xpath(expr)); for (WebElement w : xels) { debugLines.add(" Element: " + w.getTagName() + ", id=" + w.getAttribute("id")); } } // Now click the action link, if present for (WebElement wel : wels) { tcels = wel.findElements(By.id(actionId)); if (!tcels.isEmpty()) break; } debugLines.add(" Clickable link found: " + ((tcels != null) && !tcels.isEmpty())); if (tcels != null && !tcels.isEmpty()) { WebElement wel = tcels.get(0); if (scroll) { JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; javascriptExecutor.executeScript( "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));", wel); } wel.click(); WebDriverWait wdw = new WebDriverWait(driver, timeout); wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id(resultId))); wels = driver.findElements(By.name(tcName)); if ((wels == null) || wels.isEmpty()) { throw new Exception("Test case " + tcName + " failed. No results after action link click."); } } return wels; }
From source file:javax.portlet.tck.driver.TCKTestDriver.java
License:Apache License
/** * Looks for a link or button that can be clicked for the TC and clicks it if found. * /*from ww w .ja va2 s. c om*/ * First looks for a test case setup link or button and clicks it if found. Then it * looks for a test case execution link and clicks it if found. * * @return web element list containing the test case results. * @throws Exception */ @SuppressWarnings("unused") protected List<WebElement> processClickable(List<WebElement> wels) throws Exception { String setupId = tcName + Constants.SETUP_ID; String actionId = tcName + Constants.CLICK_ID; String resultId = tcName + Constants.RESULT_ID; String detailId = tcName + Constants.DETAIL_ID; String asyncId = tcName + Constants.ASYNC_ID; String notreadyId = tcName + Constants.NOTREADY_ID; List<WebElement> tcels = null; for (WebElement wel : wels) { tcels = wel.findElements(By.id(setupId)); if (!tcels.isEmpty()) break; } debugLines.add(" Setup link found: " + ((tcels != null) && !tcels.isEmpty())); // If were dealing with async, make sure the JavaScript is initialized List<WebElement> acels = driver.findElements(By.id(asyncId)); debugLines.add(" Async elements found: " + ((acels != null) && !acels.isEmpty())); if (acels != null && !acels.isEmpty()) { WebDriverWait wdw = new WebDriverWait(driver, timeout); wdw.until(ExpectedConditions.invisibilityOfElementLocated(By.id(notreadyId))); debugLines.add(" Async elements are now ready."); } // Click setup link if found if ((tcels != null) && !tcels.isEmpty()) { WebElement wel = tcels.get(0); if (scroll) { JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; javascriptExecutor.executeScript( "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));", wel); } try { wel.click(); } catch (StaleElementReferenceException e) { System.out.println("setup link: " + e.getClass().getName() + " caught when trying to use WebElements found with " + tcName); wels = driver.findElements(By.name(tcName)); for (WebElement welly : wels) { tcels = welly.findElements(By.id(setupId)); if (!tcels.isEmpty()) break; } wel = tcels.get(0); wel.click(); } debugLines.add(" Clicked setup link."); WebDriverWait wdw = new WebDriverWait(driver, timeout); String expr = "//*[@id='" + resultId + "'] | //*[@id='" + actionId + "']"; debugLines.add(" xpath string: ===" + expr + "==="); wdw.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(expr))); wels = driver.findElements(By.name(tcName)); debugLines.add(" Found elements: " + (!wels.isEmpty())); List<WebElement> xels = driver.findElements(By.xpath(expr)); for (WebElement w : xels) { debugLines.add(" Element: " + w.getTagName() + ", id=" + w.getAttribute("id")); } } // Now click the action link, if present for (WebElement wel : wels) { tcels = wel.findElements(By.id(actionId)); if (!tcels.isEmpty()) break; } debugLines.add(" Clickable link found: " + ((tcels != null) && !tcels.isEmpty())); if (tcels != null && !tcels.isEmpty()) { WebElement wel = tcels.get(0); if (scroll) { JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; javascriptExecutor.executeScript( "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));", wel); } try { wel.click(); } catch (StaleElementReferenceException e) { System.out.println("action link: " + e.getClass().getName() + " caught when trying to use WebElements found with " + tcName); wels = driver.findElements(By.name(tcName)); for (WebElement welly : wels) { tcels = welly.findElements(By.id(actionId)); if (!tcels.isEmpty()) break; } wel = tcels.get(0); wel.click(); } WebDriverWait wdw = new WebDriverWait(driver, timeout); wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id(resultId))); wels = driver.findElements(By.name(tcName)); if ((wels == null) || wels.isEmpty()) { throw new Exception("Test case " + tcName + " failed. No results after action link click."); } } return wels; }
From source file:jhc.redsniff.webdriver.BasicFindersTests.java
License:Apache License
@Test public void findReturnsElementsAsCollectionOfWebElementsforMFinder() { Collection<WebElement> dropDowns = t.find(dropDown().that(hasName("a_drop-down"))); assertThat(dropDowns, hasSize(2));//from w w w.j ava 2 s . c o m Iterator<WebElement> iterator = dropDowns.iterator(); WebElement dropDown1 = iterator.next(); WebElement dropDown2 = iterator.next(); assertEquals("select", dropDown1.getTagName()); assertEquals("select", dropDown2.getTagName()); assertEquals("OPTION A", dropDown1.getText()); assertEquals("OPTION AA", dropDown2.getText()); }
From source file:jhc.redsniff.webdriver.BasicFindersTests.java
License:Apache License
@Test public void findReturnsSingleElementForOrdinalSFinder() { WebElement dropDown1 = t.find(first(dropDown().that(hasName("a_drop-down")))); assertEquals("select", dropDown1.getTagName()); assertEquals("OPTION A", dropDown1.getText()); }
From source file:jhc.redsniff.webdriver.BasicFindersTests.java
License:Apache License
@Test public void findReturnsSingleElementForOnlySFinder() { WebElement dropDown1 = t.find(only(dropDown().that(hasName("another_drop-down_with_3_options")))); assertEquals("select", dropDown1.getTagName()); assertEquals("OPTION 10 OPTION 20 OPTION 30", dropDown1.getText()); }
From source file:jhc.redsniff.webdriver.describe.WebElementDescribaliser.java
License:Apache License
@Override public SelfDescribing describable(final WebElement element) { return new SelfDescribing() { @Override/*from w w w. j av a 2 s .com*/ public void describeTo(Description description) { describeElementTo(element, description); } private void describeElementTo(WebElement element, Description description) { description.appendText("<" + element.getTagName() + ">"); describeElementAttribute("class", element, description); describeElementAttribute("name", element, description); describeElementAttribute("id", element, description); //TODO -: describeTextOfElement(element, description); describeChildElements(element, description); } //TODO - this is too costly at the moment to be worth it private void describeTextOfElement(WebElement element, Description description) { String elementText = element.getText(); description.appendText(" " + (elementText == null ? "" : elementText)); } private void describeElementAttribute(String attribute, WebElement element, Description description) { String attributeValue = element.getAttribute(attribute); if (attributeValue != null && !attributeValue.isEmpty()) description.appendText(" (" + attribute + ":" + attributeValue + ")"); } private void describeChildElements(WebElement element, Description description) { //TODO - would be nice to output child elements - doesn't work not sure why //List<WebElement> childElements = element.findElements(By.xpath("*")); //childElements.size(); // //if(!childElements.isEmpty()){ // description.appendText("\n\t<<"); // for(WebElement child:childElements) // describeElementTo(child,description); // description.appendText(">>\n"); //} } }; }
From source file:jhc.redsniff.webdriver.matchers.TagNameMatcher.java
License:Apache License
@Override public Matcher<WebElement> getWrappedMatcher(Matcher<String> stringMatcher) { return new TypeSafeDiagnosingMatcher<WebElement>() { @Override/*from w w w . j a va 2s . com*/ public void describeTo(Description description) { description.appendText("has tagname \"" + tagName + "\""); } @Override protected boolean matchesSafely(WebElement actualItem, Description mismatchDescription) { return matchAndDiagnose(isString(tagName), actualItem.getTagName(), mismatchDescription, "tagName "); } }; }
From source file:jj.webdriver.panel.PanelBase.java
License:Apache License
String read(By by) { log("read", by); WebElement element = find(by); // does this belong here or should // there be separate generation patterns? switch (element.getTagName()) { case "input": return element.getAttribute("value"); default://from ww w . j a v a 2 s .co m return element.getText(); } }