Skip navigation.
Home

Automatic Testing of Web Applications using Selenium

1. Introduction

In order to know if a website is working normally, doing manual test can implement this. But it’s not a good idea, it should be completed automatically. In order to do some automatic tests to monitor if the website is working, if it doesn’t work normally, the test system should inform us to know. The test system should run all the time automatically. Selenium is a good tool to complete the automatic test. It can be got from “http://selenium.openqa.org/”.

2. Main Productions Of Selenium


2.1. Selenium IDE

The quickest way to learn Selenium is via a Firefox plugin called Selenium IDE. Selenium IDE can be used conveniently, it just can be used by Firefox. It can not be used to do the automatic test because before doing test, the browser (Firefox) must be open, and the Selenium IDE should be open, all these should be completed manually. There is no loop. The following is an example to show how to use Selenium IDE.

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>New Test</title>
</head>
<body>
  <table cellpadding="1" cellspacing="1" border="1">
  <thead>
    <tr><td rowspan="1" colspan="3">New Test</td></tr>
  </thead>
    <tbody>
        <tr>
	  <td>open</td>
	  <td>http://www.google.com</td>
	  <td></td>
        </tr>
        <tr>
	  <td>type</td>
	  <td>q</td>
	  <td>tank</td>
        </tr>
        <tr>
	  <td>clickAndWait</td>
	  <td>btnG</td>
	  <td></td>
        </tr>
        <tr>
	  <td>verifyTextPresent</td>
	  <td>disambiguation</td>
	  <td></td>
        </tr>
   </tbody>
  </table>
</body>
</html>

Saving the above code as test.html, and import it into Selenium IDE to run. This code simulates:
1. Opening http://www.google.com.
2. Typing “tank” as the searching content.
3. Clicking the “Google Search” button and waiting the page to load
4. Verifying if the new page contains “disambiguation” text.

2.2. Selenium Core

Selenium Core tests run directly in a browser, just as real users do. And they run in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh. Because it is implemented by javascript, so the test result can not be written to the disk.

2.3. Selenium RC

It can be used to test web applications by any language, such as Java, .NET, Perl, Python, Ruby and so on, it’s easy to do the automatic test by using a closed-loop. The following is a example which is implemented by java.

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class TestSample {

	public static void main(String[] args) {
		//Inistal the Selenium
		String url = "http://www.google.com";
		Selenium selenium = new DefaultSelenium("localhost",
                            4444, "*iehta", url);
		//Start the Selenium
		selenium.start();
		//Open the website
		selenium.open("http://www.google.com/");
		//Get the tetle of the open website
		String firstTitle = selenium.getTitle();
		System.out.println("firstTitle:" + firstTitle);
		//Type "Selenium OpenQA" as the content to search
		selenium.type("q", "tank");
		//Click the "Google Search" button
		selenium.click("btnG");
		//Wait for the page to load
		selenium.waitForPageToLoad("5000");
		//Print if the new page contains "disambiguation" text
		System.out.println(selenium
                            .isTextPresent("disambiguation"));
		//Stop the Selenium
		selenium.stop();
	}
}

The above example does the same simulation that the example of selenium IDE dose. In order to run the program, we should start the selenium server first, and then import selenium-java-client-driver.jar to the java object.

3. Website Test

3.1. Test Environment

Windows, selenium-RC-0.9.2, IE Browser, activation.jar, mail.java, selenium-java-client-driver.jar

3.2. Main function

When the windows system starts, it should automatically start the selenium server, run the test project all the time. So we use .bat file to start the selenium server and the java program. Every time we complete a whole test, if there is any error, it should be recorded to the log file .When the website doesn’t work normally, the system should send us an e-mail with the log file to notify us. The system does the following process:
1. Start the selenium server.
2. Start the test program.
2.1 Sleep for a while to wait the selenium server starts.
2.2 Init the selenium class.
2.3 Do the test.
2.4 If there is any error, send an e-mail with the log file to notify us. If there is no error, there is no e-mail.
2.5 Backup the present log file if it is not empty, delete the log files which are longer than 30 days.
2.6 Stop the selenium class.
2.7 Sleep for 30 minutes
2.8 Go to 2.2 to go on to do the test.

3.3. deployment

The deployment of this project is as following:
1. Pack the whole project as a SeleniumTest.jar(sure you can use another name if you like.).
2. make a file named “tmp.properties”(this file name can not be changed),it is used to store the variables as following:
logFileFullPath:
It is the full path of the log file.
logFileDir:
It is the directory used to store the log file. Because the test system will check the log files, then it will delete the files which are longer than 30 days. This directory had better be a special directory which is only used to store the log files.
logFileName:
It is the name of the log file.
startSleep:
When the operating system starts, both the selenium server and the java program would be started, and starting the selenium server will spend a short time, the java program have to wait until the selenium server have been started, then the selenium test can be started normally. So at first the java program has to wait for a while, after that it can init the selenium class successfully.
sleepInterval:
This is the interval test time. Every time it completes a whole test process, it would sleep for this interval time.
mail.transport.protocol:
Specifies the default message access protocol.
mail.smtp.host:
The SMTP server to connect to.
mail.smtp.port:
The SMTP server port to connect to, if the connect() method doesn't explicitly specify one. Defaults to 25.
mail.smtp.auth:
If true, attempt to authenticate the user using the AUTH command. Defaults to false. During this project, it must be true.
username:
It is the user name which makes you can use the mail server to send mails.
password:
It is the password of the user name.
from:
It is your mail address which shows the one who send a mail.
to:
It is the mail address who you want to send mails when there is any problem with the test.
The example will be like the following:

logFileFullPath=D:\\Documents\\Public\\selenium\\runjava\\log\\test.out
logFileDir=D:\\Documents\\Public\\selenium\\runjava\\log
logFileName=test.out
startSleep=100
sleepInterval=1800000
mail.transport.protocol=smtp
mail.smtp.host=mail.decisci.com
mail.smtp.port=25
mail.smtp.auth=true
username=yourusername@decisci.com
password=yourpassword
from=brad@decisci.com
to=brad@decisci.com

Note: The separator of the file path should be “\\” or “/”, otherwise the java program can’t recognize it.

3. Put all the jar files and bat files into a directory, set the bat files correctly. The content of one bat file is “java –jar filePath\selenium-server.jar”, the filePath is the path of the selenium server which is used to start the selenium’s server. The content of the other bat file is “java –classpath jarFilepath/activation.jar; jarFilepath/mail.jar;…;SeleniumTest.jar selenium.test.SeleniumTest”, the jarFilepath is the directory where you store the jar files.

4. Create the shortcuts for the two bat files, then drag them to start menu->all programs->Startup, every time when the operating system starts, the two bat files will be started, the selenium server and the java program also will be started.

The file structure is like the following:

AttachmentSize
4.jpg31.49 KB

addgfed

Hello! aeecdaa interesting aeecdaa site!

dekddad

Hello! gkfaeba interesting gkfaeba site!

gbggedg

Hello! fcgebdk interesting fcgebdk site!

Latest image