Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

From Install to JS Testing

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Welcome to the magical world of Behat, my favorite library. No joke this thing is the best. Behat is about two things:

BDD, Functional Testing and... Planning a Feature???

First, functionally testing your application. Which means writing code that will open a browser, fill out a form, hit submit and verify text on the other side.

Why test your application? Well, imagine you are in charge of safety at Jurassic Park, your job is to make sure guests aren't eaten by dinosaurs. You need to be certain that the new pterodactyl exhibit that's being put in won't turn off the electric fence around the velociraptor pen. No tests? Good luck, they know how to open doors.

And second, designing your application. As developers we'll often just start coding without thinking about what we're building or how the feature will behave.

Using behavior driven development, which Behat helps you do, you'll actually plan things out beforehand.

Imagine a world where communication on your team is perfect, you always deliver exactly what your client wants, electricity on the raptor fence never goes down and chocolate ice cream is always free. Yep, that's where we're going.

Behat Docs

Over in the browser, let's surf to the Behat documentation. In this tutorial we're covering version 3, and for whatever reason when I recorded this the website still defaults to version 2.5. So double check that you are actually looking at version 3's documentation.

I've got our project, the raptor store, loaded here. This is where dinosaurs go for the newest iphone and other cool electronics. There's a homepage, an admin section and that's basically it. This store is built using a very small Symfony2 application -- but hey, don't panic if you're not using Symfony: everything here will translate to whatever you're using.

Cool, we've got a search box, let's look up some sweet Samsung products. And here are the two results we have. I want to start this whole Behat thing by testing this. Hold onto your butts, let's going to get this thing running!

Install and Configuration

Tip

Installing Behat on Symfony 5 or newer? Read this blog post to know about how to install and configure it properly.

Over in the terminal run composer require and instead of using behat/behat we'll grab: behat/mink-extension and behat/mink-goutte-driver:

composer require behat/mink-extension behat/mink-goutte-driver

These are plugins for Behat and another library called Mink and they require Behat and Mink. We see the Mink library downloaded here, and the Behat library downloaded down there. So life is good!

Once you've downloaded Behat you'll have access to an executable called ./vendor/bin/behat or just bin/behat for Symfony2 users. Running it now gives us a nice strong error:

vendor/bin/behat

That's ok because we need to run it with --init at the end just one time in our application:

vendor/bin/behat --init

This did an underwhelming amount of things for us. It created two directories and one file.

In PhpStorm we see a features directory, a bootstrap directory and a little FeatureContext.php file and that's all of it:

... lines 1 - 2
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
... lines 5 - 6
... lines 8 - 10
class FeatureContext implements Context, SnippetAcceptingContext
{
... lines 13 - 19
public function __construct()
{
}
}

While we're here, I'll add a use statement for MinkContext and make it extend that. I'll explain that in a minute:

... lines 1 - 6
use Behat\MinkExtension\Context\MinkContext;
... lines 9 - 11
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
... lines 13 - 25

One last bit of setup: at the root of your project create a behat.yml file. I'll paste in some content to get us started:

6 lines behat.yml
default:
extensions:
Behat\MinkExtension:
base_url: http://localhost:8000
goutte: ~

When we run Behat it will looks for a behat.yml file and this tells it:

Yo! Our application lives at localhost:8000, so look for it there.

Your First Feature and Scenario

Behat is installed, let's get to writing features! In the features directory create a new file called search.feature and we'll just start describing the search feature on the raptor store using a language called Gherkin which you're about to see here.

Feature: Search
In order to find products dinosaurs love
As a website user
I need to be able to search for products
... lines 5 - 11

Here I'm just using human readable language to describe the search feature in general. Within each feature we'll have many different scenarios or user flows. So let's start with Scenario: Searching for a product that exists. Now using very natural language I'll describe the flow.

... lines 1 - 5
Scenario: Search for a word that exists
Given I am on "/"
When I fill in "searchTerm" with "Samsung"
And I press "search_submit"
Then I should see "Samsung Galaxy S II"

Don't stress out about the formatting of this, we'll cover that in detail.

The only two things that should look weird to you are searchTerm and search_submit because they are weird. searchTerm is the name attribute of this box here, and search_submit is the id of this button. We'll talk more about this later: I'm actually breaking some rules. But I want to get this working as quickly as possible.

Running Behat

Ready for me to blow your mind? Just by writing this one scenario we now have a test for our app. In the terminal run ./vendor/bin/behat and boom! It just read that scenario and actually went to our homepage, filled in the search box, pressed the button and verified that "Samsung Galaxy" was rendering on the next page. Why don't we see this happen? By default, it runs things using invisible curl request instead of opening up a real browser.

Testing JavaScript

The downside to this is that if you have JavaScript on your page that this scenario depends on, it isn't going to work since this isn't actually opening up a real browser. So, how can we run this in a real browser? There are actually a bunch of different ways. The easiest is by using Selenium.

Tip

With Symfony 5 it became easier! You can find the required instructions about Selenium configuration here. Or you can try a new library called Panther that is a bit easier than Selenium, but we're going to cover Selenium further in this course.

Grab another library with composer require behat/mink-selenium2-driver. You'll also need to download the selenium server which is really easy, it's just a jar file. Click this link here under downloads to get the Selenium Standalone Server. I already have this, so I'm not actually going to download it.

To run things in Selenium, open a new tab in your terminal, and run the jar file that you just downloaded. For me that's

java -jar ~/Downloads/selenium-server-standalone-2.45.0.jar

Tip

Firefox 47.0.0 and lower is not supported at all since Selenium 3.0.0 - update Firefox to the latest version and install the new geckodriver for it in order to use the latest Selenium server.

This will load and run as a daemon, so it should just hang there.

Our library is done downloading and we just need to activate it in our behat.yml with the line:

7 lines behat.yml
default:
extensions:
Behat\MinkExtension:
... lines 4 - 5
selenium2: ~

This gives me the option to use goutte to run the test using curl requests or Selenium to have things run in a browser. By default, this will just select goutte. So how do we make it use Selenium? I'm so glad you asked!

Above the scenario that you want to run in Selenium add @javascript:

... lines 1 - 5
@javascript
Scenario: Search for a word that exists
... lines 8 - 12

And that's it. Go back to the terminal and let's rerun this test. It actually opens the browser, it's quick but you can see it clicking around to complete the scenario. Cool!

Tip

FireFox is buggy with the new Selenium 3 server that's why it's preferable to use Google Chrome. You can explicitly specify the browser in the behat.yml config file:

# behat.yml
default:
  extensions:
    Behat\MinkExtension:
      browser_name: chrome

We write human readable instructions and they turn into functional tests, and this just barely scratches the surface of how this will change your development. Let's keep going and figure out what's really going on here.

Leave a comment!

122
Login or Register to join the conversation
Luis M. Avatar
Luis M. Avatar Luis M. | posted 4 years ago

Hi, are you going to update it to Symfony 4?

Thanks!

2 Reply

Hey Luis M.!

We may, but we don't currently have any immediate plans to do so. However, we would be happy to answer any questions you have!

Cheers!

Reply
Default user avatar
Default user avatar Simon Carr | posted 4 years ago

Using Windows 10 and Symfony 3.3. I have the following scenario


Feature: Supplier
In order to be able to work with suppliers
As a web user
I need to be able to see a list of suppliers

@javascript
Scenario: Listing all suppliers
Given I am on "/suppliers"
Then I should see "Suppliers"

When I run Behat, The browser opens and I quickly see the page before it closes, then I get the following error


@javascript
Scenario: Listing all suppliers # features\suppliers.feature:6
Given I am on "/suppliers" # FeatureContext::visit()
Then I should see "Suppliers" # FeatureContext::assertPageContainsText()
Element not found with xpath, //html
(WebDriver\Exception\NoSuchElement)

--- Failed scenarios:

features\suppliers.feature:6

1 scenario (1 failed)
2 steps (1 passed, 1 failed)
0m2.70s (11.65Mb)
2 Reply

Hey Simon,

Your scenario looks fine, which browser you run this scenario in? Google Chrome or Firefox? What's your version of Behat and what's your version of Selenium server? I think you just have some problems with compatibility, probably simple upgrade of selenium server could fix the problem.

Also, where's the "I should see..." step definition comes from? Is it from Behat core?

Cheers!

Reply
Default user avatar
Default user avatar Simon Carr | victor | posted 4 years ago

Browser if Firefox,
Here is a snippet from my composer.json

"behat/behat": "^3.4",
"behat/mink-extension": "^2.2",
"behat/mink-goutte-driver": "^1.2",
"behat/mink-selenium2-driver": "^1.3",

I am using selenium-server-standalone-3.7.1.jar

Then I should see "Suppliers" comes from the video You can see it at 4:43

Thanks
Simon

Reply

Hm, I was experienced a few weird things with Firefox. Could you try to use Google Chrome instead? I bet it'll fix your problem. IIRC, Firefox is just not ready to the latest Behat / Selenium server. The other things: your deps and version of selenium server - look good for me.

Cheers!

1 Reply
Default user avatar
Default user avatar Simon Carr | victor | posted 4 years ago

Thank you Victor, I will give that a go

Reply

Great! Feel free to contact us again if you still have some troubles with it. We'd be glad to help you to fix it ;)

Cheers!

Reply
Edward B. Avatar
Edward B. Avatar Edward B. | posted 2 years ago

To install on Symfony 5, I needed to fork the following four repos and update their composer.json files to allow Symfony 5 packages:

- Fork behat/mink
- Fork behat/mink-extension
- Fork behat/symfony2-extension
- Fork friends-of-behat/symfony-extension (remove package-builder conflict declaration)

My installation steps are:


symfony composer update
symfony composer req behat/behat:dev-master --dev
symfony composer req behat/mink:dev-master --dev
symfony composer req behat/mink-extension:dev-master --dev
symfony composer req behat/symfony2-extension:dev-master --dev
symfony composer req friends-of-behat/symfony-extension:dev-master --dev
vendor/bin/behat
symfony composer req phpunit --dev

The demo feature file works with the above setup but I've not gone beyond that point as I type this.

1 Reply
Default user avatar
Default user avatar Cristóbal Rejdych | Edward B. | posted 2 years ago

I follow your steps but when i try install (behat/mink:dev-master --dev) I get error message it requires symfony/css-selector version ^2.7 | ^3.0 | ^4.0 . I had installed css-selector 5.0.4 and I couldn't downgrade it to 4.0. Can you tell me how you resolve that conflict? Thanks for reply .

Reply

Hey Cristóbal Rejdych!

I haven't checked Ed's work, but in order for this all to work, you need to use Ed's fork of the GitHub packages instead of the real ones. For example, is fork of friends-of-behat/symfony-extension lives at https://github.com/ewbarnar... - and I talk a bit ab out how you would use a forked repository here: https://symfonycasts.com/sc...

Overall, relying in forked packages is not a great idea... the best is that we would get Symfony 5 support in Behat soon :/.

Cheers!

Reply
Jf senechal Avatar

Hello

I forked this repositories and update their composer files, but how can I install this in my application ?
Should i create packages ?

Reply

Hey Jf senechal

I don't fully understand your question. To make Behat work with this project you will have to first, download the course code from this page. Run composer install, and then follow the steps that Ed Barnard posted

Cheers!

Reply
Jf senechal Avatar

Hi Diego
it's for symfony 5
I follow the explanations of Ed Barnard https://disqus.com/by/disqu...
I fork the 4 projects mentioned.
I change the composer.json so that it is compatible with sf5
but i don't know how to install these forks in my app

Reply

Hey Jf senechal!

I haven't taken a look at Ed' work, but typically if you want to point Composer at a fork instead of the real repository, here is a comment explaining a bit about that: https://symfonycasts.com/sc...

Let us know if it works! And hopefully Behat will have proper Symfony 5 support soon.

Cheers!

Reply
Jf senechal Avatar
Jf senechal Avatar Jf senechal | weaverryan | posted 2 years ago | edited

Hi weaverryan

I added these lines to my composer.json
And all my tests work!
Yes very much that behat is compatible sf5


"repositories": [
{
"type": "vcs",
"url": "https://github.com/ewbarnard/Mink.git"
},
{
"type": "vcs",
"url": "https://github.com/ewbarnard/MinkExtension.git"
},
{
"type": "vcs",
"url": "https://github.com/jfsenechal/SymfonyExtension.git"
},
{
"type": "vcs",
"url": "https://github.com/jfsenechal/contexts.git"
}
]

Reply

Hey Edward B.!

Thanks for posting this :). This tutorial is on my list to "check" because I feared that it was totally incompat with Symfony 5... or at least you would need to do some magic to get it to work. I'll hopefully have some details soon to make this installable on Symfony 5 - some repositories may need a "poke" to release Symfony5-compatible versions.

Cheers!

Reply
Soltan Avatar

Hi, I have this error when trying to run feature file:

Could not open connection: Unable to create new service: ChromeDriverService
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:42:28.403Z'

Could you help to initialize the browser? Thank you

1 Reply

Hey Murad,

Hm, difficult to say.... try to re-install the chrome driver, on the internet I see it should help with this kind of error. If not, you can try to switch to the Firefox browser.

Cheers!

1 Reply
Soltan Avatar

How can I install geckodriver for Selenium 3 version?

1 Reply

Hey Murad,

From the docs https://firefox-source-docs... :
> Selenium users must update to version 3.11 or later to use geckodriver. Other clients that follow the W3C WebDriver specification are also supported.

Here's the page where you can find more info how to install it: https://firefox-source-docs... . As you can see, you can find an installation file from releases on GitHub, for example: https://github.com/mozilla/...

If you're on a Mac and use Brew - I think you can easily install it as "brew install geckodriver".

I hope this helps!

Cheers!

Reply
Soltan Avatar

And What I can use instead of SnippetAcceptingContext ?
Because it is not working any more

1 Reply

Hey Murad,

Instead of SnippetAcceptingContext I see you can use "--snippets-for", see deprecation message:
https://github.com/Behat/Be...

Cheers!

Reply
Soltan Avatar

I used
use Behat\Behat\Context\Snippet\Generator\ContextSnippetGenerator;

But when I'm implementing that one as here below
class LoginContext extends RawMinkContext implements Context, ContextSnippetGenerator

phpStorm asks interface reference

Reply

Hey Soltan!

So, the SnippetAcceptingContext was deprecated, but it has not been removed from Behat yet - so it *should* work. I would recommend either re-adding it, or using the "--snippets-for" option that Victor mentioned above.

Btw, don't use ContextSnippetGenerator - I don't know where you found that - but it has a totally different purpose :).

You said earlier:

> What I can use instead of SnippetAcceptingContext ? Because it is not working any more

What do you mean by it not working anymore? Do you run Behat but the snippets are not added to your class? Or do you get an error? Give us as much information as possible to help :).

Cheers!

Reply
Soltan Avatar

Thank you Victor,
Here is my dependencies list. What else or which one should I reinstall?
--
"require": {
"behat/behat": "^3.5",
"behat/mink-selenium2-driver": "^1.3",
"behat/mink-extension": "^2.3",
"vaimo/binary-geckodriver": "^2.0.1",
"tzander/page-factory": "dev-master",
"sensiolabs/behat-page-object-extension": "^2.1",
"vanare/behat-cucumber-json-formatter": "^1.1",
"bex/behat-screenshot": "^1.2",
"tonicforhealth/phpunit-extend-report": "^0.1.0",
"fzaninotto/faker": "^1.8",
"ext-json": "*",
"behat/mink-goutte-driver": "^1.2",
"shimabox/selenium-downloader": "^0.2.0",
"facebook/webdriver": "^1.6",
"spatie/phpunit-watcher": "^1.8",
"symfony/panther": "^0.2.0"
},

1 Reply

Hey Murad,

I does not mean Composer dependencies. Please, try to reinstall chrome driver in your OS. Btw, do you have chromedriver installed in your OS?

Cheers!

Reply
Sergey Z. Avatar
Sergey Z. Avatar Sergey Z. | posted 3 years ago

Hi there!
Can not install mink extension in Symfony 4.2:

composer require behat/mink-extension

Got error:
Problem 1
- behat/mink-extension 2.3.0 requires behat/mink ^1.5 -> satisfiable by behat/mink[v1.5.0, v1.6.0, v1.6.1, v1.7.0, v1.7.1].
- behat/mink-extension 2.3.1 requires behat/mink ^1.5 -> satisfiable by behat/mink[v1.5.0, v1.6.0, v1.6.1, v1.7.0, v1.7.1].
- behat/mink v1.7.1 requires symfony/css-selector ~2.1|~3.0 -> no matching package found.
- behat/mink v1.7.0 requires symfony/css-selector ~2.1 -> no matching package found.
- behat/mink v1.6.1 requires symfony/css-selector ~2.0 -> no matching package found.
- behat/mink v1.6.0 requires symfony/css-selector ~2.0 -> no matching package found.
- behat/mink v1.5.0 requires symfony/css-selector ~2.0 -> no matching package found.
- Installation request for behat/mink-extension ^2.3 -> satisfiable by behat/mink-extension[2.3.0, 2.3.1].

Tried to directly install symfony/css-selector 3.0:
composer require symfony/css-selector 3.0

Got this:

Restricting packages listed in "symfony/symfony" to "4.2.*"

[InvalidArgumentException]
Could not find package symfony/css-selector in a version matching 3.0

Can I change that restriction somehow? Could you give any advice how to install it on Symfony 4.2?

1 Reply

Hey Sergey,

Unfortunately, behat/mink package does not have a release for Symfony 4, see: https://github.com/minkphp/... . The only workaround to install it for now is to use master branch for it, like:
1. composer req behat/mink:dev-master
2. composer req behat/mink-extension

Or wait until a new behat/mink release that will support Symfony 4

Cheers!

1 Reply
Mickaël M. Avatar
Mickaël M. Avatar Mickaël M. | posted 1 year ago

Hi guys,
Is this tutorial is still valid for a symfony 5 project ?

Reply

Hey Micka,

Thank you for your interest in SymfonyCasts tutorials!

Unfortunately, it probably still works not perfect on Symfony 5. Well, Behat already supports Symfony 5, for Mink and other Behat extensions you may want to check https://github.com/FriendsO... organization that continue support for them, but the mink extension we show in this course looks like not maintained anymore. There may be more pitfalls with following this course on Symfony 5 I may not know so far, so I can't recommend you following it on Symfony 5. The best way would be to follow this course from downloaded course code - start from the start/ directory. If you want to follow it on a fresher Symfony version - I'd recommend you Symfony 4.4. We will try to rerecord this course on Symfony 5 in the future, but we don't have any certain plans doing this soon, so no any estimations yet when it might be re-recorded unfortunately.

I hope this helps!

Cheers!

Reply
Mickaël M. Avatar
Mickaël M. Avatar Mickaël M. | victor | posted 1 year ago

Thx Victor

Reply
Gonzalo G. Avatar
Gonzalo G. Avatar Gonzalo G. | posted 3 years ago

How to use it with docker, I have initialized a container with selenium, but I can't connect it with another container that has the php application inside. Any ideas?

Reply

Hey Gonzalo,

Hm, I don't know how it works internally, but we do use Docker containers on our CircleCI and it just works. As I understand, our base containers is "php" ( https://hub.docker.com/_/php ) and our Selenium containers is "selenium/standalone-chrome" ( https://hub.docker.com/r/se... ). So, I know little about Docker, but these two just work for us, though not sure what's done behind the scene of it. What Selenium container do you use? Try to use this one. I suppose, you just need to forward some ports, i.e. make them explicit and accessible from the Selenium container to your host machine, or maybe for your PHP containers instead, not sure.

As a workaround, I suppose you can just install the Selenium server manually on your PHP container and spin up it,

I hope this helps!

Cheers!

Reply
Rajaona F. Avatar
Rajaona F. Avatar Rajaona F. | posted 3 years ago

Hi, someone can help me ???
Could not open connection: Unable to create new service: ChromeDriverService

Reply

Is your web-server running? Also, remember that you need to start chrome and selenium in case you are using it

Cheers!

Reply
Rajaona F. Avatar
Rajaona F. Avatar Rajaona F. | posted 3 years ago

mink extension doesn't work in my environement
SF4.3 and php7.3
- behat/mink-extension 2.3.0 requires behat/mink ^1.5 -> satisfiable by behat/mink[v1.5.0, v1.6.0, v1.6.1, v1.7.0, v1.7.1].
- behat/mink-extension 2.3.1 requires behat/mink ^1.5 -> satisfiable by behat/mink[v1.5.0, v1.6.0, v1.6.1, v1.7.0, v1.7.1].
- behat/mink v1.7.1 requires symfony/css-selector ~2.1|~3.0 -> no matching package found.
- behat/mink v1.7.0 requires symfony/css-selector ~2.1 -> no matching package found.
- behat/mink v1.6.1 requires symfony/css-selector ~2.0 -> no matching package found.
- behat/mink v1.6.0 requires symfony/css-selector ~2.0 -> no matching package found.
- behat/mink v1.5.0 requires symfony/css-selector ~2.0 -> no matching package found.
- Installation request for behat/mink-extension ^2.3 -> satisfiable by behat/mink-extension[2.3.0, 2.3.1].

i think that the library work with older library ??? or ??? please help me ? i cant work with demo when installing dependency i have error continue 2 so i migrate in sf4 the source code but i have a pbm when i'm installing mink library, someone can help me please ?

Reply

Hey Rajaona F.

Try installing behat/mink@dev-master it's not recommended to install the dev-master branch but in this case they don't give us other option until they add compatibility with Symfony4 and since it's a dev dependency there is no much risk being taken

Cheers!

Reply

Hi !

I have an issue while running the scenario with the @javascript option (the same one as Leora had a year ago) :

Feature: Search
In order to find products dinosaurs love
As a web user
I need to be able to search for products


@javascript
Scenario: Searching for a product that exists # features\search.feature:7
Given I am on "/" # FeatureContext::visit()
When I fill in "searchTerm" with "Samsung" # FeatureContext::fillField()
Form field with id|name|label|value|placeholder "searchTerm" not found. (Behat\Mink\Exception\ElementNotFoundException)
And I press "search_submit" # FeatureContext::pressButton()
Then I should see "Samsung Galaxy" # FeatureContext::assertPageContainsText()


--- Scénarios échoués:


features\search.feature:7


1 scénario (1 échecs)
4 étapes (1 succès, 1 échecs, 2 ignorés)
0m6.74s (12.29Mb)

I've read every comment here. I have the same issue on Firefox or Chrome, for the first one, I've installed GeckoDriver, then I saw the last note, I installed ChromeDriver, and use the browser_name in my behat.yml. My Selenium Server is started.
I have no issue without the @javascript option.

Do I have to have everything working here to pursue on this course ? I'm most of the time working on APIs and back-end, I will probably, not in the near future, need to run tests on JS on front-end for that matter. I will need to work on a big API, and I'm really looking forward BDD.

I'm really not familiar with these tools, I may have made mistake while installing everything.

Reply
Ronald F. Avatar

I had the same problems with running Selenium in Docker. When I limited the version of selenium/standalone-firefox to 2.53.1 the tests went through. Seems like the Mink Selenium drivers have trouble with newer browser/Selenium versions. Not that surprising considering the last stable version is from 2016. The development versions didn't work either.

The strange thing is, that when i ran behat with the -v parameter it actually showed the correct html.

Reply

Hey Ronald,

Thank you for sharing the version that works for you. Yeah, with latest version it works not perfect. I'd recommend you to try to use Google Chrome instead of Firefox, it might work better with Behat nowadays.

Cheers!

Reply

Hey abyssweb!

Sorry for my slow reply! This is exactly when this type of testing gets tricky... because when something doesn't work... you're thinking "But why? What's missing?". One of my favorite debugging techniques is the "I break" technique shown here: https://symfonycasts.com/sc...

This will "freeze" the browser right before it fails... which sometimes can be useful to see what's going wrong - e.g. you discover that the button you're trying to click is missing or there's an error on the page (things that are difficult to spot if the page just disappears immediately when it fails). I might also try commenting-out this line and seeing if the NEXT line works or also fails.

Let me know what you find out! You could definitely also skip this part, and see if things start working in a bit. Overall, this *could* be a browser/driver setup issue... or just some other problem (like an error on the page, for example). The first type is a bit harder to debug :/.

Cheers!

Reply

Thank you for your answer.
I actually passed on this part of the course. I just watched it, and not practiced it.

I'm studying different testing technology because I have a big project coming up (a big API which will replace an old webapp), and I don't need to know how to test JS for this one.

I don't have the time to use more than one technology. I barely have the time to implement one, but I prefer to be late for a "good" reason, than on time and discovering flaws afterwards.
I don't know if I will go for Behat, PHPUnit, or phpscpec. I'm following your course on PHPUnit at this time, and will folliow with the one on phpscpec next. I liked how you user Behat on the REST API courses though.

I will try again later, when I could, to make Mink work correctly, and I will try your suggestions. :)

Reply

Hey abyssweb!

> I don't have the time to use more than one technology. I barely have the time to implement one,

I get that :p.

> I don't know if I will go for Behat, PHPUnit, or phpscpec. I'm following your course on PHPUnit at this time, and will folliow with the one on phpscpec next. I liked how you user Behat on the REST API courses though.

PHPUnit will be your easiest option if you want to implement tests on your API. That's simply because it's the "lowest-level" tool - there's no BDD or magic at all, which has some negatives, but also lowers the barrier to entry. Using Behat is probably the highest barrier to entry with the API stuff (even though it's fun to use it). A middle-ground would probably be to use PHPUnit, but in a BDD kind of way. You wouldn't have feature files or scenarios (sadly), but you still could think of each test class as a "feature" and each method as a "scenario" (and you could even name that method in a descriptive way to show what "thing" you're API user is actually trying to accomplish in that test). By building a base "test class" with your own helper methods, you can also help make your tests more descriptive - e.g. instead of something like $client->request('POST', '/login'), you would say $this->login(), which is shorter... but also more descriptive.

Anyways, take that advice or leave it - it's all up to what you want and "can" take on :).

Cheers!

Reply

Hi Ryan,

I'd like to extend Behat\Mink\Driver\Selenium2Driver so that I can add a few methods and still have access to the public and protected methods that the class offers.

Is there a way to achieve this in behat.yml?

I'm using Symfony 4 and in my behat.yml I have:

default:
extensions:
Behat\MinkExtension:
browser_name: chrome
base_url: http://theatre.localhost
javascript_session: selenium2
goutte: ~
selenium2: ~
Behat\Symfony2Extension:
kernel:
bootstrap: features/bootstrap/bootstrap.php
class: App\Kernel
suites:
default:
contexts:
- FeatureContext:
kernel: '@kernel'
- DebugContext
- Behat\MinkExtension\Context\MinkContext

I came up with this to execute private methods on the driver but it's messy:

public function executeMethodOnDriver($driver, $methodName, ...$params)
{
$closure = function (Selenium2Driver $driver) use ($methodName, $params) {
return $driver->$methodName(...$params);
};

$closure = Closure::bind($closure, null, $driver);

return $closure($driver);
}

for example:

  /**
* @Given I test xpath
*/
public function iTestXpath()
{
$driver = $this->getSession()->getDriver();
$xpath = '//div[contains(@class, "address")]';
$source = $this->executeMethodOnDriver($driver, 'findElement', $xpath);
}

I suppose I could extend RawMinkContext and then use __call as a proxy to executeMethodOnDriver.

Kind regards,

Nigel

Reply

So,

I realised that even if I could extend Behat\Mink\Driver\Selenium2Driver it would fail because there are so many private methods that the extending class would not have access to.

So I changed behat.yml to include this:

default:
extensions:
App\Tests\Behat\MinkExtension\ServiceContainer\CustomMinkExtension:
browser_name: chrome
base_url: http://theatre.localhost
javascript_session: selenium2e
goutte: ~
selenium2e: ~

I extended MinkExtension:

class CustomMinkExtension extends MinkExtension
{
public function __construct()
{
$this->registerDriverFactory(new ExtendedSelenium2Factory());
MinkExtension::__construct();
}
}

I extended Selenium2Factory:

class ExtendedSelenium2Factory extends Selenium2Factory

getDriverName return 'selenium2e';

buildDriver (array $config) to build ExtendedSelenium2Driver instead of Selenium2Driver

and

Just copied the whole of Selenium2Driver into ExtendedSelenium2Driver and worked from there.

I think that this is the only way?

Kind regards,

Nigel

Reply

Hey Nigel,

Thank you for sharing your solution with others! Yes, it looks like you extended it correct, I don't see any other way to do it. Well, you can hack things by copy/paste that file to your project and do necessary changes in it, but it will be dirty, and this way you won't have updates from the library :/ So, your way is much more intelligent, but as you see it has some limitations.

I don't know why exactly you need to extend that Selenium2Driver, but if you think there's a bug or a missing feature - I'd recommend you to open an issue in its repository: https://github.com/minkphp/... - this should be the correct one. You can discuss your idea there, and maybe open a PR to implement it if people will agree on it.

Cheers!

Reply

Hi Victor,

Ultimately extending Selenium2Driver didn't work for me; as you said copying the the whole Selenium2Driver code into the extending class is bad practice and just extending the class does not grant access to the private methods or variables.

So what I finally did was create a Selenium2DriverDecorator:

From FeatureContext I create a new decorator:


private function getDecoratedDriver(): Selenium2DriverDecorator
{
return new Selenium2DriverDecorator($this->getSession()->getDriver());
}

In the Selenium2DriverDecorator constructor I assign the driver to a private property:


class Selenium2DriverDecorator

private $driver;

public function __construct(DriverInterface $driver)
{
$this->driver = $driver;
}

And then access methods and properties irrespective of their visibility via the __call and __get magic methods.

 

public function __call($methodName, $args)
{
$driver = $this->driver;
$closure = function (DriverInterface $driver) use ($methodName, $args) {
return $driver->$methodName(...$args);
};

$closure = Closure::bind($closure, null, $driver);

return $closure($driver);
}

public function __get($name)
{
$driver = $this->driver;
$closure = function (DriverInterface $driver) use ($name) {
return $driver->$name;
};
$closure = Closure::bind($closure, null, $driver);

return $closure($this->driver);
}

This allows me to treat the decorator as if it was the driver itself which makes the code more readable:

And I can achieve what I set out to do (simulate "dragenter" and "dragleave" events):


public function simulateEventAfterDragstart(
string $event,
string $sourceXpath,
string $destinationXpath): void
{
$source = $this->findElement($sourceXpath);
$destination = $this->findElement($destinationXpath);

$this->wdSession->moveto(array(
'element' => $source->getID()
));

$script = $this->getSimpleEventScript('dragstart');

$this->withSyn();
$this->executeJsOnElement($source, $script);


$script = $this->getSimpleEventScript($event);
$this->withSyn();
$this->executeJsOnElement($destination, $script);
}


private function getSimpleEventScript($event): string
{
return <<<JS_HEREDOC
(function (element) {
var event = document.createEvent("HTMLEvents");

event.initEvent("$event", true, true);
event.dataTransfer = {};

element.dispatchEvent(event);
}({{ELEMENT}}));
JS_HEREDOC;
}

It took me a while but I think that this is the best solution.

When I've got all the functionality I need I will suggest updates for the GitHub repository.

Kind regards,

Nigel

1 Reply

Hey Nigel,

Wow, that's complex! Glad you found a solution that kinda fits you, and thanks for sharing it with others! Good luck with it!

P.S. Good plan about sending a PR later ;)

Cheers!

Reply
Default user avatar
Default user avatar Marwen Ben Ahmed | posted 3 years ago

hello friends,
when i run command ./vendor/bin/behat i show this issue :
Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException)

and my behat.yml file has this content :

default:
extensions:
Behat\MinkExtension:
browser_name: chrome
base_url: https://front.manewco.lo
goutte: ~
selenium2: ~

and my package.json has this code :

"require-dev": {
"behat/behat": "^3.5",
"behat/mink-extension": "^2.3",
"behat/mink-goutte-driver": "^1.2",
"behat/mink-selenium2-driver": "^1.3",
"behat/symfony2-extension": "^2.1",}

any idea of this bug please ???
thank's ....

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial uses a very old version of Symfony. The fundamentals of Behat are still valid, but integration with Symfony will be different.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.4.0, <7.3.0",
        "symfony/symfony": "^2.7", // v2.7.4
        "twig/twig": "^1.22", // v1.22.1
        "sensio/framework-extra-bundle": "^3.0", // v3.0.16
        "doctrine/doctrine-bundle": "^1.5", // v1.5.1
        "doctrine/orm": "^2.5", // v2.5.1
        "doctrine/doctrine-fixtures-bundle": "^2.2", // v2.2.1
        "behat/symfony2-extension": "^2.0" // v2.0.0
    },
    "require-dev": {
        "behat/mink-extension": "^2.0", // v2.0.1
        "behat/mink-goutte-driver": "^1.1", // v1.1.0
        "behat/mink-selenium2-driver": "^1.2", // v1.2.0
        "phpunit/phpunit": "^4.8" // 4.8.18
    }
}