Skip to content

RedsniffWebDriverTester

nico78 edited this page Dec 14, 2014 · 2 revisions

Entry point for commands

In above examples have all used an object called browser - this is an instance of a RedsniffWebDriverTester, (or a sub-class of it).

Just like the WebDriver type in Selenium/Webdriver, it embodies the browser, and simulates a user interacting with it, and is the main point of entry for running commands.

We create one just by wrapping a WebDriver instance, such as a ChromeDriver, if we're testing using Google Chrome.

Above, for clarity, we called the variable 'browser' but it gets used so often in tests that I would suggest using a single letter - I use 't' for tester.

 RedsniffWebDriverTester t = new RedsniffWebDriverTester(new ChromeDriver());

It provides the following instance methods (scroll right for description):



Method Example Behaviour
find(finder)
 Collection buttons = 
 t.find( button() )
 WebElement okButton = 
 t.find( only( button("OK")))
If passed an MFinder, such as div() it returns a collection of all such elements on the page. If passed an SFinder such as only(div()) then it returns that one element. In either case, it will throw an AssertionError (with explanation) if the expected item(s) are not found.
assertPresenceOf(finder)
 t.assertPresenceOf( div().that( hasName("search") )) 
Throws an exception if the expression does not find anything on the page
assertAbsenceOf(finder)
 t.assertAbsenceOf( div().that( hasName("errorBlock") )) 
Throws an exception if the expression does find anything on the page
the(finder)
 WebElement aDiv = t.the( div() ) 
synonym for find(only(finder))
newTesterFrom(webelement)
 WebElement mainSection = t.find(...);
 RedsniffWebDriverTester reducedScopeTester = t.newTester(mainSection);
 
 reducedScopeTester.find(.....);
 reducedScopeTester.assertPresenceOf(...)
If your testing was limited to a particular area of a page, (and which was guaranteed not to be replaced by an ajax operation), - e.g. a “main section” then you can limit yourself to this area by creating a tester rooted in this area, rather than the whole page. That tester will then behave as usual but just find elements inside the section – ie under the root element.
assertTextPresent(string)
 t.assertTextPresent(“Successful”)
checks that the text appears somewhere on the page (not recommended unless a small page). This is effectively reading the whole page as pure text then looking for your string inside that
assertTextAbsent(string)
 t.assertTextAbsent(“Something wrong”)
checks the text does not appear anywhere on the page. Again, not recommended – see above.
getPageSource()
 String renderedHtml = t.getPageSource()
returns the current page as html. Note that when debugging, rather than print this out, it may be easier to run in Chrome mode and then use F12 to inspect the page. Still useful if running in HtmlUnit (headless) mode.
getDriver()
 WebDriver driver = t.getDriver()
returns the underlying WebDriver wrapped by this RedsniffWebDriverTester
quit()
 t.quit()
Closes the browser, by calling driver.quit() on the wrapped driver
goTo(String url)
 t.goTo(“http://www.redsniff.org”)
Redirects the browser to the supplied url
clickOn(finder)
 t.clickOn(button(“OK”))
 //same as:
 t.clickOn(only(button(“OK”))
 
 t.clickOn( each(button(“Delete”)))
Finds the element described and performs a click on it. (ends up calling element.click() ) if given an Mfinder like button(), it will assume uniqueness and insert only(..) If you actually mean to click on every button found, then you can type t.clickOn( each(button() )
type(string, finder)
 t.type(“clowns”, textbox() )
 
 //same as
 t.type(“clowns”, into( textbox() )
types the supplied text into the item found by finder. (ends up calling element.sendKeys(text) ) Same rules on uniqueness and each apply Can use syntactic sugar method into(...) – which just takes and returns a finder – to make it read better
clear(string, finder)
 t.clear(textbox())
clears a textbox
tab(finder)
 t.type(“clowns”, into( textbox() )
 t.tab(textbox() )
tabs out of a textbox (depending on driver used, either by sending the tab key, or by firing an onBlur event
submit(finder)
 t.submit( form())
submits a form (use when you can't just click a button to submit for some reason – mainly there for legacy reasons)
tick(finder)
 t.tick( checkbox() )
same as clickOn but makes more sense for a checkbox – (might rename this to toggle or something)
choose(finder)
 t.choose( dropDownOption("Option 1")   
     .withinA( wicketItemByPath(dropDownPath))
       .withinA(wicketItemByPath(formPath)));
same as clickOn, but makes more sense for a drop-down (select) item.
clearSession()
 t.clearSession()
clears the cookies on the browser
assertDownload()
 TBA
TBA
waitFor(finder) TBA TBA
Clone this wiki locally