Java Code Examples for org.openqa.selenium.WebElement.getText()

The following are Jave code examples for showing how to use getText() of the org.openqa.selenium.WebElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ats-framework   File: HiddenHtmlSingleSelectList.java   View Source Code Vote up 6 votes
/**
 * @return the single selection value
 */
@Override
@PublicAtsApi
public String getValue() {

    new HiddenHtmlElementState(this).waitToBecomeExisting();

    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement element : optionElements) {
        if (element.isSelected()) {
            return element.getText();
        }
    }
    throw new SeleniumOperationException("There is no selected 'option' in " + this.toString());
}
 
Example 2
Project: marathonv5   File: NativeEventsTest.java   View Source Code Vote up 6 votes
private void checkDoubleClickEvent(int eventToCheck) throws InterruptedException, InvocationTargetException, AWTException {
    events = eventToCheck;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    Thread.sleep(50);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    new EventQueueWait() {
        @Override public boolean till() {
            return actionsArea.getText().contains("(2");
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(button, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    new Actions(driver).moveToElement(b).doubleClick().perform();
    AssertJUnit.assertEquals(expected, t.getText());
}
 
Example 3
Project: marathonv5   File: NativeEventsTest.java   View Source Code Vote up 6 votes
public void enteredGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_ENTERED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.keyPress(KeyEvent.VK_ALT);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    r.keyRelease(KeyEvent.VK_ALT);
    new EventQueueWait() {
        @Override public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    new Actions(driver).moveToElement(t).keyDown(Keys.ALT).moveToElement(b).click().keyUp(Keys.ALT).perform();
    AssertJUnit.assertEquals(expected, t.getText());

}
 
Example 4
Project: ats-framework   File: RealHtmlMultiSelectList.java   View Source Code Vote up 6 votes
/**
 * @return the selected values
 */
@Override
@PublicAtsApi
public String[] getValues() {

    new RealHtmlElementState(this).waitToBecomeExisting();

    WebElement element = RealHtmlElementLocator.findElement(this);
    Select select = new Select(element);
    List<WebElement> selectedOptions = select.getAllSelectedOptions();
    String[] result = new String[selectedOptions.size()];
    int i = 0;
    for (WebElement selectedOption : selectedOptions) {
        result[i++] = selectedOption.getText();
    }
    return result;
}
 
Example 5
Project: NoraUi   File: Step.java   View Source Code Vote up 6 votes
/**
 * Checks if HTML text contains expected value.
 *
 * @param pageElement
 *            Is target element
 * @param textOrKey
 *            Is the new data (text or text in context (after a save))
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_WRONG_EXPECTED_VALUE} message (with screenshot, with exception) or with
 *             {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT} message
 *             (with screenshot, with exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void checkText(PageElement pageElement, String textOrKey) throws TechnicalException, FailureException {
    WebElement webElement = null;
    String value = Context.getValue(textOrKey) != null ? Context.getValue(textOrKey) : textOrKey;
    try {
        webElement = Context.waitUntil(ExpectedConditions.presenceOfElementLocated(Utilities.getLocator(pageElement)));
    } catch (Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT), true, pageElement.getPage().getCallBack());
    }

    String innerText = webElement == null ? null : webElement.getText();
    logger.info("checkText() expected [{}] and found [{}].", value, innerText);
    if (!value.equals(innerText)) {
        new Result.Failure<>(innerText, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_WRONG_EXPECTED_VALUE), pageElement, value, pageElement.getPage().getApplication()), true,
                pageElement.getPage().getCallBack());
    }
}
 
Example 6
Project: colibri-ui   File: CheckSteps.java   View Source Code Vote up 5 votes
@Step
@Then("значение элемента \"$fieldName\" равно \"$expectedValueOrKeyword\"")
public void stepCheckValue(String fieldName, String expectedValueOrKeyword) {
    WebElement webElement = getWebElementByName(fieldName);
    String actualElement = webElement.getText();
    String expectedValue = propertyUtils.injectProperties(expectedValueOrKeyword);
    assertEquals(format("Значение поля [%s] не соответствует ожидаемому [%s]", actualElement, expectedValue), expectedValue, actualElement);
}
 
Example 7
Project: phoenix.webui.framework   File: SeleniumMultiValueEditor.java   View Source Code Vote up 5 votes
@Override
public void setValue(Element ele, Object value)
{
    ElementsSearchStrategy<WebElement> strategy = searchStrategyUtils.findElementsStrategy(WebElement.class, ele);
    
    List<WebElement> eleList = strategy.searchAll(ele);
    
    for(int i = 0; i < eleList.size(); i++)
    {
        WebElement webEle = eleList.get(i);
        String tagName = webEle.getTagName();
        String text = webEle.getText();
        
        String attrName = null;
        String attrValue = null;
        
        if(!webEle.isDisplayed())
        {
            continue;
        }
        
        if(filter.filter(tagName, attrName, attrValue, text))
        {
            webEle.sendKeys(value.toString());
        }
    }
}
 
Example 8
Project: kheera   File: AbstractPage.java   View Source Code Vote up 5 votes
@Override
public String getText(WebElement w) {
	String returnValue = "";
	try {
		if (null != w.getAttribute("value")) {
			returnValue += w.getAttribute("value");
		} else if (null != w.getText()) {
			returnValue += w.getText();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return returnValue;
}
 
Example 9
Project: marathonv5   File: JTextAreaTest.java   View Source Code Vote up 5 votes
void getAttributes() throws Throwable {
    driver = new JavaDriver();
    WebElement textArea = driver.findElement(By.cssSelector("text-area"));
    AssertJUnit.assertEquals("true", textArea.getAttribute("editable"));
    textArea.sendKeys("Systems", Keys.SPACE);
    String previousText = textArea.getText();
    textArea.clear();
    textArea.sendKeys("Jalian" + previousText);
}
 
Example 10
Project: testing_security_development_enterprise_systems   File: CounterPageObject.java   View Source Code Vote up 5 votes
public Integer getCounterValue(){
    WebElement text = getDriver().findElement(By.id("form:counterTextId"));
    String value = text.getText();
    try{
        return Integer.parseInt(value);
    } catch (Exception e) {
        return null;
    }
}
 
Example 11
Project: marathonv5   File: NativeEventsTest.java   View Source Code Vote up 5 votes
public void rightClickGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_CLICKED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON3_MASK);
    r.mouseRelease(InputEvent.BUTTON3_MASK);
    new EventQueueWait() {
        @Override public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    new Actions(driver).moveToElement(b).contextClick().perform();
    AssertJUnit.assertEquals(expected, t.getText());

}
 
Example 12
Project: hippo   File: SiteSteps.java   View Source Code Vote up 5 votes
private String getElementText(WebElement element) {
    if (element.getTagName().equals("input")) {
        return element.getAttribute("value");
    }

    return element.getText();
}
 
Example 13
Project: AlipayAuto   File: AlipayAuto.java   View Source Code Vote up 5 votes
private static String getOppositeUser(String transactionNo) {
	// ��ȡ�ؼ��ֶ�Ӧ��������
	WebElement keywordInput = driver.findElement(By.id("J-keyword"));
	keywordInput.clear();
	keywordInput.sendKeys(transactionNo);
	WebElement keywordSelect = driver.findElement(By.id("keyword"));
	List<WebElement> options = keywordSelect.findElements(By.tagName("option"));
	// until������ʾֱ���ɵ��ٵ�
	// WebElement selectElement = wait.until(ExpectedConditions
	// .visibilityOfElementLocated(By.id("keyword")));
	// ��Ҫִ��JavaScript��䣬����ǿתdriver
	JavascriptExecutor js = (JavascriptExecutor) driver;
	// Ҳ������ô��setAttribute("style","");
	js.executeScript("document.getElementById('keyword').style.display='list-item';");
	js.executeScript("document.getElementById('keyword').removeAttribute('smartracker');");
	js.executeScript("document.getElementById('keyword').options[1].selected = true;");
	js.executeScript("document.getElementById('J-select-range').style.display='list-item';");
	// ���ý���ʱ��ѡ��
	Select selectTime = new Select(driver.findElement(By.id("J-select-range")));
	selectTime.selectByIndex(3);// ѡ�е������������
	System.out.println("selectTime.isMultiple() : " + selectTime.isMultiple());
	// ���ùؼ���ѡ��
	Select selectKeyword = new Select(driver.findElement(By.id("keyword")));
	// selectKeyword.selectByValue("bizInNo");//�˴���value��д<option>��ǩ�е�valueֵ
	selectKeyword.selectByIndex(1);// ѡ�е��ǽ��׺�
	System.out.println("selectKeyword.isMultiple() : " + selectKeyword.isMultiple());
	WebElement queryButton = driver.findElement(By.id("J-set-query-form"));// �õ�������ť
	// ���������ť
	queryButton.submit();
	WebElement tr = driver.findElement(By.id("J-item-1"));// �Ȼ�ȡtr
	WebElement td = tr.findElement(By.xpath("//*[@id=\"J-item-1\"]/td[5]/p[1]"));
	return td.getText();
}
 
Example 14
Project: module-template   File: Unit2Helper.java   View Source Code Vote up 5 votes
public Units2 all() {
    if (unit2Cache != null) {
        return new Units2(unit2Cache);
    }

    unit2Cache = new Units2();
    List<WebElement> elements = driver.findElements(By.cssSelector("span.unit2"));
    for (WebElement element : elements) {
        String name = element.getText();
        int id = Integer.parseInt(element.findElement(By.tagName("input")).getAttribute("value"));
        unit2Cache.add(new Unit2Data().withId(id).withTextField(name));
    }

    return new Units2(unit2Cache);
}
 
Example 15
Project: colibri-ui-template   File: AndroidDynamicFieldSteps.java   View Source Code Vote up 5 votes
@Step
@Then("значение динамического поля \"$field\" равно \"$expectedValue\"")
public void stepCheckValueInDynamicFields(@Named("$field") String field, @Named("$expectedValue") String expectedValue) {
    IElement parent = getCurrentPage().getElementByName(field);
    IElement nested = pageProvider.getPageByName(DYNAMIC_FIELDS_PAGE_NAME).getElementByName(EDIT_TEXT_NAME);
    WebElement elementFound = finder.findNestedWebElement(parent, nested);
    String actualElement = elementFound.getText();
    assertEquals(format("Значение поля [%s] не соответствует ожидаемому [%s]", actualElement, expectedValue), expectedValue, actualElement);
}
 
Example 16
Project: marathonv5   File: NativeEventsTest.java   View Source Code Vote up 4 votes
public void movedGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_MOVED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    new EventQueueWait() {
        @Override public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    b.click();
    AssertJUnit.assertEquals(expected, t.getText());

    tclear();
    new Actions(driver).moveToElement(b).click().perform();
    AssertJUnit.assertEquals(expected, t.getText());
}
 
Example 17
Project: opentest   File: AssertElementText.java   View Source Code Vote up 4 votes
@Override
public void run() {

    super.run();

    By locator = this.readLocatorArgument("locator");
    String text = this.readStringArgument("text", null);
    String textContains = this.readStringArgument("textContains", null);
    boolean caseInsensitive = this.readBooleanArgument("caseInsensitive", false);

    this.waitForAsyncCallsToFinish();

    WebElement element = this.getElement(locator);
    String elementText = element.getText();

    boolean textIsValid = false;

    if (text != null) {
        if (caseInsensitive) {
            textIsValid = elementText.equalsIgnoreCase(text);
        } else {
            textIsValid = elementText.equals(text);
        }

        if (!textIsValid) {
            throw new RuntimeException(String.format(
                    "Element %s failed validation. The expected value was \"%s\", "
                    + "but the actual value was \"%s\".",
                    locator, text, elementText));
        }
    } else if (textContains != null) {
        if (caseInsensitive) {
            textIsValid = elementText.toLowerCase().contains(textContains.toLowerCase());
        } else {
            textIsValid = elementText.contains(textContains);
        }

        if (!textIsValid) {
            throw new RuntimeException(String.format(
                    "Element %s failed validation. We expected its value to contain \"%s\", "
                    + "but the actual value was \"%s\".",
                    locator, textContains, elementText));
        }
    } else {
        throw new RuntimeException(
                "Neither the \"text\" argument, nor the \"textContains\" "
                + "argument were provided.");
    }

}
 
Example 18
Project: testing_security_development_enterprise_systems   File: CommentPageObject.java   View Source Code Vote up 3 votes
public String getCommentText(int position){

        String htmlPos = "" + (position + 1);// XPath starts from 1 and not 0

        WebElement comment = getDriver().findElement(
                By.xpath("//table[@id='commentTable']//tbody//tr["+htmlPos+"]/td[2]"));

        return comment.getText();
    }
 
Example 19
Project: mot-automated-testsuite   File: WebDriverWrapper.java   View Source Code Vote up 2 votes
/**
 * Get the text within the specified element.
 * <p>Note: This is a low-level way to locate the element. Please only use this method if there is no better way to
 * locate the element, e.g. using a label.</p>
 * @param id  The element id
 */
public String getElementText(String id) {
    WebElement element = webDriver.findElement(By.id(id));
    return element.getText();
}
 
Example 20
Project: oscm   File: WebTester.java   View Source Code Vote up 2 votes
/**
 * Reads the text of the element with the given id. This is used for text
 * within an element, e.g. &lt;p id="id"&gt;text&lt;/p&gt;
 *
 * @return the text of the element
 * @throws NoSuchElementException
 *             if element is not present
 */
public String readText(String id) {
    WebElement element = driver.findElement(By.id(id));
    return element.getText();
}