Monday, November 19, 2012

Selenium 2 Java Quick Setup

I had a seemingly random issue with a website where it would fail one out of x times. Since I haven't used Selenium 2 yet I thought I would run a Selenium test.

To get started in Selenium 2 and Java the quickest way is to use Selenium IDE and generate some tests. Then use the File -> Export Test Case As -> Java / JUnit 4 WebDriver Backend. After the test is created then just drop it into Netbeans or Eclipse. The first stumbling block I ran into was that I needed Both Selenium Client and Selenium Server Jars added. Without the Selenium Server Jar, I saw an unhelpful error that stated: "com/google/common/base/Function". After adding those 2 Jars, if you don't use the Eclipse or Netbeans test runners, make sure to add the JUnit Jar. Then you can run your test. Here is a shell that does nothing but run a test that was outputted from Selenium IDE.

package webdriversample;

import com.thoughtworks.selenium.Selenium;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverExample {

    private Selenium selenium;

    @Before
    public void setUp() {
        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://google.com";
        selenium = new WebDriverBackedSelenium(driver, baseUrl);
    }

    @Test
    public void googleSearchExample() {        
        selenium.open("?q=test");          
        Assert.assertTrue(selenium.isTextPresent("Google"));
    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }

    public static void main(String[] args) {        
        Result result = JUnitCore.runClasses(WebDriverExample.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }                
    }
}

Wednesday, September 26, 2012

Setting up Apache To Work with PHP and Zend Framework in Fedora for Development

To get started the first thing we need to do is install Apache and PHP.
yum install httpd php
You can setup Apache to run in any users public_html directory but I am the only one using my computer and I like to name my websites directory whatever I want so we have to create the folder and give Apache permissions to it:
mkdir ~/websites
chmod 711 /home/accessrichard
chmod 755 /home/accessrichard/websites
After that we have to set Apache to redirect our localhost traffic to the above website directory. Create your own Apache config file in /etc/httpd/conf.d
touch /etc/httpd/conf.d/accessrichard.conf
Add something similar to the following in the file we just created replacing your home directory name and your website folder name as appropriate. In the below example I am setting up 2 different websites, demo.dev and demo2.dev.
NameVirtualHost *:80
<VirtualHost *:80>
     ServerName demo.dev
     DocumentRoot /home/accessrichard/websites/demo/public

     SetEnv APPLICATION_ENV "development"

     <Directory /home/accessrichard/websites/demo/public>
         DirectoryIndex index.php
         AllowOverride All
         Order allow,deny
         Allow from all
     </Directory>
</VirtualHost>
<VirtualHost *:80>
     ServerName demo2.dev
     DocumentRoot /home/accessrichard/websites/demo2/public

     SetEnv APPLICATION_ENV "development"

     <Directory /home/accessrichard/websites/demo2/public>
         DirectoryIndex index.php
         AllowOverride All
         Order allow,deny
         Allow from all
     </Directory>
</VirtualHost> 
We also have to modify our etc/hosts file in order to redirect website traffic from demo.dev and demo2.dev to localhost as follows:
127.0.0.1               localhost.localdomain localhost demo.dev demo2.dev
::1             localhost6.localdomain6 localhost6
And finally within our website directory we will have our php .htaccess file redirect all traffic to our router index.php. In our public direcotry create the file .htaccess with the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Tuesday, January 3, 2012

Set MySql Timezone information on Linux

In order to use the MySQL CONVERT_TZ() function on Linux which converts a datetime from one timezone to another, MySql needs timezone definitions and offsets which is dependent on the operating system in use. Otherwise CONVERT_TZ() returns NULL values.

To do this on Linux, use MySQL's utility script, "mysql_tzinfo_to_sql".

Example usage:
mysql_tzinfo_to_sql /usr/share/zoneinfo 
This returns a sql script to run to setup the timezone information. The following will setup the timezone information directly:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
Now you can view your timestamps in local timezone:
SELECT CONVERT_TZ('2010-01-02 00:00:00', 'UTC', 'America/Chicago' );