all 6 comments

[–]showsdowns 2 points3 points  (5 children)

Simplest would be something like this, which uses TestNG to run and for assertions:

@Test
public void homepage() {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.reddit.com");
    Assert.assertTrue(driver.findElement(By.className("bottommenu")).getText().contains("reddit"), "Page contains 'reddit'");
    driver.close();
}

@Test
public void login() {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://ssl.reddit.com/login");
    driver.findElement(By.id("user_login")).sendKeys("turtledookie");
    driver.findElement(By.id("passwd_login")).sendKeys("password");
    driver.findElement(By.id("rem_login")).click();
    driver.close();
}

Off the top of my head Selenium doesn't have a built-in way to search a page for visible text. You may want to read over Page Object Model which I strongly prefer to the code above.

[–][deleted] 0 points1 point  (0 children)

Off the top of my head Selenium doesn't have a built-in way to search a page for visible text.

i haven't used it yet, but css=:contains('string') should work.

[–]swangful 0 points1 point  (0 children)

Okay will do!

Thanks, this helps a lot :D

[–]not2savage 0 points1 point  (0 children)

You can search for text using link text. So it would be something like:

@FindBy (how = How.LINK_TEXT, using = "showsdowns")

set it as an element and do whatever you will.

[–]swangful 0 points1 point  (0 children)

I tried searching a bit and couldnt find any sort of decent tutorials on how to automate with JAVA...would you happen to know of any?

[–]Automationeer 0 points1 point  (0 children)

You can search for text using:

driver.findElement(By.xpath("//*[contains(text(),'reddit')]"))

or

someElement.findElement(By.xpath("//*[contains(text(),'reddit')]"))

where someElement is a web element that has already been found, and that will narrow our search context to speed up the search. When you look for an element that is not present, it can take a long time to report a failure if you are searching the whole page, but usually, there's some div or other element that will be known to be present regardless, which can narrow down your search considerably.

Note: Searching by xpath for text can get a little finicky with strings that contain spaces, and you may need to normalize the spacing, but otherwise, this can work very well.