 So bear with me. Thank you. Yeah, I'm really excited to be here I'm come from the symphony side of things. Hey, this is perfect transition into my first slide here There we go So I come from the symphony side of things Though we're not gonna talk about symphony today We are gonna talk about B hat and BDD which I am also something that's I'm a part of so that's why this is my first Drupal con And I'm really excited to see over here. I'm really excited to be here. In fact, there we go Now I can actually see everybody way back there So I'm in the symphony world. I do symphonies documentation primarily I also this is my one slide to sort of hit myself that that's the company. I work for a campy labs us. We do mostly Consulting and training around the symphony sphere. So like I said, I'm all set everything symphony This has been sort of crazy so far this week to be a part of What basically is about eight times the size of our normal conferences? So it's been awesome I'm also a writer for campy university calm, which is basically screencast and other ways to learn things online with good practices and actually not boring and I'll mention that again later because I'm gonna give you guys all a coupon code for a Free screencast that's gonna cover what we're gonna cover today in more detail So if you like what you see today with be hat and BDD, which I think you will you can keep running with it for free after This so I'll come back to that later most importantly I'm the husband of the much more talented Atlanta Pellum And she's fairly well known in the symphony world because I put her in each of my presentations like this How many people know Leanna? That's yeah, the the two people in the room. I know basically so So yeah, and when I did that in symphony thing It was like three quarters of the room raise their hand So if you could all so my favorite things to do before the cat got out of the bag in the symphony community about her If you could all if you have your phones just tweet at her right now. So that's her name. Just say hi She's here. You could say I don't know just you can you can heckle me towards her and she can provide me with that heckling feedback afterwards So that's probably less distracting than heckling me during it So say hi to her and she's here also and she's awesome and she's the voice behind camp University Come on. I do not want to have to advance myself. That's what I'll do There we go. Okay. All right. So today we're talking about be hat behavior driven development. So let's back up real quick a little bit of background Where things break down in projects and I think we will at least more or less agree with this is the Organization of the projects not necessarily the development It's the whole mess of who's doing what and how are we even organizing and planning the features that we're building so the typical project and probably this is somewhat familiar to people how many people how many people have seen this Series. Yeah, okay. Good. But I'll probably like half people Yeah, so there's like a million of these things This is basically what I'm talking about. So how the customer explained it how the project leader understood it How the programmer wrote it, which I actually really like because that just makes no sense at all I'm a programmer by the way. So if I'm ripping on programmers. I'm ripping on myself. I'm not an outsider What the customer really needed and my personal favorites, which is what we gave to the beta testers It's funny because it hits close to home, right? Okay, so yeah, so it's sort of funny because this is all like computer science, right? And this is sort of what actually happens in the real world and it's funny because it hits too close to home So where it breaks down first and foremost is different roles different languages people of different Technical levels and we are all very important in the overall process So we have to work together, but we use different languages and part of the reason you use different languages Obviously because we're at different technical levels This one is especially I like for Programmers is that our code may not align with our business value. The only reason we really write code I mean, this is not necessarily totally true because as programmers we'd like to write code But the only reason we're supposed to write code is for business value one way or another Even if that's your pet project, there's sort of business value in that pet project So does your code actually align with what your business needs or does your code? Did you kind of go off in a direction for four straight days because something was really cool or interesting and really wasn't that important? So being able to align what we're actually coding with the business values So that it's basically we're bringing as much value to the company with as little effort as possible And this is basically what I'm talking about So as if you like to program then this is probably at some way in the back of your mind We're like, oh, that's really cool. I should run with that and keep programming it and I know I'm supposed to be doing this other thing And the first thing is sort of over planning under planning planning that whole process How much do we plan? Are we planning at all? Are we over planning? Are we having? meetings like to our meetings every day six hours of meetings every day anybody Are we not planning at all? So kind of getting the planning process so that it we have to plan right but actually getting it so that we have Almost a plan for our plan. How do we plan efficiently? Okay, so let's get down to the the BDD stuff behavior driven development So here's sort of the evolution of test driven development here way back when somebody said hey We should start testing our code. There's unit test back there said, okay We have a function when I call this function I need to assert that basically when I put three and five in it It comes back as eight because that function is called add okay, and then later said somebody said We should write our test first so if we're gonna write to the then by the way use the calculator thing because that's sort of the Agile the unit testing example today We should write our tests first and then implement our code and the advantage there being that This is something that's really good for behavior driven development as well is that as programmers We need to know when to stop we need to know when the requirements have actually been fulfilled so with test driven development I write the test first and I stop working go home as soon as all my tests are green and I don't need to program any further than that because All my tests are passing so I basically done my job and then finally a behavior driven development is last That's what we're gonna talk about and it's very very interesting by far the most interesting thing on here at least from my perspective So the the the grandpa of this is of course in technology if you're a grandpa of something it happened only 10 years ago The grandpa of this is Dan north and he said this is a good quote Said behavior is a more useful word than test the idea is we need tests for application But instead of having them be something that's buried in the developer sphere somewhere And it's a very technical we want to elevate those so the tests themselves are more about the behavior What does the user do did they go to this page that they click this link when they click this link? What did they see that's the kind of language that developers understand because that's what they're building all the way up to the least Technical people they understand that that's what they're doing as well I'll mention this really quickly. I'm not gonna go over this But if you are curious about this there's two types of BDD there's story BD And there's spec BDD roughly what that's talking about is a unit test versus functional test If that's not making any sense to you don't worry about it If you're like, ooh, I know what that means and that's really interesting then go ahead and check out if you're interested in the spec We're gonna talk about the story BDD. So if you're interested about the spec BDD Then you can check out this library, which we're not gonna talk about it's a little bit low-level It's a little bit more of a developer tool only but it's also very nice so we're gonna talk about what's called scenario oriented BDD and By the way, we're gonna do Actually, how many people would in the room would consider themselves developers? Okay, that's good. That's kind of what I said that was most of the room and for those of you that didn't raise your hands It's okay. We're also going to be going through stuff That's kind of useful across the whole thing because the whole idea of what we're doing It's supposed to be kind of something that can be done on an organization-wide basis So we're actually gonna be going into real code But developers hang on with me for just like two minutes because we have to get a little bit of philosophy And they'll actually go into the actual practices here. So with behavior-driven development The goal is to create that single vocabulary so that when we're describing a feature We're all kind of using the same language to do it And this comes from all the way from somebody having like this big idea that they don't know about All the way down into implementing and testing it at every stage We're trying to get back down to like what's the actual feature supposed to do in this specific language So this is the four-part process that we're gonna follow and this is the theoretical part We're gonna go through these one by one in a second But basically here's the situation. Somebody comes with you. It's just this big idea It's like the owner of the company and you like bust into your Office or maybe your home on the weekend Hopefully not bust into your office and has this big idea and now we need to get that big idea kind of down into something That's saying so the first thing we're gonna do is it is probably is coming in with what we consider Maybe five different features because he's like an admin area this and front end this and mobile that Okay, so the first we're gonna do is we're gonna define the business value First you want to do is focus on the business value of whatever the idea is if it doesn't have business value Then it's not actually something we should work on Second thing is we're gonna prioritize those so now that we've sort of defined five different features and their business value Will prioritize those this one's the most important. This one's the least important Um step three and this is where things get interesting We're gonna describe those with readable scenarios and we'll see scenarios later But they're actually when we start talking about Um what the actual user story is meaning they go to this page. They click on this They do this that would be a scenario Uh describing your feature and then finally and of course, this is the behavior driven development part Um once we've done all of that we're actually going to implement it So the idea is we can describe in this language exactly how a future is supposed to work And that's actually the starting point for the development process Regardless of whether the developer is the ones that wrote that or if our project managers the ones that written that We actually have this specification and the important thing here is the specification is from the user's perspective Because no one on the business side cares whether or not we had to create a new database table for this What they care about is the user's behavior. You're able to go to this page Click on this and see this result. So that needs to be the starting point for us as developers to actually make sure that we're Um accomplishing the end behavior the end goal that they want So this is where something called gherkin comes in. How many people have heard of gherkin? Okay, it was like uh 25 percent or so Fortunately, it's very easy gherkin is a language and it's the language that uh is uh used to describe features So again, we have all these ideas. Somehow we have to sit down. Um, ideally it doesn't have to be this way But what if we are all describing these features with a structured language because obviously like anything else We're developers. So we know that if we're all speaking the same language, then we're going to communicate more efficiently So project consists of many features that need to be planned planned written and shared somehow already it might just be right now an email or Something that's jotted on a piece of paper and then uh handed to you. So gherkin is the structured language to do that and it looks like this So again, the idea is that we have five features coming in and we've decided to uh Uh separate those five features and we're going to do this So this would actually go into a file and we would describe the feature using this format Now there's three sort of uh, I guess there's sort of four variables there The first one's just a feature and that's just a title So we do something like feature and just kind of some title that you would know this is a feature for the admin area Um, then the three other parts are very very important The first part in order to this defines the business value and for developers This is the hardest one to get right because we're not used to necessarily thinking about the business value And i'll go over some good ones and some bad ones later, but we typically think of um something like in order to um Let's see here like in order to access the admin area. That's your business value I was like accessing the admin area is not business value You're not making any money because your admin is able to go into the the admin area You're making money because something more like in order to be manage the products that are on the front of our site That's kind of the business value. So a is the business value And i'll give examples of these uh specifics b is the role or person who will benefit So if we go on with the one I said earlier, it would be some sort of admin user or owner of the company some sort of Or content editor and then c here Is a short feature description Okay, so we're gonna come back to this but a and b are the two big ones the business value and who's gonna benefit from that business value So here's a kind of first example So, uh, it's you know, they come to you and says, hey, we need to have our site in french Okay, so this is what your feature might look like for that The business value is to be able to read the news in french that makes sense and you can actually decide That's not really good business value because we actually don't have any french users So it kind of spells out whether or not that's actually going to be important to you as a french user That would obviously be the person that's going to benefit from this business value So it's first two lines that I know this is all kind of theoretical stuff still at this point They force anyone on the chain and and this is mostly developers in the room To think about the feature like why am I building this in the first place and who's going to benefit and that Just that small process, uh, like believe me like I do this. This is how I I do my sides This helps you focus Um on what is actually going on and like why they would actually want something and potentially ask really, uh constructive questions back like why are we doing this again? Okay, so that step one would you'd get you'd basically um do that for all the different features that are coming in Step two is that you'd prioritize those so let's say we have three different features Those are the three features right there I've obviously not showing the other lines and you just prioritize those This is not something by the way this process that you necessarily need to do This is just a kind of a framework. It says if you use this process You can see how easy it is all of a sudden when you approach a large feature now that you've broken it down into smaller pieces Prioritized like well, where do we start? Well, that depends on Basically how important each of those business values are it's important for you guys to be able to start adding things into the admin interface Immediately then you're going to want to start with the features that benefit the admin user because the front end stuff would happen later Okay Now this is where things start to get a little bit more interesting So we have our feature and we're almost ready to start developing on it And this is where we start describing it as Scenarios and the scenarios are always written in the first person point of view of the person benefiting from it So that line up there as a site administrator in this case All the eyes down the one eye do this We are the site administrator and we're talking at the technical level of the site administrator Because the goal here is twofold one for us to kind of understand and be able to get a spec of what we need to build And two to do it in a language that can actually be understood by the person we're developing it for Right and if we can't describe The user's behavior and their language then this might not be a well thought out feature If it's so technical that we can't even describe it in language that they would describe Then something is poisoned something is amiss with the situation. So I think let's actually go down the next one and break this down Scenarios break down and scenarios the feature thing a second ago That's important and it's important to focus on business value these scenarios. This is where things are very important So they always kind of break down into the same three pieces which is given When and then and as I go through this even if you haven't seen gherkin before you're going to be familiar with this Because these are basically user stories. They're just structured in a very specific way So given is always your way to kind of play god and set up things So if you need to make sure that there are three page nodes and on your site or something before you start writing your scenario Then it's your Spot to basically play god and set up the system the initial state of the system Before the user would start interacting with it. So this guy's given. I am on slash admin slash news page The second part when Is the action they're going to take So it's always going to be some setup and then your user's going to take action And then finally then is going to be what did they observe after they took that action And this makes sense, right? I'm always we always want a user to do something and then observe that something happened And of course the you can also say and and but and that's just a way to chain it So the fact that we have and I fill in and and I press those are just basically when those are user actions So always given when and then and that's a single user path This is just one more example because by the way when as you get into this stuff This is kind of the hardest part is to kind of get a feel for how this natural language should be So given there are five news articles. That's where I think you're kind of playing god and say, okay We're going to start this user store I'm going to make sure there's five articles in the database to start Because when I am on slash admin page and I click news administration Then I should see five news articles. That's your way to kind of say Hey when this is this is basically the behavior that we expect from the news admin set or sorry the article admin section So we're about to kind of switch gears into what we can do with the gherkin stuff if nothing else If we didn't do anything else, I would want you to walk away and say that was interesting We should kind of like look at that gherkin language and at the very least kind of Maybe start to structure our conversations in that way at least of thinking about things in terms of scenarios and user stories Because that made sense and breaking things down in the features It would give us that kind of consistent language just to help make things a little bit more sane But it's going to get much better than this because we are going to turn those into tests and this is where things get really nutty So the behavior driven development and gherkin is what we've talked about so far We're going to switch gears now to a library called behat So having a standard way of describing our features and our scenarios is cool Having that human readable language execute as functional tests against our application is way way more interesting So behat, so I'm going to introduce a few different libraries to you guys piece by piece. Okay. The first one is behat This is just a php library And what it allows you to do and you'll see why this is useful in a second Is it allows it basically allows you to write those scenarios the given the when and the then What it does is it parses those given whens and thens and for each of those lines executes a php function The idea is that if we if we say something like given there are five articles in the database That would map to a php function Where in that function we actually guarantee that there are five articles in the database So we'll write human readable sentences. It's going to be executing a php function for each of those sentences How many people have heard of composer? That's more than I was expecting that is great. So that's great. I'm not going to talk about composer composer I mean, I'm going to talk about it. Obviously. I'm not going to talk about composer composer is uh Okay, so I'm going to talk about composer very a little bit. It's a glorified downloader It's actually much more than that. It's a sort of fundamental change in symphony or symphony In php. Wow Fundamental change in php that's going to allow all the different communities to share code and share libraries much more easily But for our purposes, it's a glorified downloader and it's going to help us download the behat library into our project So by the way, I'll post these slides online and also as I go through these slides I will have little bitlies in different spots. So if you're like, oh, what was that code that he used to install behat? I have bitlies on here. So you can just jot those down So this is what using composer looks like composers just say far file, which is an executable php file So you grab it and then you make a composer.json file which looks like this And this is basically a fancy little file that says please go download the behat library into my project Okay, so this is all about composer at this point So i'm not going to park on that too much and this is the actual way that you tell composer Go read that composer.json file, which is just a configuration file and download behat, which is what we had under that require key into our project Okay Now if you don't aren't familiar with composer the end result of this step Is we now have the behat library downloaded into a vendor directory in our project and that's it So nothing really scary has happened. It's just downloaded it for us. But this is the way that you're actually going to install it Um one other thing that it did so I sort of lied a little bit So it downloaded a behat into the vendor directory and it also gave you a bin slash behat executable And quite literally like if you you could do all of this inside of your drupal project It will put behat on the vendor directory and give you this bin slash behat file You can run it. This is just an executable and this is what running behat looks like This is obviously it's sort of help page and we're gonna use behat several times So so far we have behat in a vendor directory and we have a single executable file And that's it. Nothing. Uh, uh, it's scary or impressive yet So what behat needs to work Is feature files and so earlier when we were creating our feature I said for each feature you put that in a file that quite literally means that we will create a file called something like Article admin dot feature and we will put the feature and the scenarios under that feature inside of that file So what behat needs is just these feature files A single php class, which is going to be empty by default. I'll show you that in a second and that's it And later we're going to add a configuration file, but we're not going to worry about that yet So basically the only thing you need to start with behat once you've installed it is a single php class You run bin slash behat dash dash init and that gives you that single class So you can see it created a features directory a features slash bootstrap directory And then he features bootstrap feature context dot php class So so far I have behat downloaded into my vendor directory I have that bin slash behat executable, which I've just used to create this one file And we'll talk about why that's important that file is important in a second Um, but I want to keep keep the score if you will about just the few things that are happening There aren't big global weird changes happening And this is what that class looks like by default. So I'll talk more about the significance of this in a second But it is literally an empty class. So it did generate this file. Don't worry about this yet Cool. So let's um, we're obviously going to work our way up to doing some really interesting Interesting things with our testing our websites. Let's first test the using the ls program So let's kind of rewind 20 years or 30 years or 40 years Probably 40 years. Um, so when basically pretend like we're developing the ls Utility for you next so line is troubled like calls you on his phone and you guys are talking and he's like Hey, I need you to build the ls utility for me. And you're like, you know what? I'm going to use behat to test them do behavior driven development So we're going to describe the ls as a feature. So what I've done here is see remember it created that features directory So I'm actually going to put an ls dot feature file in the features directory And I'm not going to I'm going to talk less about the Feature stuff up on top, but you can see that's the business value and the person that's going to benefit from it And then this is your first requirement line. It says, um, okay Hey, if you have two files in the directory, then when you run ls, you should be able to see them That's the behavior of the applications. You're like, okay, that's a scenario That's sort of like one user flow. So let's turn that into a scenario And remember we're writing in the natural the voice of a unix user because that's who was going to get a benefit from our business value So it makes sense for us to run commands and do other things that a unix user is comfortable doing So the scenario is to list two files in the directory. So given I have a file named foo And I have a file named bar When I run ls, then I should see foo in the output and I should see bar in the output Okay, and by the way, I'm just making this language up. I'm following given when then but other than that I'm just saying whatever sounds most natural to me putting myself in the shoes of a Unix user. So there's not any rule or as why why did I use this word here or something like that? This is just me making these things up So now we've run that we've written our first feature file with a single scenario there We're going to execute behalf from the command line And this is what it looks like you can see it's actually finding our feature file And you can see it going over our scenario there And what it's saying is it says five undefined steps there in the bottom because remember what be hat does It reads each of those lines by the way each each of these lines is called a step So I'll say step sometimes and I'll also say kind of scenario line sometimes So five undefined steps because it tries to find a function for every step So I'm going to read this step this one line. I'm going to try to find a function to execute for that And the way it does that is via regular expressions So below that output that you just saw so this is still a command line It actually prints out a bunch of regular expression functions that you are then going to copy into that feature context class So you end up with something that looks like this So you wrote this language you just invented that language you ran behat and behat actually dumped out all these Functions with regular expressions above them that match the language that you used inside of your scenario So the one on top is a regular expression It says given I have a file named and then there's a little like wild card syntax there Anytime you quote text inside of a scenario So like given I have a file named quote foo unquote when it generates the regular expression for you It puts that in a little wild card syntax there And then when that line is actually read next time it reads given I have a file named foo It will execute this function and as for the arg one value it will pass you foo or bar or whatever you have there So step five here is all we need to do now is we wrote the scenario It generated all the php code and regular expression stuff that we needed So we don't ever have to write regular expressions with this. I remember when I first started hearing about behat I was like I was like I know what the next step. It's going to be it's going to be regular expressions And I'm going to leave so no regular expressions. I use behat all the time I've never actually even really needed to tweak a regular expression So it generated the methods for us with the regular expressions above them All we need to do now is fill in the guts of that So I given I have a file named foo. We'll just touch that file to make sure it's there I have a directory named dur so where I use the make dur I run that's us running a command so we use the exec function to run it and then then I should see in the output and Don't worry about this but basically this is using Basically what this last line does here and I did this kind of mostly for brevity on this slide here Is it basically checks to see if this string here is in that output And if it's not it throws an exception it throws an error So you could actually just do a normal string checking there to see if this string is in that string and if it's not throw an exception So now when we run bin behat it actually passes this time So what behat does it reads each of those steps and then executes the function That's tied to it by regular expression and of course what we're doing in those functions is Creating a file creating another file running ls and then verifying that those things were in the output So actually behind here if I like did If it did like an ls here, I'd actually physically see a foo in a bar file because that we were just creating a foo in a bar file Right inside that directory Yes, I always forget I have this slide out there. How many people know what this is? Okay, yeah, I figured It's a celebratory thing If you want to see the ls thing because the ls thing is really cool as far as like how behat itself works There's a bitly and I'll post the slides later and actually goes through some some other complexities and things that you would actually do in this case So again behat You rewrite a scenario step it matches the regular expression inside the feature context class So it calls that function And then in that function we do something And then unless the function throws an exception it's a pass So the way that you fail is you throw an exception inside that function and that would actually come up as a failure Cool. So let's go into the next part here. So having a um, this ls thing is cool. But let's uh The end goal of course is to do something on the web So one more library and then we're going to put them together and all kinds of crazy things are going to happen So behat executes functions mink totally different library Which is wonderful mink is probably my all-time favorite single library inside of php And I would use it in any project where I was doing any type of testing at all So what it is it's a standalone library So just you guys can bring this library or either these libraries into your dribble five projects right now if you wanted to It's just a standalone library bring it in there And it allows you to command a browser in php So kind of you would have a functions or you're where you're basically able to say go to this page Click on this link fill out this form press this button and then look on the screen to see if you saw what you thought you would see So we're going to go through mink very quickly because it's a very easy library Fortunately, uh, this is what it looks like to use mink directly. You you create what's called a driver and a session Those aren't very important until the next slide And this is actually what you have mink do that session you can think of that as basically like a browser So you say session arrow visit you give it a website And then once you've done that you can get the status code and get the current url from that page You can also go further to use css selectors So, okay. I want to be hat org now that i'm there. I can use css electors to drill down into different parts of my dom And once i've done that I could actually go and get the text attributes HTML anything I want about those And ultimately like click on an element Or fill out an html form or do whatever we want. What's happening in the background? We run this so this is actually just a php file And if I if I just went to the command line and executed this php file It's actually in the background by default using curl It's going in the background and actually going to be hat org and then Going through the dom and actually finding the link and clicking the link And if we had it filling out forms it would be physically going there and finding the form fields that we want to fill out Filling those out and actually submitting the the the form So this is actually making real http requests in the background. This is not some sort of fake system This is effectively like a very powerful crawling tool Um Two other things I will tell you about well sort of one other thing I'll tell you about this is very easily and we'll see this in a second very easily without changing anything But one line in the beginning I can have this test run in selenium Just change one line at the beginning and all of a sudden a real browser is popping up And you're clicking here and it's actually going there and this is why this stuff gets so powerful because you're going to have Uh things that you want to test that rely on javascript And you can't do that unless you open in selenium or something like zombie But basically something other than just curl requests The other thing is it means ceo a this uh this le click thing here There's lots of other functions that you can use on that Especially or well, it's most interesting if you're using something like selenium. You can say double click right click hover over hover off Blur focus right click drag two So you can actually use this interface to say find this css element and drag it to this other one That's all possible. There basically isn't anything. I haven't found that this api can't do It's all there even like the weirdest javascripty kind of stuff So if we're able to have this mink object inside of uh behat instead of our feature context Inside the place or a second ago. We were just touching files and doing very low level things We could be very dangerous because then we could start saying things like given i am on slash admin And then in our function we could say okay mink go to the slash admin page Okay, and that's exactly what we're going to do and we're going to do it with a very very small amount of effort So behat and mink are two different libraries mink extension is a plugin So when you hear extension in the behat world, that's the name for a behat plugin So mink extension is going to make putting these together Uh just a matter of configuration Cool, so i'll go pass this quickly but basically if you if you were sort of following along with your project You would already have behat installed. So all you need to do at this point is install mink and this mink extension library Which is nothing more than again, this is composer stuff But again, all this is doing is just downloading things into the vendor directory of our project glorified way of doing that So you can that's just a a few more libraries up there that's uh that we need So our all this does is add a few more things into our vendor directory and that's it I'm a big proponent. You could probably tell about Less layers and simple things. I don't like I don't like going to a talk and being like that was cool Except I have no idea what's going on He told me to like start using windows again or something and I got scared No, it's very very small layers here. Um at this point We have like five different libraries that are in the vendor directory of our project The feature context class and a single dot feature file and that's it. So there's not a lot going on Um In this case, uh, we are going to create what's uh behat.yaml file So so far we haven't done any configuration of behat and for the most part you don't you will make this behat.yaml file And what this basically says is hey behat look at this mink extension thing so that you can tie them together So I'm not going to park on this detail But this is what the behat configuration file looks like it just sits at the root of your project The only interesting thing here is probably the base url So if you're testing your application that would be uh, let's say we're doing local development That would be something like you know my site dot local Because when we use behat and make together in a second that's going to tell it kind of what domain to be making the request to And the only other change you need to do Is go to the feature context class And change it to extend mink context instead of behat context Now some of you in the room are going to be very comfortable with the namespaces object-oriented stuff and other people are not going to be as comfortable Um, there when you use behat, this is obviously object-oriented stuff. Now for the most part, uh, depending Well, it depends on your level you may not deal with this stuff very much or you may deal with it a lot It basically depends on where you are in the developer food chain. Are you a developer or are you more of a qa person? Um, so for the most part though, don't let that scare you the good news about this stuff And by the way, behat is not a brand new idea. This is based off of cucumber So you don't have to use behat. You can use cucumber Um, but the reason we have behat is because we're php developers and that's the syntax that we're familiar with So even if you're not totally familiar with the object-oriented stuff and that's somewhat new to you You're still developing in php So there are going to be some weird little things to to kind of learn to pick up on as far as the object-oriented side of things But it's php You're calling functions and it should feel very familiar versus kind of going to a totally different language and having your developers Try to try to do this stuff and in something like ruby Okay So extending a second ago by the way that said extends behat context because remember that class was generated for us Now it says extends make context And that did two things for us The first thing is anywhere inside of our feature context class remember that's where we write our functions Um, we have access to the mink object. That's when we saw a second ago So very quickly now you can see oh man, I've accessed the mink object I can start making it surf around to different pages and find things and click links and right click and double click The second thing it gave you this is much more interesting Is a pile of definitions So a step is a single line in a scenario A definition is the php code That's executed when that step is read And one of the great things about using behat is as you use it more and more Your list of definitions is going to get greater and greater and greater on day one You may have some basic definitions so that you can say hey when I go to this page and click this Those might be built-in sentences that you can say because those come out of the box I'll actually show you that in a second, but eventually you'll you'll start having your own custom definitions And what that means is your own custom sentences that hook up to some functionality And after let's say a few weeks or a month Your bag of pre-built sentences that you can use to test your application is going to get greater and greater and greater And what ends up happening is more and more and more you're able to write sentences to test your application And not actually have to touch php code at all because you have this big bag Of ready to go sentences that you can just pull from to say go to this page Log in as this user with this rule create this node all these built-in definitions So a second ago before we extended mink context Those were the four built-in definitions we had That's really the only language that we could have used so far in our scenario file And obviously those are the four that we did for the ls feature And by the way, I got this by running bin slash bhatt dash dl So that stands for definition list list my definitions after we extended mink context This is what we have and I know you can't read that which is awesome Basically all the normal things you do in a web Context are already done for you when I go to slash foo and I click on Search and I fill in this field and then I should see this in this element All of those things you can say and you don't have to write the php code to do it Because you've inherited now from the mink extension library So example of this suppose we're testing wikipedia.org or this could be your site Let's create a feature. Okay, so that's what the feature looks like We're actually going to test the search part of wikipedia And there's a scenario Okay, given I am on slash wiki slash main page when I fill in search with behavior driven development And I press search button then I should see agile software development This runs without any coding Because those were all definitions that were built in if I looked at that definition list You would have seen things like when I go to and then a little wild card that tells you that you're like Oh, I can just write in my scenario when I go to slash admin And I don't have to write the php code to command and make to do that. It's already done for me So day one you're going to get a whole bunch of these things out of the box And that's what i'm talking about like as the days go by you're going to write your own And it's going to get bigger and bigger and bigger and the menu of things you're able to do Is going to get greater and greater and greater So let's talk about behat inside drupal specifically because behat inside drupals is uh, Actually exceptionally interesting because of the sort of special challenges of uh, of having a drupal application So so far we've talked about what I call black box testing Which is we're here on our local development box or some server And we're going to test something that's way over there totally different server We just used uh behat and make a second ago to test wikipedia Clearly I do not have access to wikipedia servers. Who cares? We're just making real http requests So I call that black box testing And there are some extra challenges with that because you can't of course write tests that uh, you know If you write a test that says you know login with this username in this password Your tests have the potential to break two weeks later if somebody changes that sort of test users password So you have special challenges when you do the black box testing, but it's incredibly pragmatic You guys could all during the next session at the end of this session Actually install behat and start testing your production application Right from your local machine by writing these human readable sentences and probably without doing any php code You could probably do quite a bit of damage with it So that's called black box testing and then the other way of testing is actually more for custom development This is where we actually take control of the drupal instance and say no no no Before I start this test I want you to insert these three nodes these three page nodes and these two users and this user should have an administration role So that in the rest of the scenario you can say when I log in is that user and I click to edit this node Basically the rest of your scenario you're guaranteed to have that set of data because you've actually taken control of drupal And put those items into the database before the test ran So it's like how do we do this? How do we sort of bootstrap drupal or get access to drupal inside of uh Inside of our feature context our behat class Um Unfortunately, you don't really have to worry about that actually skip passes really quickly. This is this is exactly what I said before It's okay. If I want to install this stuff on my application That bitly up there is just that composer that json file. So number two that was the question It's like, okay, how do we get access to drupal? And the answer is Somebody else already did it for you So introducing some of you guys already know this but just if you do not pretend like you're surprised and it's awesome Anyways, because this is really really awesome introducing a library made by the drupal community Which I did not help with So I was like to say when I should not be giving credit for something though. I have looked at this library and I was like This is awesome Called drupal extension Yes, this is awesome. Oh, that's nice. Yeah I love you guys So uh drupal extensions. Okay, so you're already excited about it, which is which is I haven't even told you what it does yet Um, so it gives you a number of things one even more built-in step definitions. So even more things That are helpful or drupal specific Are sentences that you don't have to write any php code for they're just going to be there So if you ran installed this drupal extension and ran that bin slash behat dash d l the definition list The list is going to be twice as long with things that you can run out of the box Second thing is it gives you the ability to actually hook into your drupal instance and take control of the data So a very simple shown example is a second you can start adding nodes and users and and and other things It also gives you this is a really cool thing Which is not drupal specific at all although we didn't realize that until I saw these guys implement it The idea of regions and what this is going to be instead of saying something like when I click submit It's like well, what if I have three buttons on my page that I'll have the words submit on it? It's how does uh Mink know which of those three buttons to do and in those cases you have to do a little bit more work to kind of target it Well, it's sort of a built-in support for regions. So you can basically say I'm going to call Let's say we have a sidebar region I'm going to go in a configuration file and say the sidebar region has this css selector You know sidebar region has pound signed sidebar That's its css selector then in your uh steps you can say when I click on search in the sidebar region So it makes this much easier where you can be confident that you're targeting the little spot you have And the original reason that This is sort of thought up is if we change themes In theory it can just be a change of configuration if you're using a lot of regions because your region might not be pound signed sidebar anymore It might be something different But if you're sort of referring to it generically as the sidebar Then you can just change the configuration file the point to a different css selector On this one. I'm going to mention again Cooks to load more sentence definitions from uh contrib modules. I'm going to come back to that one a second This one has Dangerous dangerous awesome potential especially with a community the size of the Drupal community and as active as the Drupal community is So I'm going to talk about that one again in a second, but I'm going to show you an example first So this is a scenario I am not a Drupal developer and I did this in about five minutes and it worked out of the box And I had no one to hide nobody to high five because there was nobody there with me But I was really excited So given I am logged in as a user with the administration role By the way, this is using something called a background. Don't worry about that Basically, that's a given statement Uh, given I'm viewing a page node with the title cool means When I click edit in the body region and I fill in the following and I press save then I should see ipsum in the body region Whoops my bad There we go Yes, um and actually This actually works. I should have been a little bit more descriptive with this what a better test would have been With the title cool beans, then I should see cool beans in the body region. So just kind of pretend that's there This actually works, but not it's not as clear as I would want it to be So this is what's really cool about this. This is what the Drupal extension installed when I say given I am logged in as a user with the administration role or the sorry the administrator role That actually goes behind the scenes creates a user with a random username and a random password in the database Gives them that role then tells mink to surf to the login page Puts in the username and password of that user hits submit and is done All of that happened with that one sentence Behind the scenes. Yes. This is what I'm talking about. This is incredible potential Uh, same thing down here given I'm viewing a page node with title cool means This actually creates a node of type page and gives it that title behind the scenes Those are built-in definitions that came with the Drupal context So you don't have to uh do any coding uh if you don't want to um, but what's really cool about this Of course is when you do find something that's not built in You know how a library this Drupal extension that's doing things very similar to what you might need to do in Drupal So it's like the age old thing where it's like we have examples So even when you need to push this further than the Drupal extension might be giving you You have a nice library that's already doing things interacting with Drupal behind the scenes and you can be like Oh, I just need that same thing but just changed a little bit um, and then uh, and then this of course is the the region thing that I was talking about That's specific to the Drupal extension as well, which I actually really really like um, so that's it So I did a Drupal install uh made uh out of the box I installed behat I wrote that feature in that scenario and I ran it and it worked And that just worked completely out of the box for me I went to my database and I saw there was a new username in there and a new node and all that kind of stuff Um, because you know when things work on the first try you're usually suspect like that Well, I don't know Um, these are uh the Drupal extension runs in three different modes There's the black box mode that I was talking about before there's also a Drupal and also a Drush mode what these define is How should we take control of the Drupal instance? The black box mode doesn't take control at all So if we did if we were using the black box mode and we wrote given I have a user With the administrator role that actually throw an air and say in black box mode Um, you you're not allowed to do that and this is what you would use You'd use black box mode if you're testing uh testing against your production machine or your staging machine And you're not actually intending to mutate the data. You're you're there to kind of be a witness What I really like and and this library has just been built beautifully. It's just such a fantastic example of developers from the Drupal community coming in Um and using a tool that was made more by the symphony-ish community and just doing an absolutely wonderful job with the implementation of it So behind the scenes you can go into Drupal or Drush mode So as far as I do want you to take control of my Drupal instance and insert things in the database. How does it do that? The Drupal uh mode is perfect for local development It actually bootstraps that local development instance and it could just call the same kind of functions Uh, you know create node add user those types of things. Um, those probably aren't actually the function names I have used those functions before but I'm not fluent Uh, it uses those to actually create those users. Cool Drush that one's very interesting This is where it actually uses Drush behind the scenes So this gives you the potential to be executing your test locally and have Drush pointing to some other server And it's actually adding users and nodes and things like that now that one's a little bit And I'll kind of mention Some of the people behind this so you can talk to them after that was a little bit of a working process because I don't think that Drush, uh, at least right now version. I was using Nestle supports certain things like creating nodes But I'm talking way outside of my area of expertise and all of this Works across the board. Of course this one maybe not because that one's changing all the time But basically there are drivers inside the library that you just set the mode. You're like, I'm using Drupal 6 So when it goes to interact with Drupal, it's like, okay, we're using the Drupal 6 functionality or using the Drupal 7 functionality So this is not a 6 7 or 8 tool. It's just been written beautifully. So it's basically it's got a driver layer So that you just tell it what version and it knows how to call how to interact with your Drupal instance Um, okay, this is the last thing that I was going to talk about which the contributed modules And this is where the the potential is huge for this community So imagine That you bring in a module some popular module that helps you do thumb nailing or the views module And that module actually has packaged within it people have contributed built-in definitions So you install the Drupal extension and you get all these beautiful Drupal specific things Then you install views and of course, I know views is core and Drupal 8 But just follow me with this you install the view module and all of a sudden you have 20 more built-in definitions that are specific to views and all of a sudden you have things like given I have a view with this and that and this and that So every module that you install gives you more and more pre-built definitions that you could use This is not something that is there yet. What's there is the Drupal extension Already has built-in hook points to automatically bring those in The only thing that the community that we need to do is actually go into those different modules and add them So and if you guys are using the views module for for instance and you're testing against that I guarantee you're going to start writing sentences like this given I have a view this and this and this and behind the scenes You're actually going to write the code your own custom definition to take care of that If you contribute that back to the view module And somebody brings the view module in this will automatically pick that up in the rest of the Drupal community Which I understand is quite big We'll enjoy that and hopefully they'll give you your things back And this is this is especially interesting because of the the black box testing It's always when you have this site that's on production. You don't even necessarily have developers And you have these 10 modules installed on it and you can actually test it with this huge Glossary index of these built-in definitions from all the different modules that you have So this to me if that takes off it's game over This is going to get totally out of hand in a really exciting way Really really quickly the fact that as a community we're basically sharing these definitions these built-in functionalities to accomplish things So do it. This is this is exactly what people are going to people are already using behat So matter of us contributing things back and kind of getting that conversation with Module maintainers that may or may not be familiar with behat and kind of talking about the the pros of doing this Last thing I'll talk about and it's so easy You want to run these tests in selenium because so far when you run these tests when you write these english sentences It's just running curl requests in the background. It's just silent. It's not actually opening a real browser That's all you do That's it on a scenario by scenario basis You opt in to selenium or you don't opt into selenium and that's it So if we have a feature with five scenarios and only one of them actually requires javascript I mean the rest of them. It's it's the same javascript on or off You'd have four scenarios without that little at javascript and you'd have to job at add the at javascript where you need it If we ran bin slash behat with this Actually, I'll come back to that slide a second It looks exactly the same if you could actually see this what happened is it ran A browser popped up it did all of those things I commanded it to do with human readable sentences And then it closed and it said that it passed So this is real world stuff This is why I use behat and make more than anything else because I end up with a test suite that's like 75 Headless meaning things that don't require javascript And if you don't open up a browser it runs faster because opening up a browser It's just something that's a little bit more expensive and then I have 25 of my test suite that does require javascript So I just put that little flag on there and i'm done It also makes debugging a little bit easier because I might have something that doesn't require javascript But I'll just add the at javascript for a second so I can actually see it pop up and and actually like see what the test is seeing So I can see like why the heck my test is failing Oh, and uh, this is how you run selenium So if you haven't run selenium you download a jar file and you run one command And that's it. So even the selenium side of things at this point is sort of absurdly easy cool and Basically, we're done at this point except well, I'm done now It's your guys's turn your homework one install the drupal extension It's a project on drupal That's just a bit late to go to the project on drupal It tells you the installation instructions It's even easier than what I've told you because it takes care of downloading everything else for you Second writes some features for your app And there's documentation on how those look And that's it high five your teammates and celebrate you actually can start testing you can do this this afternoon You can do this uh this weekend tomorrow during the hackathon And last thing I'll say is if you want to go further with this and this is what I was talking about earlier You can go to our website kmp university dot com that coupon code is going to be good until tomorrow So just I see people already doing it right now just make an account and that usually costs 12 dollars We're just giving it away for free. That's going to take um, that's really going to take you through a lot of the Basically everything that you're actually going to need to know we don't talk about the drupal extension It's the only thing we don't talk about specifically there everything else you're going to need about drupal Is going to come from uh that screencast right there second thing Talk to the open source people. I haven't mentioned their name yet the open sorcery people. They're a company here in portland They're absolutely awesome. They're the ones that have been so far just driving this stuff So there's no official company in funding behind doing the drupal extension things But they've been doing it because they're using it as I understand basically on everything right now They're using on all their client sites. They've been awesome. They're doing wonderful things So read their blogs and find those guys. Um and last thing my friend molessa anderson If if you're more of a business person, um or really anything She has been doing a tremendous amount of this work and she's thinking about this problem from a holistic thing Like who actually writes these things? Um, who runs them when they fail? Who's notified? What are the challenges with sort of the live black box testing type of thing? She's wonderful. She also um did quite a bit of work on writing behat tests for drupal.org to facilitate the drupal 6 to 7 upgrade There you go Oh, yeah, and she's right there. Yeah, thank you. Yeah, she's right there in the corner Yeah, her friend was selling her out, which I thought was awesome Cool and and that's it and the last thing I'll say is special thanks to jonathan, uh headstrom He works for open sorcery and he was absolutely awesome in helping out this and he is the main author Um with along with some other people about the drupal extension and uh, oh, and that's liana and I and we love you I'm so happy to be here and thank you very much for having us Um, you can see that I was tweaking things last second Your question. Yep Is this on yeah, it is worth noting also that it is Very easy and possible to integrate a suite of behat tests with jenkins Which when you are working with an automated dev flow Is really really awesome because you set up a bunch of tests and you have immediate regression testing So we are doing that on a number of our sites And uh, it is worth the extra one step if you are already using jenkins to integrate your behat tests with jenkins Yeah, it's very easy and and uh, if you're interested in doing that Just go tackle him because obviously he has lots of experience in it and we do it the same thing It's not it's not hard at all. Um, so absolutely. Yep Uh, do we have any metrics yet on speed comparisons between this tool and say like cucumber carabara? Oh, it is as far as that's going to be the same because where you slow down It's uh, you slow down because it's hitting your site So it comes down to how fast is your site loading as far as like which one loads faster It's it's not going to be enough your sites. What's slowing you down? So if you're using selenium I'm my assumption is that basically Especially if you're developing on local hosts, you're gonna have a browser window pop up and then disappear That's absolutely correct. Yeah, because like you can barely I doubt you'll be able to actually see what's going on Oh, so good. So good. Yeah. Yeah, so there a way to like slow it down and like make it do a delay between each step Yeah, so I'm so glad that I paid you to ask that question. I'm just kidding. No, that was I love it. You're already thinking ahead to like the debugging thing. There isn't I mentioned in the video, but it's um I mentioned in the video it's called it's a another extension called the bhatch extension It adds more custom definitions. I don't actually use that extension But it has a really helpful definition in it and if you watch the screencast You'll see I go and grab it and what it allows you to say is when you're running it in selenium Because it's right selenium with nice thing about selenium is it runs super fast So you're like You know and it closes when it fails too, so you can't actually see it so two things about that I'm actually going to change gears here. One is it is possible also to have screenshots Taking the failures. There's ways to hook in and say it failed So let's take a screenshot of what we saw the other thing is you make you bring in this custom definition And what it is is the language you make the language whatever you want But the language I use is I say I break so I will see that I have a failure right before I let's say I I'm about to I say and I click submit and I get an error that says like no submit button found So I'm like what's going on is am I looking at an error page is submit button label something else So right above there temporarily I'll put and I break and I'll rerun the test And what it does is it actually opens up goes there does everything and as soon as it gets to the and I break It pauses it freezes And what it's doing if you look at the terminal is it just says like press enter to continue So it'll sit up there and just basically sit there And you can like right click and inspect element and see whatever you want to do and as soon as you're satisfied You just go back to the terminal hit enter and it keeps going. So that's not something that's built into Be hat by default But like I said look up the b hatch extension or watch your screencast It's about four. It's a function. That's about four lines long and it gives you that capability Probably it probably should be in core because I basically can't live without it Any other questions? I guess if we are we going to create that All right, perfect. So we're we are going to We as a generous word on my part create a be hat group on dribble that'll work So that's not created right now, but it will be created either today or tomorrow So find on there we can kind of continue the conversation and and and basically hopefully I'll work together to get more and more Definitions and strategies behind this. Yep Uh, I don't know if there's an irc for this you'd have to look you'd have to look it up I don't I don't know mostly because I get distracted easily on irc Awesome. If you have if you have more questions. Oh, here's one more. Yeah Yes, I have one question where I work at we have One lady who does all the testing on the front end So whenever marketing tells us to build something marketing doesn't actually test it the people who deal with the people who deal with our website test it Um, one of the problems that we found is she's a great tester, but a lot of times she can't communicate To us developers or we don't understand her as well as we should And this in our scenario would this be helpful where she actually creates the be hat Win and all that stuff and then that creates the class and we go in and modify it as needed How technical is she and what's she doing to test now? Is she testing manually or is she doing like a selenium test? She's we thought about throwing selenium on firefox for her, but um, I don't think she she's able to right now Okay. Yeah, so um in that case the the Basically the one of the beautiful things about using be hat is when you have developers that are comfortable with php It's going to be a huge tool because you can do whatever you want to now for people that are less technical You basically have a few options one of the big advantages over this over selenium Is that selenium requires x path that makes your test very fickle when you write these scenarios? You rely on labels and so as long as your label doesn't change no matter if you move things around then It's going to be less fickle. So um, basically to answer your question. She may be able to write these tests I would try that But it may it may end up being too technical for her still even to kind of get this pattern down there That's something that melissa is basically looking at as far as like the holistic process of what that would look like Now in theory if the developers are Also writing tests for their application Then you guys are going to be building up a big suite of built-in definitions And at some point you could actually have her write those built-in step definitions Because she would have a big enough menu that she could actually run those now The only problem with all this still and this is something that I think that we will fix Shortly like over the next six to twelve months is that there's no way for a non technical person right now to run their tests So all this stuff is great human readable stuff, but it would still be I wrote this beautiful human readable thing It's using all step definitions that are available But I still need to go give it to a developer so that developer can run it from the command line Okay, that's something that actually like melissa what some other people was working on Because then it's actually quite powerful because that's just human readable sentences Is there a web interface to actually start using these and click and see if they fail and if they fail That what step did they fail and kind of bring that up to the higher level Then it starts to become a more interesting tool for the less technical people. I think that's very important Yeah, we actually have had some success kind of on that front With actually even a client a client project manager who wrote out What was more or less a set of instructions? Yeah, that's that's pretty good. It's not exactly the right format but close enough right and then you okay Well, this is what they meant to do and this is the scenario that they want to test And then you just take and you tweak it so that it fits into the regular expressions Um You get they get it close enough so that you can see what are they trying to test Your level of effort for building those tests is really nominal once they've done most of the work To define what they're actually trying to test. So I think that's I think that's that's it right there And Melissa was actually talking about this. We were in a meeting before this was like You don't necessarily expect your project managers or salesmen to write these perfectly But you can get them to write them in because they're user stories, right? If you're going to talk about the user in action You're going to get that and then you're just saying the effort to get those into the actual correct syntax Sexual correct structure language is nominal and you're still kind of forced to get into that Even though we're the ones writing the gherkin language We're forced to get into that structure of thinking about the business value and writing out the requirements from a user perspective It'll also give you really good insight into oh, I didn't think of writing a test for this particular component But now they want me to write this, you know, this one function this this one test this one line Oh, okay. That's something that's useful. Let's write that nice. Oh, yeah So here's in yeah, and they'll they'll kind of in that case They're almost saying something that is translating to a step That you don't actually have defined because you didn't really thought about that but now you can now Yeah, now you can define it All right, if you have any other questions come find any me or any of the people I mentioned But thank you guys very very much. Enjoy the rest of the conference