1

I tried to test the addition operation of 1 + 7 below; but don't know how to get the result output of the text field whose attribute "name" is "Input".

Any pointer would be appreciated.


package hw9;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class calculator {
  private WebDriver driver;
  private String baseUrl;

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.math.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testCalculator() throws Exception {
    driver.get(baseUrl + "/students/calculators/source/basic.htm");
    driver.findElement(By.name("one")).click();
    driver.findElement(By.name("plus")).click();
    driver.findElement(By.name("seven")).click();
    driver.findElement(By.name("DoIt")).click();

    String output = driver.findElement(By.name("Input")).getText();
    System.out.println("Output: " + output);  // **<--- Empty output**
    assertEquals(8,output);

  }

  @After
  public void tearDown() throws Exception {
      driver.quit();
  }
}

HTML of code in question is listed below:


      <td> 
        <div align="center"> <font size="+1"> 
          <input name="Input" type="text" size="16">
          </font></div>
      </td>
6
  • Try using By.id("Input") Commented Jun 27, 2017 at 23:20
  • The element doesn't have id; it has name and type attributes only. Commented Jun 27, 2017 at 23:31
  • You said text field whose id is "Input" in the first sentence. Also, it would help if you posted the HTML of the page you are testing. Commented Jun 27, 2017 at 23:38
  • I edited the original question already. Commented Jun 27, 2017 at 23:41
  • Even after you get your answer, assertEquals(8,output); will never succeed, because an int can never equal a String. Commented Jun 27, 2017 at 23:50

1 Answer 1

6

Try driver.findElement(By.name("Input")).getAttribute("value").

element.getText() is used to get the text node inside a tag, for example <tagname>text</tagname>.

But, an input does not have the text inside the tag (represented by a node), but inside the value attribute; for example: <input type="text" value="SomeValue">.

So, if you want to get the node text inside an element, you have to use element.getText(), but if you want to get the value of an input, you have to use element.getAttribute("value").

Sign up to request clarification or add additional context in comments.

6 Comments

There's no value attribute.
I believe this should work, see stackoverflow.com/questions/7852287/…
Hello, @user1972031! If there is not any value attribute, it means the value or text of the input is empty; in this case, when you call element.getAttribute("value"), this is going to return null.
Hello, @user1972031! I'm pretty sure that If your input has some visible text (it means, you can see it in the html page from the browser), then it has a value attribute with that text. If there is not any value attribute, it means the value or text of the input is empty; in this case, when you call element.getAttribute("value"), this is going to return null. If it is the case, then you have to check your SUT (System Under Test), which is not setting properly the value of the input, not your selenium script.
@budi: The link "stackoverflow.com/questions/7852287/…" that you provided has a good explanation (quoted verbatim below): -------------------------------------------------------------- Well, it turns out that if the attribute is missing, it will try to get the corresponding property. So you can take a "value" from a textarea. – C-F --------------------------------------------------------------
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.