Skip to content

Commit

Permalink
Merge pull request #25 from knpuniversity/code-blocks-ch12
Browse files Browse the repository at this point in the history
Add code blocks to the ch12 (login-scenario)
  • Loading branch information
weaverryan committed Nov 9, 2015
2 parents dfcabcb + 87ad5e9 commit 55e4112
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions knpu/login-scenario.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,62 @@
Open up `authentication.feature`, because, you can't do much until you
login. Let's add a scenario:

Scenario: Logging in
[[[ code('82516911da') ]]]

We remember from running `behat -dl` that we have a lot of built in language
already. Let's save some effort and describe the login process using these.
We remember from running `behat -dl`:

Given I am on
```bash
./vendor/bin/behat -dl
```

That we have a lot of built in language already. Let's save some effort and describe
the login process using these:

We want to start on the homepage. PhpStorm tries to help by autocompleting this
[[[ code('486cd08e9d') ]]]

We want to start on the homepage. PhpStorm tries to help by auto-completing this
step, but when I hit tab, it prints it with extra stuff. When you use the `-dl`
option, the "Given I am on" ends with a bunch of crazy regex. Anytime you see
regex like this, it's just forming a wildcard: something where you can pass any
value, surrounded by quotes.

Oh, and one other thing: even though each line starts with `Given`, `When` and
`Then` in the definition list, that first word doesn't matter. We could actually
say,
say:

Then I am on "/"

It doesn't sound right in English, but technically, it would still run.

Alright! Given I am on "/". Next, I will click 'Login'. The built-in definition for
that is "I follow". There's no built-in definition for 'I click' but we'll add one
Alright! Given I am on "/". Next, I will click "Login". The built-in definition for
that is "I follow". There's no built-in definition for "I click" but we'll add one
later since that's how most people actually talk.

But for now let's add,
But for now let's add:

When I follow "Login"
[[[ code('1506ced3d2') ]]]

Remember, these all use the named selector, so we're using 'Login' because that's the
Remember, these all use the named selector, so we're using "Login" because that's the
text of the link.

On the login page, we need to fill in these two fields. Again, because of the named
selector, we'll target these by the labels "Username" and "Password". There are a few
definitions for fields, byt the one I like is "When I fill in field with value". So,
definitions for fields, byt the one I like is "When I fill in field with value". So:

And I fill in "Username" with "admin"
And I fill in "Password" with "adminpass"
[[[ code('578445fea9') ]]]

Yes I know, that isn't the right password - this won't work yet. It's cool though.

Finally, press the login button with
Finally, press the login button with:

And I press "Login"
[[[ code('af59a089e0') ]]]

Notice that you *follow* a link but you *press* a button. Then we need to find something
to assert - some sign that this worked. Login on the browser. Hmm, nothing says
"Congratulations, you're in!"... but our login button *did* change to "Logout". Let's
look for that with:

Then I should see "Logout"
[[[ code('c88d3499f5') ]]]

Good work team. Let's run this!

Expand All @@ -73,7 +78,7 @@ is going on.

Debugging tip number 1: right before the failing step, use a step definition called:

And print last response
[[[ code('eb92324591') ]]]

Now run this again. It still fails, but first it prints out the entire page's code. Yes,
this *is* ugly - I have another debugging tip later. There are two important things: first,
Expand All @@ -95,7 +100,13 @@ on failures.
***

Let's remove our debug line and update this to the correct password which is
"admin". And now let's rerun it! It's alive!
"admin". And now let's rerun it:

```bash
./vendor/bin/behat features/web/authentication.feature
```

It's alive!

But we have a *big* problem: We're assuming that there will *always* be an `admin`
user in the database with password `admin`. What if there isn't? What if the intern
Expand Down

0 comments on commit 55e4112

Please sign in to comment.