Monthly Archives: September 2015

Check to see if Selenium Server is already running in PHP via Windows command line ( or any java jar file)

The script below will find out if the Selenium Server (java jar file) is already running in windows.  It uses the Windows Management Instrumentation Command-line (wmic) to look at the processes that are running as “java.exe” and gets the command line that started them.  The php script than Iterates through the command line output and looks for  “selenium-server-standalone”.

If you’re not familiar with Selenium you can learn how to use it with PHP here!

Advertisements
Automating Browser Testing with PHP and Selenium

Automated Browser Testing via PHP using Selenium and FB PHP-Webdriver

Automating your browser testing is very easy with using PHP and Selenium.  Below is a diagram of how it all works and the steps on how to achieve controlling your Browser with PHP!

Automating Browser Testing with PHP and Selenium

Automating Browser Testing with PHP and Selenium

Steps to install the basic components

  1. Install PHP on your machine download the PHP executable and set its path up in your environment (not sure how to do that?).
  2. Install Composer which will be used to download the source code for the facebook web driver
  3. Download the Selenium Standalone Server here http://www.seleniumhq.org/download/ (Direct link: selenium-server-standalone-2.53.1.jar)
  4. Run the selenium server by double clicking the jar file or invoking it from the command line..  You could also run this in your php script if you put the jar file in the same directory as the php script: exec(“java -jar “.__DIR__.”\selenium-server-standalone-2.53.1.jar”);
  5. Create a composer.json file https://gist.github.com/phpdave/580e5666c1d78d12e66e
  6. Run composer install using the composer.json file – This will download the facebook web driver
  7. Create an example PHP script on your computer.  https://github.com/facebook/php-webdriver/blob/community/example.php
  8. Run the script on the command line C:\myscripts\> php example.php
  9. This should start your browser up and start running the example script code.  You could then use the web driver to screen scrape, enter in various input and take various actions that are needed to test your application out.  You’ll also have the power of PHP to do various things such as save the results of your test to a file and all this is done in PHP!  So you don’t have to learn another language.  You’ll also want to get comfortable with firefox’s developers tools so you can inspect an element and get a unique selector for that element so you know how to interact with that element.

Note:

  • Supported Platforms: Windows, OSX, Linux.  Not supported on the IBMi.  This makes sense since browsers run on a users machine not on the i.  Therefore to automate you may have to setup a Linux VM on a server and have it called to test.  Or you can run the code on your local machine http://www.seleniumhq.org/about/platforms.jsp

Referencee
www.seleniumhq.org/download/

Adding PHP formatter to Selenium IDE firefox plugin to allow copying and pasting commands in PHP format

  1. Install the plugin here: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/
  2. In Selenium IDE go to Options->Clipboard Formatting-> PHP (PHPUnit)

This will give you output like this:

$this->open("/Page1/");
$this->type("name=field2", "99999");
$this->click("name=Submit");

which you’ll need to modify into this format if your using the PHP FB webdriver:

$driver->get("/Page1/");
$driver->findElement(WebDriverBy::name('field2'))->sendKeys("99999");
$driver->findElement(WebDriverBy::name('submit'))->click();

Adding a user extension to selenium IDE to add a new function

  1. Modify the user-extension.js that the selenium ide is using and add a function like this that will do an random email:
Selenium.prototype.doTypeRandomEmail = function(locator) {
    /**
    * Sets the value of an input field to a random email id,
    * as though you typed it in.
    *
    * @param locator an <a href="#locators">element locator</a>
    */

    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    /* The following block generates a random email string */
    var allowedChars = "abcdefghiklmnopqrstuvwxyz";
    var stringLength = 8;
    var randomstring = '';

    for (var i=0; i<stringLength; i++) {
        var rnum = Math.floor(Math.random() * allowedChars.length);
        randomstring += allowedChars.substring(rnum,rnum+1);
    }

    // Append a domain name
    randomstring += "@example.com"

    // Replace the element text with the new text
    this.browserbot.replaceText(element, randomstring);
};
  1. Then you can just call the function like this:
$this->typeRandomEmail("id=email");

Setting it up on an Amazon Linux EC2 instance

Follow this example to setup PHP on EC2 if you don’t already have a linux box to play with

Open the ports 7055 and 4444 on your EC2 server instance’s security group inbound rules

SSH into your Linux box run:

References: https://lambda-linux.io/blog/2015/01/28/announcing-firefox-browser-support-for-amazon-linux/

PHP DB2 on IBMi error: MYTABLE in MYFILE not valid for operation. SQLCODE=-7008

UPDATE:  Make sure to check if i5_allow_commit is not 0 (No Commit *NC *NONE).  You can check by running this in your PHP code

var_dump(ini_get(‘ibm_db2.i5_allow_commit’));

If the value is not 0 you might fix the problem by setting it to 0 in /usr/local/zendsvr/etc/conf.d/ibm_db2.ini and doing an Apache restart on Zend Server going to the 5250 typing “GO ZENDSVR/ZSMENU” option 6, option 5.

In case you run into the issue where your SQL updates and inserts statements aren’t processing you may want to try appending “WITH NONE” to the SQL statement.

When I ran the update statement

UPDATE MYFILE.MYTABLE SET FIELD1 = ? WHERE FIELD2 = ?

I got

MYTABLE in MYFILE not valid for operation. SQLCODE=-7008

Its a new file created via SQL
When I added WITH NONE it works

UPDATE MYFILE.MYTABLE SET FIELD1 = ? WHERE FIELD2 = ? WITH NONE

Even hard coding the values created the same error.  The problem appears to be a commitment control / journaling issue with IBMi DB2 connection.  You could try to change the settings of your connection.