Selenium + ant + testng (SAT) Framework

Source: Internet
Author: User
Tags glassfish testng
Architecture

 

This framework is created using ant, testng and selenium.
Through this framework, a user is able to create an automatic test case which can be run later by executing a single command. The uses of different frameworks are as follows:
Selenium: this framework is needed for recording a testcase, exporting it into Java, and executing it on a browser
Testng: this framework is used to organize different tests, and to report results.
Ant: Provides the glue to hold everything together, and execute basic commands.

 

Pre-requisites:


Basic knowledge of selenium IDE, selenium RC, testng and ant.

 

 

Setup:


1. Install selenium ide on your Firefox browser.
2. Make sure you have ant installed.
3. Grab the testws.zip file from here. Unzip it into a directory of your choice.

 

About the sample
There are 2 bundled samples:
1. adminlogintest. java: This test logs into the glassfish Admin console using the username/password: admin/administrator. it then clicks on the 'Enterprise applications 'link, and tests to see if there are no applications deployed. else, test will fail.
2. simpletest. Java: This test accesses http: // localhost: 8080/page, and tests whether the term "glassfish project" appears or not.

TheConfigDirectory containsTestng. xmlFile, and is a place holder for configuration files.
TheLibDirectory contains required jar files, and is a place holder for other library jars/zips.
TheTestDirectory contains test java files and is a place holder for other test files.
Once the tests are run,BuildAndTest-OutputDirectories will be created.
TheBuildDirectory contains compiled classes, and is a volatile directory that is deleted by the targetCleanInBuild. xmlFile.
TheTest-OutputDirectory contains result files, and is generated by testng.
Notes:
To run the bundled sample, follow the steps for "execute your test through ant" (see below ).
Currently, I face some issues running this on IE. I have successfully been able to run tests using Firefox.

 

How:


Write a test case
The basic structure that one has to follow to create a test case is:
1. Record A testcase Using Selenium IDE (Firefox browser only), export the test case into Java
2. Edit the test case-Java file, and add testng annotations
3. Execute the test case using ant

 

1.Record Test Using Selenium IDE and export it into Java:
I. Open the website you 'd like to test in Firefox browser
Ii. Start the selenium IDE from your Firefox browser
Iii. Ensure that the base URL in Firefox is the same as the website you want to test
Iv. Start clicking on the website. Selenium will record your actions.
V. When finished, do: file-> export test as...-> JAVA-selenium RC and save the Java file.

After exporting, your Java test case shoshould look like this:

2.Add testng to your test case:
I. Open the Java test file you just created in an editor
Ii. Add the following:
(A) To the import statements:
Import org. testng. Annotations .*;
(B) To variable declarations:

Private defaselselenium selenium;
Private Static string timeout_period = "10000 ";

(C) To the class:

@ Beforesuite (alwaysrun = true)
Public void setup (string browser ){
Selenium = new defaultselenium ("localhost", 4444, "* Firefox", "http: // localhost: 8080 ");
Selenium. Start ();
Selenium. Open ("http: // localhost: 8080 ");
}

@ Aftersuite (alwaysrun = true)
Private void stoptest (){
Selenium. Stop ();
}

(D) After each statement in the test that will take time for a page to load:

Selenium. waitforcondition ("Selenium. browserbot. isnewpageloaded ()", timeout_period );

(E) on top of the test method:

@ Test (groups = {"<your test group name> "})
E.g. @ test (groups = {"roller "})

(F) Edit page to be opened. make sure Arg to 'open' method of selenium points to the right context root e.g. if http: // localhost: 8080/<context_root> is the correct URL to test, then since we already have tried to open localhost: 8080 in setup, we only need to open the context root page.
E.g.
Selenium. Open ("<context_root> ");

Iii. Save the test file
(). (Optional) Update testng. XML file. testng uses this file to read which groups to execute and also to pass parameters to the test. this file is only required if build. XML file uses testng. XML file to run the test.
Note: In sample test encoded ded, testng. XML is used, and will need to be updated.

Iv. Ensure that the test file you created is mentioned in the build. xml compile target.

3.Execute your test through ant
I. Start domain required by your webpage. e.g. asadmin start-Domain
Ii. Start database if needed (not needed for bundled sample test)
Iii. Start selenium server e.g. CD testws/lib, Java-jar selenium-server.jar
Iv. Execute the command: "ant run" from 'testws '. You shoshould see a new Firefox browser window open up, and run through your test.
V. Check reports generated by testng under 'testws/test-output' directory. Open the index.html to see different output from the test.


Console output:


»
  • Printer-friendly version
  • 9429 visits
  • Login or register to post comments
  • Aditya_dada's blog

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.