Author - Manju Kashyap Post Views - 372 views
Run Selenium

Run Selenium with PHP on local

  1. Create a folder for the php project and install the php-webdriver and php-unit library using composer as given in the following steps:
    • Create a composer.json file with the following code:
      {
      "require": {
      "php":">=7.1",
      "phpunit/phpunit":"^9",
      "phpunit/phpunit-selenium": "*",
      "php-webdriver/webdriver":"1.8.0"
      },
      "config": {
      "platform": {
      "php": "7.3"
      }
      }
      }

      Note: You can change the package versions as per your requirement.
    • Then open the command prompt and navigate to the project folder and run the following command:
      composer require (preferred) or
      composer update

      1. Type the above command and press enter.
      2. It will prompt for “Search for a package:” >> press enter
      3. It will ask to re-run the command with –dev >> type no and press enter

      Now it will install all the required packages and dependencies
      Selenium with php

    • It will create the vendor folder along with the composer.lock file, in the project folder
      vendor
  2. Download Selenium Server:
    • You will need to download the latest selenium web driver from Downloads|Selenium. By following this link, you will need to scroll down a bit and click the link as shown below:
      Download Selenium Server
    • After clicking the above link, a model/popup will be opened. You will again need to scroll down a little bit to get the required stable version. I will prefer to download the jar file only as shown below:
      download the jar file
    • Now place this jar file in a separate folder in the project folder as I placed it in the folder “selenium-drivers” in my project folder “Testing”
      page3
  3. Download the Browser web driver
  4. You can download the browser driver from the link Selenium Browser driver below:
    Selenium Browser driver

    • You will need to choose the driver version as per the browser version installed on your computer, to make it run properly.
    • Now extract the downloaded zip file to get the browser driver exe file and place this executable file in the same folder where you placed the selenium web driver, like below:
      selenium web driver
      Note: I will explain here the testing for Chrome browser only but you can do the same for other browsers as well like Firefox, Microsoft Edge, etc. There will be little few changes in the code accordingly.
  5. Enable Curl with PHP on Windows: To enable CURL on windows,
    • open the php.ini file from the location where php is installed. For example in our case, it is present under the “C:\Program Files\php-8.2.2-Win32-vs16-x64” folder as shown below:Enable Curl with PHP on WindowsSo right-click on the file and open it with any editor of your choice. You can use simple notepad++ for editing.
      If you have xampp installed on your machine, you can also open it from there by clicking on the Config button and then selecting the PHP(php.ini) option from the list as shown below:xampp

      However, XAMPP is not required for selenium testing for any web app.

    • Now, in the editor, enable CURL by uncommenting the line which is specifying the curl extension as below:
      curl extension
    • Along with the CURL, we will also need to enable other extensions which will be used for selenium testing, as below:
      extension=bz2
      extension=fileinfo
      extension=gd2
      extension=mbstring
      extension=openssl
      extension=pdo_mysql
      extension=pgsql
      extension=shmop
      extension=sqlite3
      extension=tidy
      extension=xmlrpc
      extension=xsl
  6. Now create a separate folder to contain the test classes like we will create the selenium-tests folder and will create a new file “GoogleSearchChromeTest.php” with the following piece of code:
    <?php
    require 'vendor/autoload.php';
    use PHPUnit\Framework\TestCase;
    use Facebook\WebDriver\Chrome\ChromeOptions;
    use Facebook\WebDriver\Chrome\ChromeDriver;
    use Facebook\WebDriver\Firefox\FirefoxDriver;
    use Facebook\WebDriver\Firefox\FirefoxProfile;
    use Facebook\WebDriver\Remote\DesiredCapabilities;
    use Facebook\WebDriver\Remote\RemoteWebDriver;
    use Facebook\WebDriver\WebDriverBy;
    class GoogleSearchChromeTest extends TestCase
    {
    protected $webDriver;
    public function build_chrome_capabilities(){
    $capabilities = DesiredCapabilities::chrome();
    return $capabilities;
    }
    public function setUp(): void
    {
    $capabilities = $this->build_chrome_capabilities();
    /* Download the Selenium Server 3.141.59 from
    https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
    */
    $this->webDriver = RemoteWebDriver::create('http://localhost:4444', $capabilities);
    }
    public function tearDown(): void
    {
    $this->webDriver->quit();
    }
    /*
    * @test
    */
    public function test_searchTextOnGoogle()
    {
    $this->webDriver->get("https://www.google.com/ncr");
    $this->webDriver->manage()->window()->maximize();
    sleep(5);
    $element = $this->webDriver->findElement(WebDriverBy::name("q"));
    if($element) {
    $element->sendKeys("Selenium Test");
    $element->submit();
    }
    print $this->webDriver->getTitle();
    $this->assertEquals('Selenium Test - Google Search', $this->webDriver->getTitle());
    }
    }
    ?>
  7. Following is the description of the test scenario that we are going to test using this file:
    Test Scenario – (Browser – Chrome, Platform – Windows 10)Navigate to the URL https://www.google.com/ncr
    Locate the Search text box
    Enter ‘LambdaTest’ in the search-box
    Click on the search button and assert if the title does not match the expected window title
  8. Now open the command prompt, you can use Visual Studio Code editor for the same, and type the following command and hit enter to run the chrome driver on port 4444:
    selenium-drivers\chromedriver.exe --port=4444
    open the command prompt
    Note: selenium-drivers\chromedriver.exe is the path of the chrome driver.
  9. Now, open another command prompt window and enter the following command to run the selenium tests present in the GoogleSearchChromeTest.php file:
    vendor\bin\phpunit --debug selenium-tests\GoogleSearchChromeTest.php
  10. You will see the chrome browser open and enter the mentioned keyword to search as expected:
    Selenium test
  11. You can also verify the result in the output in the command prompt as well:
    page11

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri