Phpstorm Selenium

broken image


PhpStorm supports unit testing of PHP applications through integration with the PHPUnit testing framework.

Selenium is an open source tool used for Web Browser Automation. So far, Selenium has developed a lot and now transformed into API and can support many programming languages such as Java, PHP. Selenium IDE Is Dead, Long Live Selenium IDE! In August of 2017 Simon shared the bleak, hard news that as of Firefox 55 Selenium IDE would no longer work. Selenium IDE (or as we'll refer to it from here on out – the Legacy IDE) was, and is, dead. Since then quite a lot has happened with the IDE.

Before you start

Phpstorm Selenium

Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Configure local PHP interpreters and Configure remote PHP interpreters.

June 24, 2016 Gaurav Tiwari selenium automation, best java ide, best practices, eclipse, IDE, Intellij, intellij idea, JAVA, netbeans, Selenium webdriver As an automation tester, we spend a lot of time playing around the code, debugging it and reusing it. Here you can also match their total scores: 7.6 for Selenium IDE vs. 8.9 for PhpStorm. Or you can verify their general user satisfaction rating, 99% for Selenium IDE vs. 99% for PhpStorm. We suggest that you take some time to review their differences and decide which one is the better alternative for your organization.

When you first open IntelliJ you have the option to import a project from the initial dialog box. Click the Import Project link. Then select the pom.xml file. IntelliJ recognizes that the pom.xml file is a Maven project, and begins the process of importing the project from Maven. The defaults in this dialog are fine, so click the Next button.

Download and install PHPUnit

Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.

Download and install phpunit.phar manually

  • Download phpunit.phar from the PHPUnit Official website and save it on your computer:

    • If you need full coding assistance in addition to the ability of running PHPUnit tests, store phpunit.phar under the root of the project where PHPUnit will be later used.

    • If you only need to run PHPUnit tests and you do not need any coding assistance, you can save phpunit.phar outside the project.

Download and install phpunit.phar with Composer

  1. Inside composer.json, add the phpunit/phpunit dependency record to the require or require-dev section. Press Ctrl+Space to get code completion for the package name and version.

  2. Do one of the following:

    • Click the Install shortcut link on top of the editor panel.

    • If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

Click next to the package record in the composer.json editor gutter to jump to the corresponding Settings/Preferences page and configure PHPUnit manually.

Integrate PHPUnit with a PhpStorm project

If you use a local PHP interpreter, PhpStorm performs initial PHPUnit configuration automatically. In the case of remote PHP interpreters, manual PHPUnit configuration is required.

Configure PHPUnit automatically

Guru99 Selenium Webdriver

  1. Store the phpunit.xml or phpunit.xml.dist configuration file under the project root.

  2. Install PHPUnit with Composer.

PhpStorm will create the local framework configuration on the Test Frameworks page and the PHPUnit run/debug configuration.

Configure PHPUnit manually

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP | Test Frameworks.

    On the Test Frameworks page that opens, click in the central pane and choose the configuration type from the list:

    • In local configurations, the default project PHP interpreter is used, see Default project CLI interpreters for details.

    • To use PHPUnit with a remote PHP interpreter, choose one of the configurations in the dialog that opens:

  2. In the right-hand pane, choose the PHPUnit library installation type:

    • To use Composer autoloader, specify the path to the autoload.php file in the vendor folder. See Composer for details.

    • To run PHPUnit from phpunit.phar, download phpunit.phar, save the archive in the project root folder, and specify the path to it. For local configurations, you can download the archive by clicking the Download link. To use it in the current project, make sure a default PHP interpreter is defined.

      When you click , PhpStorm detects and displays the PHPUnit version.

  3. In the Test Runner area, appoint the configuration XML file to use for launching and executing scenarios.
    By default, PHPUnit looks for a behat.yml configuration file in the project root folder or in the config folder. You can appoint a custom configuration file.

    You can also type the path to a bootstrap file to have a PHP script always executed before launching tests. In the field, specify the location of the script. Type the path manually or click and select the desired folder in the dialog that opens.

    Note that you can also provide an alternative configuration and bootstrap file when editing a PHPUnit run/debug configuration.

Generate a PHPUnit test for a class

  1. Open the Create New PHP Test dialog by doing any of the following:

    • From the main menu, choose File | New. Then, choose PHP Test | PHPUnit Test from the context menu.

    • In the Project tool window, press Alt+Insert or right-click the PHP class to be tested and choose New | PHP Test | PHPUnit Test.

    • In the editor of the PHP class to be tested, position the caret at the definition of the class. Then, press Alt+Enter and select Create New Test from the popup menu. This way, you can generate a test for a PHP class defined among several classes within a single PHP file.

      To create a test for a certain method, position the caret within the method declaration. The chosen method will be automatically selected in the methods list of the Create New PHP Test dialog.

  2. The Create New PHP Test dialog opens.

    Provide the parameters of the generated test:

    • The test file template, that is, the template based on which PhpStorm will generate the test class. Make sure that PHPUnit<6 is selected in the Test file template list.

    • The name of the test class. PhpStorm automatically composes the name from the production class name as Test.php.

    • The folder for the test class file, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its psr-4 package prefix, or the directory value specified in the phpunit.xml configuration file.

      To specify a different folder, click next to the Directory field and choose the relevant folder.

    • The namespace the test class will belong to, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its psr-4 package prefix.

    • The production class methods to generate test method stubs for. Select the checkboxes next to the required production class methods. To include inherited methods from parent classes, select the Show inherited methods checkbox.

      PhpStorm will automatically compose the test methods' names as test. You can customize the code templates used for generating test method stubs on the Code tab of the File and Code Templates settings page.

After the test is created, you can navigate back to the production class by choosing Navigate | Go to Test Subject. For details, see Navigate between a test and its test subject.

If you are relying on the PHPUnit configuration file for providing the containing folder and namespace for the test class, make sure that it is selected on the Test Frameworks page as described in Integrating PHPUnit with a PhpStorm project.

Generate PHPUnit test methods

  1. Open the required test class in the editor and position the caret anywhere inside the class definition.

  2. Choose Generate in the context menu or press Alt+Insert. Then choose Test Method from the Generate list.

  3. Set up the test fixture, that is, generate stubs for the code that emulates the required environment before test start and returns the original environment after the test is over:

    • Choose Generate in the context menu or press Alt+Insert. Then choose SetUp Method or TearDown Method from the Generate list.

    For more details, see Fixtures on the PHPUnit Official website.

You can customize the code templates used for generating PHPUnit test methods on the File and Code Templates page of the Settings/Preferences dialog Ctrl+Alt+S. To quickly access this page, in the Generate list, select Edit template from the submenu of a method.

Run and debug PHPUnit tests

You can run and debug single tests as well as tests from entire files and folders. PhpStorm creates a run/debug configuration with the default settings and a launches the tests. You can later save this configuration for further re-use.

Run or debug PHPUnit tests

  • In the Project tool window, select the file or folder to run your tests from and choose Run '' or Debug '' from the context menu of the selection:

    PhpStorm generates a default run configuration and starts a run/debug test session with it.

Run or debug a single test

  • Open the test file in the editor, right-click the call of the test and choose Run '' or Debug '' from the context menu.

Phpstorm Selenium Tutorial

Run a selection of tests

  1. Open the target file in the editor, right-click the desired test target, that is, a class or a method being tested, and either choose Go To | Test or press Ctrl+Shift+T.

  2. From the popup menu, select the tests to be executed. For multiple selection use Ctrl and Shift.

  3. Press Ctrl+Shift+F10 to run the tests selection.

After a test session is over, PhpStorm automatically creates a run/debug configuration with its Test scope set to Composite. See PHPUnit for details.

Save an automatically generated default configuration

  • After a test session is over, choose Save from the context menu of the file or folder.

Run or debug tests through a previously saved run/debug configuration

  • Choose the required PHPUnit configuration from the list on the toolbar and click or .

Create a custom run/debug configuration

  1. In the Project tool window, select the file or folder with the tests to run and choose Create run configuration from the context menu. Alternatively, choose Run | Edit Configurations from the main menu, then click and choose PHPUnit from the list.

  2. In the PHPUnit dialog that opens, specify the scenarios to run, choose the PHP interpreter to use, and customize its behavior by specifying the options and arguments to be passed to the PHP executable.

Monitor test results

PhpStorm shows the tests execution results in the Test Runner tab of the Run tool window.

The tab is divided into 2 main areas:

How To Use Intellij

  • The left-hand area lets you drill down through all unit tests to see the succeeded and failed ones. You can filter tests, export results, and use the context menu commands to run specific tests or navigate to the source code.

  • The right-hand area displays the raw PHPUnit output.

Run PHPUnit tests automatically

You can have PhpStorm re-run tests automatically when the affected code is changed. This option is configured per run/debug configuration and can be applied to a test, a test file, a folder, or a composite selection of tests, depending on the test scope specified in this run/debug configuration.

  1. Run the tests.

  2. On the Test Runner tab, press the toggle button on the toolbar:

  3. Optionally, click the button and set the time delay for launching the tests upon the changes in the code:

In this series on Selenium 4, we are providing use cases and code samples to explore the upcoming changes to the WebDriver API.

In the previous blog post, we discussed Migrating to Selenium 4 and today we'll detail how to install Selenium 4 so that you can check out all of the new features!

While Selenium has various language bindings, Java and JavaScript are the most widely used, so this post will cover how to install these two bindings.

Supercharge Selenium with Applitools Visual AI

Get Started

Upgrading from Selenium 3 to Selenium 4

If you are already using Selenium with Maven, you can upgrade it to Selenium 4 by changing the Selenium version to 4. Refer to line 28 in the example below:

Embedding: https://gist.github.com/ShamaUgale/649056e1b9aaee89c197c8255735014d.js

If you are already using Selenium with Gradle, you can upgrade it to Selenium 4 by changing the Selenium version to 4 as shown on line 17 in the example below:

Embedding: https://gist.github.com/ShamaUgale/f95716c6055ed7e0c36446e4448832fe.js

Installing Fresh Instance of Selenium 4

Before beginning, make sure Java is installed on your machine and the JAVA_HOME environment variable is set in your system path.

The Selenium Java binding can be set up in two different ways: via build tools, or manually. I'll outline the steps for both. However, you only need to choose one approach.

Install Selenium via build tools

There are various Java build tools available to manage the build and dependencies; the most popular and widely used ones are Maven and Gradle.

Install and setup Maven

Maven is a build and dependency management tool for Java based application development which helps with complete build lifecycle management.

Maven is also available with the Java IDEs (IntelliJ, Eclipse, etc) as a plugin.

However, if you are not using the plugin, you can install Maven manually.

Download Selenium using Maven

Maven looks for a pom.xml file for information on project, configuration, dependencies and plugins etc.

Maven Central Repository is the place where all the dependencies/libraries for all versions can be found. We can search for a library and copy and paste the dependency into our pom.xml file to download them.

For example, Selenium 4 can be downloaded using maven by adding the dependencies in pom.xml as shown in the below sample.

Test Setup

You are now all set to write tests and run them through the IDE or through the command line. You can test the setup, by adding the following code snippet into a new Java class under the ‘src' directory.

Embedding: https://gist.github.com/ShamaUgale/7c057b0b2f37f240d749948c1c66fd99.js

Install and setup Gradle

Gradle is another popular build tool used for java based applications these days. It's open source and the build scripts can be written in Groovy or Kotlin DSL.

Gradle can be also installed as a plugin in Java IDEs (IntelliJ, Eclipse, etc).

However, if you are not using the plugin, you can install Gradle manually.

Download Selenium using Gradle

Gradle looks for a build.gradle file where all the dependencies, plugins and build scripts are written.

Maven Central Repository is the place where all the dependencies/libraries for all versions can be found for Gradle as well. We can search for a library and copy and paste the dependency from the Gradle tab into our build.gradle file to download them.

For example, Selenium 4 can be downloaded using gradle by adding the dependencies in build.gradle file as shown in the below sample.

Embedding: https://gist.github.com/ShamaUgale/f95716c6055ed7e0c36446e4448832fe.js

Test Setup

You are now all set to write tests and run them through the IDE or through the command line. You can test the setup, by adding the following code snippet into a new Java class under the ‘src' directory and build.gradle file in the project root directory.

Embedding: https://gist.github.com/ShamaUgale/7c057b0b2f37f240d749948c1c66fd99.js

Install Selenium manually

Download Selenium

The jar files for Selenium 4 Java bindings can be downloaded from the official Selenium downloads page.

It will be downloaded as a zip file, unzip it.

Selenium scripts can be executed on various platforms, namely Firefox, Chrome, Internet Explorer, Safari, Opera and Edge. To explore and install the drivers for each refer to documentation links under the 'Browsers' section on Downloads page.

Add Selenium to IntelliJ

Within IntelliJ:

Go to the File Menu Project Structure Modules Dependencies Click on ‘+' Sign -→ Select JARs or directories → Select all the Selenium jar files and click on OK button.

Also, add the downloaded browser drivers under the project directory. In our example we will add chromedriver to run our tests on Chrome browser.

Test Setup

Create a new Java file under the ‘src' directory and add this code:

Embedding: https://gist.github.com/ShamaUgale/7c057b0b2f37f240d749948c1c66fd99.js

JavaScript

You can download and manage the Selenium JavaScript bindings using npm.

Install NodeJs and NPM

As a prerequisite, NodeJs needs to be downloaded and installed on the machine. To verify the installation, use the below two commands

Both should give the current version installed as an output.

Download and install Selenium Webdriver

Using the below command we will install Selenium with npm package manager on your machine.

Additionally, to run the tests on the browsers (Chrome, Firefox and IE etc), browser drivers need to be downloaded and set in the system path.

The drivers can be downloaded from the Downloads page and the links can be found under the 'Browsers' section on the page. Windows users may add the driver path to the PATH variable under environment variables. Mac and Linux users need to add a driver path to the $PATH variable in the shell.

Once we have installed the Selenium libraries and set the path for the browsers, we need to add the package.json file to the project root directory. NPM requires this file to install the dependencies and plugins used to build the project along with running the tests.

package.json can be created either manually or running the below command which adds the file to the current directory it is run from.

Below sample package.json file can be referred for installing Selenium 4 and running our tests. If you are already using Selenium Javascript bindings, to upgrade to Selenium 4, change the version as shown on line 12 in the below sample build file.

Embedding: https://gist.github.com/ShamaUgale/292fdb8bea71215593ba5396e79033e8.js

Once you have updated the Selenium 4 dependencies in the package.json file, run the below command to install all dependencies defined in your package.json file under dependencies section.

Selenium Basics Tutorial

The test is written in 'googleSearchTest.js' file. Below is the sample code, which opens a Firefox browser, navigates to google.com and searches for 'Selenium 4' and hits ENTER.

Embedding: https://gist.github.com/ShamaUgale/c20bdc0dc70e19737e0dfcbe29d9d826.js

To execute the test use the below command

Summary

We went through the setup instructions for Selenium 4 for two most popular language bindings – Java and JavaScript with ready to use sample tests and build files.

Now that we are ready with the Selenium 4 setup on our machine, in the next post of this series, we will explore the newly added features offered in Selenium 4 and the benefits they provide with working code samples.





broken image