How to Use Selenium with PHP

How to use Selenium with PHP?

Try Following things

  1. Get Phpunit installed and working
  2. Also have JAVA sdk & jre on your pc.
  3. Now record test cases using selenium IDE.
  4. Export the testcases to php files.
  5. Using these exported functions create an library of test cases.
  6. Create suite which calls the functions/tests from library.
  7. Now to execute Start Selenium Server using java command.
  8. Using phpunit Execute the suite.

for refrence how to write these files click here and also try on git hub

How to use selenium with php on linux?

Thanks a lot Sam and othre for taking interest in this question.
After Lot of R&D finally I got it running by downgrading firefox version.

You can get it working with:
selenium-server-standalone-2.53.0.jar
AND
Mozilla Firefox 38.6.1.

On Centos6.5 Text Mode

I can't get element on PHP website with Selenium

The input tag is in iframe, in Selenium you will have to switch to iframe first and then you can interact with the input tag.

Please switch it like this :

WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "mainframe")))
product_import = browser.find_element_by_xpath('//*[@id="but_2"]')

You will have to import these :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

You will also have to make sure that the input is under just this iframe. There could be a possibility of nested iframes as well.

Also, iframe id has to be unique in HTMLDOM to make this to work.

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Also, I would say if id is unique please use id not xpath for this :

browser.find_element_by_xpath('//*[@id="but_2"]')

A way better approach would be with WebDriverWait element_to_be_clickable. Something like this :

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "but_2"))).click()

How do you run Selenium headless in PHP?

This has been improved in php-webdriver 1.11.0 (2021-05-03).

Start headless Chrome

$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments(['--headless']);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);

// Start the browser with $capabilities
// A) When using RemoteWebDriver::create()
$driver = RemoteWebDriver::create($serverUrl, $capabilities);
// B) When using ChromeDriver::start to start local Chromedriver
$driver = ChromeDriver::start($capabilities);

See php-webdriver wiki article for more Chrome examples.

Start headless Firefox

$firefoxOptions = new FirefoxOptions();
$firefoxOptions->addArguments(['-headless']);

$capabilities = DesiredCapabilities::firefox();
$capabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions);

// Start the browser with $capabilities
// A) When using RemoteWebDriver::create()
$driver = RemoteWebDriver::create($serverUrl, $capabilities);
// B) When using FirefoxDriver::start to start local Geckodriver
$driver = FirefoxDriver::start($capabilities);

See php-webdriver wiki article for more Firefox examples.

Selenium 2 (WebDriver) and Phpunit?

Quick update: phpunit does now support Selenium 2

  • https://phpunit.de/manual/3.6/en/selenium.html
  • https://phpunit.de/manual/4.8/en/selenium.html

At the time of writing, PHPUnit does not support Selenium 2.

php-webdriver from facebook allows the complete WebDriver API to be called from PHP in an elegant way. To quote:

Most clients require you to first read the protocol to see what's
possible, then study the client itself to see how to call it. This
hopes to eliminate the latter step.

It is used by starting up the Selenium 2 server, which provides the interface at localhost:4444/wd/hub.

/usr/bin/java -jar /path/to/selenium-server-standalone-2.7.0.jar

then running the PHP test code, which calls that interface. For example:

<?php

require '/path/to/php-webdriver/__init__.php';

$webdriver = new WebDriver();

$session = $webdriver->session('opera', array());
$session->open("http://example.com");
$button = $session->element('id', 'my_button_id');
$button->click();
$session->close();

The WebDriver API is mapped to PHP methods, compare calling click on element in the example with the element/click API call in the documentation.

The test code can then be wrapped in regular phpUnit tests.

This is not native phpUnit support, but it's a quite robust approach.

Why use Selenium with Laravel for testing?

You actually already got the answer. What you state is correct.

Laravel integrated tests will internally emulate the requests and may use some advantages (like Disabling Middleware, Mocking, Spying, etc) for making sure that you are isolating a particular problem (Test Case). The idea is to test the application without introducing third party components side effects into the battlefield, this would be: the client browsers, external services, etc. These type of tests are really fast and lightweight. Also very suitable for testing API calls.

So Selenium is exactly for covering all those cases, in wich you actually want to cover those scenarios affected by third party components side effects, like JavaScript in either Chrome, IE, Firefox, etc, even in the different versions of those. You can understand this like an attempt to be as close as possible to the real world scenario, where the client browser actually may interfere with the expected behavior of your application. Also, it's possible to trigger screenshots if you want to visually validate CSS or interactive components. It is important to mention that because of this browser hook, these tests are way slower to execute.

The takeaway of this should be that you don't need to either use one or the other exclusively. They work similarly, but in the end, they provide different capabilities. You can have a set of Laravel integration tests, and a set of Selenium tests for those things that matter to you.
I suggest you this Laracast

I may provide you an example in my project, not that relevant, but at least a way to show that both test types can coexist in the same project.

Hope this helps!



Related Topics



Leave a reply



Submit