Skip to content
Advertisement

How can I use a Selenium type method?

I have a JSP page with an input text field.

<table>
    <tr>
        <td><input type="text" id="searchText" name="searchInput"/></td>
    </tr>
</table>

I wrote a Selenium test case that verifies that the search input text is present.

public class UIRecipeListTest extends SeleneseTestBase {

    @Before
    public void setup() {
        WebDriver driver = new FirefoxDriver(
                        new FirefoxBinary(
                            new File("C:\Program Files (x86)\Mozilla Firefox 3.6\firefox.exe")),
                        new FirefoxProfile()
                    );

        String baseUrl = "http://localhost:8080/RecipeProject/";
        selenium = new WebDriverBackedSelenium(driver, baseUrl);
    }

    @Test
    public void testShowRecipes() {
        verifyTrue(selenium.isElementPresent("searchText"));
        selenium.type("searchText", "salt");
    }
}

The verifyTrue test returns true. However, the selenium.type test failed with this error:

com.thoughtworks.selenium.SeleniumException: Element searchText not found

What should I do to make the test work?

Advertisement

Answer

The first parameter needs to be a selector. searchText isn’t a valid CSS or XPath selector.

You would use something like selenium.type("css=input#searchText", "salt");.

I also wanted to point out that you seem to be going between two versions of Selenium.

selenium.type(String,String) is from the Selenium 1 API. You should keep to the 1 version, and if it’s going to be Selenium 2, you need to do something like,

WebElement element = driver.findElement(By.id("searchText"))

and use

element.sendKeys("salt");

Source: Selenium API type(String,String)

Advertisement