Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code blocks to the ch12 (login-scenario) #25

Merged
merged 1 commit into from
Nov 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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