Struggling with selenium, splinter and phantomjs

If you’re trying to test with selenium and splinter, and getting failed tests in phantomjs but the tests work in chrome or firefox, try these settings:

Service arguments

browser = Browser(‘phantomjs’, service_args=[‘–ignore-ssl-errors=true’, ‘–ssl-protocol=any’, ‘–webdriver-loglevel=DEBUG’])

Maximize the window

browser .driver.maximize_window()

Or set your window size

browser .driver.set_window_size(1024, 1024)

Setup karma and jasmine for front end unit testing

Jasmine is a great unit testing tool for angular and for javascript. You have the ability to run your unit tests through chrome, firefox, phantomjs and several more.

For this post I’ll be setting up the jasmine environment to run through firefox, chrome and then the ghost browser: phantomjs.

Prerequisites

Make sure you have npm installed

Install karma and karma-cli globally (remove -g if you just want it locally)

npm install -g karma
npm install -g karma-cli

Setup your package.json file

In a new folder create your package.json file by running:

npm install init (insert your project details)

Install packages using npm

npm install karma –save-dev
npm install karma-jasmine –save-dev
npm install karma-firefox-launcher –save-dev
npm install karma-chrome-launcher –save-dev
npm install karma-phantomjs-launcher –save-dev

The –save-dev will indicate to npm that it should add this library as a dependency in your package.json file.

You will see the following content added to package.json

“devDependencies”: {
“jasmine-core”: “^2.3.4”,
“karma”: “^0.13.12”,
“karma-
chrome-launcher”: “^0.2.1”,
“karma-firefox-launcher”: “^0.1.6”,
“karma-jasmine”:
“^0.3.6”,
“karma-phantomjs-launcher”: “^0.2.1”,
“phantomjs”: “^1.9.18”
},

Add the karma configuration file

To generate your karma.conf.js file, run the following command:

karma init karma.conf.js

Then follow the following setup process:

Do you want to capture any browsers automatically?

PhantomJS
Chrome
Firefox

What is the location of your source and test file?

test/unit/**/*Spec.js

 

There is not file matching this pattern. (Add a test/unit/TestSpec.js, make sure you
have the folders test/unit set up)

Test that it runs

in your file, TestSpec.js add:

describe(‘Hello moo’, function () {
it(‘Goes baa’, function() {

});
});

Then run

karma start karma.conf.js

Phantomjs will run, then Chrome broswer will open up, Firefox browser will open up.

Karma should say:
PhantomJS 1.9.8 (Windows 8 0.0.0) is idle
Chrome 46.0.2490 (Windows 8.1 0.0.0) is idle
Firefox 41.0.0 (Windows 8.1 0.0.0) is idle
You will see in your command line: Executed 1 of 1 SUCCESS.

(If there’s no success you may have a typo in your test js file)

Done

You now have your testing environment configured.

Read about the types of tests you can write on the jasmine documentation website, http://jasmine.github.io/.

Install phantomjs on vagrant

Setup

Make sure you have the right stuff installed

sudo apt-get install nodejs nodejs-dev npm

Check you have nodejs installed by requesting the version

nodejs -v

Check you have npm installed by requesting the version

npm -v

npm errors

I kept getting errors from npm, eg:

Object #<Object> has no method ‘_extend’

I had to update my nodejs. When I checked the version of nodejs I had a 0.6.x version.

I struggled to update nodejs, but found this script online that helped:

sudo apt-get install curl
curl –silent –location https://deb.nodesource.com/setup_4.x | sudo bash –
sudo apt-get install nodejs

Which came from askubuntu.com. http://askubuntu.com/questions/49390/how-do-i-install-the-latest-version-of-node-js

Install phantom js

sudo npm install -g phantomjs

Commit just one file to git

Run this command if you would like to commit just one file to git:

git commit -m ‘commit comment’ file-path

With ‘commit comment’ being the commit message you want to add. And ‘file-path’ being the path to your file and the file name.

Adding a sqlite3 database in Windows

Edit your php.ini

Uncomment in php.ini by removing the semi-colon ‘;’ :

extension=php_sqlite3.dll

Save and restart your apache.

Some tutorials mention also adding:
extension=php_pdo.dll

But this didn’t work for me.

If you receive the error:

PHP Startup: Unable to load dynamic library ‘C:\xampp\php\ext\php_pdo.dll’ – The specified module could not be found.

Just comment out the ddl:

;extension=php_pdo.dll

Adding the sqlite3 database

Go to the folder you would like to add your sqlite database, then run

sqlite3

Make sure that the location of where your sqlite3 executable is located is added in your environment variables.

Which will start an sqlite3 interface up. In there type:

.open database.sqlite

With ‘database.sqlite’ being your database file name. Make it whatever you would like it to be.

To exit the interface click “ctrl + c”.

Now you will see a newly created file in your directory.

Adding to your Environment variables for windows

Environment variables for windows

Adding environment variables is important to know. Environment variables are values that can be stored on your computer that can then be referenced by you computer.

Scenario: Adding python to a windows pc

Setting up python on a windows pc, you will need to make sure there is an environment variable set up to reference the command “python”. This will point to the python executable on your pc.

To view all your environment variables on windows you do the following:

  • Right click on your “My computer” icon
  • Select advanced system settings
  • Click on environment variables

The most common environment variables to edit are the values in the “Path”. This will allow you to point to an executable in a file path.

If for example you have php installed on your pc, but you cannot run php in the command line, you need to update or add the correct filepath to point to the php executable, which is often here: C:\xampp\php

Returning to python, I have python installed here: C:\Python27\Scripts. So I have made sure this file path is in my environment variable “Path”.

Remember to seperate multiple paths with a semi colon ‘;’.

Eg

C:\Python27\Scripts; C:\xampp\php;

This will remain the same for any program you want to run in your command line. If the file path is not in your environment variables you won’t be able to run the program from your command line.

Open a new command line window

You need to open a new command line instance in order to use the environment variable. You won’t be able use the path reference in the same command line window.

Use the command line tool to scaffold a wordpress site on windows

Pre-requirements

Make sure you have apache and phpmyadmin installed. You can download and install xampp here, which will provide you with both and more.

https://www.apachefriends.org/download.html

You will also need an internet connection.

This is easier if you use Linux, but if you prefer using Windows here is a quick guide.

Get wp-cli.phar

Follow the online instructions to get wp-cli.phar:

http://wp-cli.org/

If that doesn’t help try these alternative install methods:

https://github.com/wp-cli/wp-cli/wiki/Alternative-Install-Methods

Add the wp-cli.phar file to your system. I have a bin folder on Windows that I add any .phar files to.

C:\bin\wp-cli.phar

To run the wp-cli.phar on windows you will need to use the following command:

php [filepath]\wp-cli.phar

With [filepath]\ being the path to the location of your wp-cli.phar file.

Eg:

php C:\bin\wp-cli.phar

Check that the wp-cli.phar is working:

Run

php C:\bin\wp-cli.phar –info

Add a database

Go to your phpmyadmin, and add a blank database for your new wordpress to use. Typically your phpmyadmin will be here: http://localhost/phpmyadmin.

Just click on the “Databases” tab and create a new database called wp-test.

Add the mysql.exe filepath to your environment variables so that the command line can run mysql. You also need to have the php.exe filepath in your evironment variables;

Eg I added the below to my PATH variable in my evironment variables:

mysql

C:\xampp\mysql\bin;

php

C:\xampp\php;

Remember to separate each file path with a semicolon ;.

Read this post if you need to learn how to add to your environment variables.

Change to your wordpress directory

Go to the directory you want to add wordpress to. This will need to be on a server like apache. If you have xampp installed add wordpress in the htdocs folder.

Eg

cd C:\xampp\htdocs\wordpress\

This can be in any directory you like, just create the directory you want to install in and make sure you are in that directory in your command line.

Download wordpress using the command line

Step 1. Now that you have wp-cli.phar working download wordpress.

Run

php C:\bin\wp-cli.phar core download

The bonus of this approach is if you have already downloaded wordpress it will be cached on your system and the system will use that cached copy to install. You do not need to download it every time.

Setup the wp-config.php

Step 2. Every wordpress installation needs a wp-config.php file. Run the below to create the file.

Run

php C:\bin\wp-cli.phar core config –dbname=[db name] –dbuser=[db user] –dbpass=[db pass]

Eg

php C:\bin\wp-cli.phar core config –dbname=wp-test –dbuser=root

I have left out the –dbpass as I don’t have a password. By default in phpmyadmin the user name will be root and the password blank.

Getting an error?

If you get this error, you need to check that you have the mysql.exe location in your environment variables.

Error:
‘mysql’ is not recognized as an internal or external command, operable program or batch file.

You need to open a new command line instance in order to use the environment variable. You won’t be able use the mysql path reference in the same command line window.

Set wordpress up

Step 3. This is where you add in your admin user details and database details.

Run

php C:\bin\wp-cli.phar core install –title=’Test’ –admin_user=[your user name] –admin_password=[your password] –admin_email=[your email]  –url=[your wordpress url]

Eg

php C:\bin\wp-cli.phar core install –title=’Test’ –admin_user=admin –admin_password=securepassword –[email protected]  –url=http://localhost/wordpress/

Done.

You now have the ability to get wordpress up and running through the command line.