Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Webdriver Laravel Package

instaclick/php-webdriver

PHP client for Selenium WebDriver, enabling browser automation and end-to-end testing from PHP. Control Chrome/Firefox/RemoteWebDriver, manage sessions, elements, waits, and actions, with support for Selenium Grid and popular testing frameworks.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require instaclick/php-webdriver
    

    Ensure your project uses PHP 5.3+ (or higher if possible).

  2. Basic Usage:

    use Instaclick\WebDriver\RemoteWebDriver;
    
    $host = 'http://localhost:4444/wd/hub'; // Default Selenium Grid URL
    $driver = RemoteWebDriver::create($host);
    
    // Example: Open a URL and find an element
    $driver->get('https://example.com');
    $element = $driver->findElement(WebDriverBy::name('q')); // Search input
    $element->sendKeys('Laravel' . \Instaclick\WebDriver\Keys::ENTER);
    
  3. First Use Case: Automate a login flow for a Laravel app’s admin panel:

    $driver->get(env('APP_URL') . '/admin/login');
    $driver->findElement(WebDriverBy::name('email'))->sendKeys('admin@example.com');
    $driver->findElement(WebDriverBy::name('password'))->sendKeys('secure123');
    $driver->findElement(WebDriverBy::cssSelector('button[type="submit"]'))->click();
    

Implementation Patterns

Common Workflows

  1. Page Object Model (POM): Encapsulate interactions in reusable classes:

    class LoginPage {
        private $driver;
    
        public function __construct(RemoteWebDriver $driver) {
            $this->driver = $driver;
        }
    
        public function login(string $email, string $password) {
            $this->driver->findElement(WebDriverBy::name('email'))->sendKeys($email);
            $this->driver->findElement(WebDriverBy::name('password'))->sendKeys($password);
            $this->driver->findElement(WebDriverBy::cssSelector('button[type="submit"]'))->click();
        }
    }
    
  2. Integration with Laravel: Bind the driver to Laravel’s service container in AppServiceProvider:

    public function register() {
        $this->app->singleton(RemoteWebDriver::class, function ($app) {
            return RemoteWebDriver::create('http://selenium:4444/wd/hub');
        });
    }
    

    Use dependency injection in controllers/tests:

    public function testLogin(RemoteWebDriver $driver) {
        $loginPage = new LoginPage($driver);
        $loginPage->login('admin@example.com', 'secure123');
    }
    
  3. Cross-Browser Testing: Dynamically switch capabilities:

    $capabilities = DesiredCapabilities::chrome();
    $capabilities->setCapability('browserName', 'firefox');
    $driver = RemoteWebDriver::create($host, $capabilities);
    
  4. Async Testing: Use WebDriverWait for dynamic content:

    $wait = new WebDriverWait($driver, 10);
    $wait->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::id('flash-message')));
    

Gotchas and Tips

Pitfalls

  1. Session Management:

    • Issue: StaleElementReferenceException if the page reloads or the element is no longer attached.
    • Fix: Re-find the element or use explicit waits:
      try {
          $element->click();
      } catch (StaleElementReferenceException $e) {
          $element = $driver->findElement(WebDriverBy::id('dynamic-element'));
          $element->click();
      }
      
  2. Headless Mode:

    • Issue: Some websites block headless browsers (e.g., Chrome in headless mode).
    • Fix: Use user-agent spoofing or emulate mobile devices:
      $capabilities->setCapability('goog:loggingPrefs', ['performance' => 'ALL']);
      $capabilities->setCapability('browserName', 'chrome');
      $capabilities->setCapability('chromeOptions', [
          'args' => ['--disable-blink-features=AutomationControlled']
      ]);
      
  3. Slow Tests:

    • Issue: Selenium Grid can introduce latency.
    • Fix: Use parallel testing with Dockerized Selenium nodes or reduce implicit waits:
      $driver->manage()->timeouts()->implicitlyWait(2); // 2 seconds
      
  4. XPath/CSS Selectors:

    • Issue: Fragile selectors break with UI changes.
    • Fix: Prefer data-testid attributes or unique IDs. Use By::xpath("//*[@data-testid='submit']").

Debugging Tips

  1. Logs: Enable verbose logging to diagnose issues:

    $driver->manage()->logs()->get('browser');
    

    Or configure the driver with logging:

    $driver = RemoteWebDriver::create($host, [], [
        'loggingLevel' => \Monolog\Logger::DEBUG
    ]);
    
  2. Screenshots: Capture screenshots on failure:

    try {
        $element->click();
    } catch (Exception $e) {
        $driver->takeScreenshot('screenshot.png');
        throw $e;
    }
    
  3. Network Conditions: Simulate slow networks for testing:

    $capabilities->setCapability('chromeOptions', [
        'args' => ['--net-conditions=Latency=200']
    ]);
    

Extension Points

  1. Custom Commands: Extend the driver with custom methods:

    class ExtendedWebDriver extends RemoteWebDriver {
        public function scrollToBottom() {
            $this->executeScript("window.scrollTo(0, document.body.scrollHeight);");
        }
    }
    
  2. Hooks for Laravel: Use Laravel events to trigger tests (e.g., after deployment):

    // In a service provider
    Event::listen('deployed', function () {
        $driver = app(RemoteWebDriver::class);
        // Run smoke tests
    });
    
  3. Docker Integration: Use docker-compose.yml to spin up Selenium:

    services:
      selenium:
        image: selenium/standalone-chrome
        ports:
          - "4444:4444"
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor