Monday 30 September 2013

Selenium Testing Framework


When writing test scripts, you might find yourself trying to solve similar problems over and over again. So why not re-use the same solutions over and over again? The following framework will help you get the most out of your code without needing to do tons of maintenance every time something changes.
Before getting into specifics, lets first talk about what should be implemented when writing test scripts. At GLG, our tests typically go through a set of steps on the website. They also have to interact with elements on the site in order to accomplish these steps. Then there is the actual tests themselves, which review the displayed content on the site. Essentially, we have tests that assert the content on the site, a set of steps we need to take in order to perform these tests and then the pages with elements that we are checking or interacting with.
When drawn as an ERD, we have something like this:






How can we best make use of this structure? We want tests to focus on use cases, actions that focus on what steps to take, and pages that allow us to interact with them. From a development standpoint, this means we have tests containing a list of actions that can be used to obtain the values required to check or change the state of the system under testing. Actions can interact with pages in order to complete these steps and make sure they accomplished their task. Pages can be used by actions or tests to add, edit, clear, or read data. Adding these relationships to the ERD, we get something like this:







What does this look like in code? How would we best accomplish this task? Well, that depends on what you are doing. For example, maybe you need to test 5 different types of projects, or maybe you need to test same page differently each time. Either way, as a coding standard, you should always design classes with a specific purpose. Anything that is shared should go into a base class for common functionality.

Thursday 19 September 2013

Making a function in user extension to generate random email address



This is an example of how to send an email from a component. You would typically put this into your components controller.
Contents
·         1 Fetch the mail object
·         2 Set a sender
·         3 Recipient
·         4 Create the mail
·         5 Sending the mail
Fetch the mail object
A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.
$mailer = JFactory::getMailer();
Set a sender
The sender of an email is set with setSender. The function takes an array with an email address and a name as an argument. We fetch the sites email address and name from the global configuration. These are set in the administration back-end (Global Configuration -> Server -> Mail Settings).
$config = JFactory::getConfig();
$sender = array(
    $config->getValue( 'config.mailfrom' ),
    $config->getValue( 'config.fromname' ) );

$mailer->setSender($sender);
In 3.1, $config->getValue() should be changed to $config->get()
Recipient
You set the recipient of an email with the function addRecipient. To set the email address to the currently logged in user, we fetch it from the user object.
$user = JFactory::getUser();
$recipient = $user->email;

$mailer->addRecipient($recipient);
If we had multiple recipients we would put each recipients email address in an array.
$recipient = array( 'person1@domain.com', 'person2@domain.com', 'person3@domain.com' );

$mailer->addRecipient($recipient);
Create the mail
We need to set a subject line and create the text body. The subject is set with setSubject.
The easy way to create an email body is as a string with plain text. Use the function setBody to add a message to the mail body. You can also attach a file with addAttachment. It takes a single file name or an array of file names as argument.
$body   = "Your body string\nin double quotes if you want to parse the \nnewlines etc";
$mailer->setSubject('Your subject string');
$mailer->setBody($body);
// Optional file attached
$mailer->addAttachment(JPATH_COMPONENT.'/assets/document.pdf');
If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with IsHTML. When sending HTML emails you should normally set the Encoding to base64 in order to avoid unwanted characters in the output. The subject line and any attachments are handled as above, with the exception of images embedded in the HTML. These are taken care of with the function AddEmbeddedImage.
$body   = '<h2>Our mail</h2>'
    . '<div>A message to our dear readers'
    . '<img src="cid:logo_id" alt="logo"/></div>';
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
// Optionally add embedded image
$mailer->AddEmbeddedImage( JPATH_COMPONENT.'/assets/logo128.jpg', 'logo_id', 'logo.jpg', 'base64', 'image/jpeg' );
Normally you would leave any images on your server and refer to them with an ordinary HTML image tag, to reduce size of the mail and the time sending it.
Sending the mail
The mail is sent with the function Send. It returns true on success or a JError object.
$send = $mailer->Send();
if ( $send !== true ) {
    echo 'Error sending email: ' . $send->message;
} else {
    echo 'Mail sent';
}
You would probably want to write your own error handler, if there is an error sending the mail.