Web UI Testing with Java/Selenium -
Astronomy Picture of the Day Search Page
Step Definitions Code
If you do not already have code snippets from your feature file, see the main tutorial first.
Every step in our feature file is going to require the web driver, so let's initialize that in the main class before any of the steps.
WebDriver driver = new ChromeDriver();
Hover over
WebDriver and import, then do the same form
ChromeDriver.
I like to also create a variable to hold the start url here, but for this file you can do it within the first step if you prefer.
String url = "https://apod.nasa.gov/cgi-bin/apod/apod_search";
(Optional)
Now we can start coding the steps.
Our first step is
@Given("I am on the search page")
So, our code needs to open the search page. Delete the generated comment and the throw exception and replace it with:
driver.get(url);
If you didn't define the url variable above, then put the whole url here instead of "url."
You can run the feature file now to make sure everything's working so far. Since this is a Background step, it'll run before every scenario, so it should open one Chrome browser, plus another browser for each variable you listed in the data table on your feature file—and take you to the search page on each one. For simplicity's sake, let's say you listed three variables. Then you should have four open browsers now. Your console output should also show 19 Steps (11 skipped, 4 pending, 4 passed).
This is also a good time to code the script to clean up after itself. We can do this with an @After tag, which you can place before or after the other step definitions. It'll be run after each scenario.
@After
public void close_browser() {
driver.quit();
}
This will close the browsers for you. (You'll need to import @After.)
Our next step is
@Given("I have entered text in the search box")
So we need some text. We're going to want to enter it
and verify it's there before testing the "clear" button, so let's hold it in a variable. (First delete the generated comment and the throw exception.)
String text = "some text";
(You can put whatever you want between the quotation marks.)
Now we need to find the text box. Go to the search page, right-click on the text box and select "Inspect." That'll give you the HTML code, which should look something like this:
<input name="tquery" id="search" size="25" maxlength="60">
It has an id! This is usually the best way to locate items, so we'll find element by ID:
driver.findElement(By.id("search"))
(You'll need to import "By.") And we need to enter text into it, so follow up with .sendKeys:
driver.findElement(By.id("search")).sendKeys(text);
This should enter the text you specified earlier into the text box. To be able to verify this visually, let's let it sleep for half a second:
Thread.sleep(500);
The time is given in milliseconds, so 500 is half a second. Eclipse will probably ask you to surround the sleep with try/catch, so go ahead and do that (just hover over and select that option).
Now if you run the feature file, you should see your text entered in the first browser before it closes. The rest of the browsers will not have text yet, because their step comes later.
But—the point of this is automation! We don't want to have to watch it, so we need the code itself to verify the text was entered. Let's find the text and verify it matches what we sent. We'll find the text box (by id) again, and grab its value:
driver.findElement(By.id("search")).getAttribute("value");
and to compare this to our previous text, let's store it in its own variable:
String enteredtext = driver.findElement(By.id("search")).getAttribute("value");
Now, we want to assert that they match. We can do this with
Assert.assertTrue()
. You'll need to import
Assert
from
org.junit
. (Be careful
not to import from
junit.framework
)
The first argument is the error message it'll throw if it doesn't pass the text. The second argument is the test, so the code should look something like this:
Assert.assertTrue("Text in text box does not match", text.equals(enteredtext));
You can delete the sleep code if you want.
Now, we're at the button step:
@When("I click {string}")
Notice the variable. A string will be imported from the feature file. This is how we'll know which button we're trying to click. Let's give it a better name than just "string."
public void i_click(String mybutton) {
Go back to the search url, and inspect the buttons. We have:
<input type="Submit" value="Find the words">
<input type="Reset" value="Clear the form">
No id.
But the "value" is exactly what the feature file will pass in. We'll have to use an xpath, using our "mybutton" variable for the value.
driver.findElement(By.xpath("//input[@value='"+mybutton+"']"))
and we want to click the button, so add:
.click()
to the end:
driver.findElement(By.xpath("//input[@value='"+mybutton+"']")).click();
And that's it for this step!
Our next step is
@Then("the text in the search textbox should disappear")
We can copy the same code we used above to get the value of the text box, but this time we want to assert that the entered text is equal to an empty string. And let's change the error message to reflect that:
String enteredtext = driver.findElement(By.id("search")).getAttribute("value");
Assert.assertTrue("Text in text box has not disappeared", enteredtext.equals(""));
If you want, you can add another sleep step so you can see the text disappear.
And we're done with the first scenario!
Our next step is:
@When("I enter {string} in the textbox")
The strings will come from our data table in the feature file. Let's rename the variable to "text."
public void i_enter_in_the_textbox(String text) {
Now we can reuse the code we wrote above for entering text into the text box:
driver.findElement(By.id("search")).sendKeys(text);
And since we've already written the "click" step, if you run the feature file now, you'll see it run the searches. The console will also output:
4 Scenarios (3 pending, 1 passed)
19 Steps (3 skipped, 3 pending, 13 passed)
You'll see the first scenario has passed now, since we completed it. Our remaining steps will have the code assert that the searches run correctly.
The next step is
@Then("I should be taken to a results page")
We can verify this by finding the words "Search Results" on the page. We can do this by scanning the page source:
driver.getPageSource().contains("Search Results");
To make our code a little more readable, lets store this into a variable. It will return either "true" or "false," so we need a boolean:
boolean resultspage = driver.getPageSource().contains("Search Results");
Now we just need to assert that it is true:
Assert.assertTrue("Did not find 'Search Results'",resultspage);
And this step is done.
Our final step is:
@Then("{string} should appear somewhere on that page")
Let's again change the variable to "text":
public void should_appear_somewhere_on_that_page(String text) {
Then we can copy our code from above, only now we are searching for "text," which will be passed in from the feature file:
boolean resultspage = driver.getPageSource().contains(text);
And let's change our assertion error message to be more specific:
Assert.assertTrue("Did not find '"+text+"'",resultspage);
And that's everything! Running the feature file now should show all scenarios and steps have passed.
Back to tutorial.
--
SummerDale - 05 Sep 2020