1 A First Web UI Test - essenius/FitNesseFitSharpSelenium GitHub Wiki
Ensure that FitNesse was setup correctly and that it is up and running as per the readme.
Open a browser, and enter URL http://localhost:8080/FirstSeleniumTest.
Enter the following test script:
!define SearchBar {name:q}
!define SearchCriterion {Dutch Cheese}
!define CaseInsensitiveMatch {^(?i)(${SearchCriterion})}
!define AcceptCookieButton {id:L2AGLb}
!define ResultStats {id:result-stats}
!|Script |selenium |
|set browser |Chrome |
|open |http://www.google.com?hl=en |
|wait until title matches|Google |
|click element |${AcceptCookieButton}|if visible |
|set element |${SearchBar} |to |${SearchCriterion}|
|submit element |${SearchBar} |
|wait until title matches|^(?i)(${SearchCriterion}) |
|wait for element |${ResultStats} |
|check |text in element |${ResultStats}|=~/About.*results/|
!|script |
|show|screenshot|
|close |
Save it, and press Test
. You should notice that a Chrome browser window starts and goes to Google search (may happen behind the FitNesse browser window). If you see the following test result after some time, then the installation went well.
Let’s do a quick walkthrough of the script. First, we define a few variables to hide technical details and prevent us from repeating ourselves. It’s easier to read that we are looking for a search bar than that we are looking for something which has a name q (which is the text box where you can enter your search at Google), and if we are looking for Dutch Cheese, we should specify that only once.
The script uses the selenium
fixture. We are using the Chrome
browser, where we open up Google. These days every site asks for permission to use cookies, so we accept that. Then we enter the search criterion into the search bar, and we submit the search (i.e. press Google Search). Now a new page should open with the results, and we handle that by waiting until the title has changed into the pattern we expect. This pattern is a regular expression, which translates to “starts with the search criterion, case insensitive”. Then we wait for an element with the result statistics (how many results were found and how long it took), and we check that it contains About
, then a number of characters, and then results
. Then we show a screenshot, and we close the browser.
Note how the script was split into two, and the second one doesn't use the fixture name. This implies a continuation of the previous fixture instance. This can be handy to show progress with jobs that take a bit longer since tables are always rendered when they finish, but it can also be better for layout. In this case, the second reason is the main one.