Tuesday, 18 October 2011

Example: Selenium and TestNG framework

Aim:To create java selenium Scripts using a IDE and execute them in TestNg Framework.

Difficulty level:Medium
What you will learn?
  • How to switch from IDE to TestNg Framework
  • Basics of the TestNg Framework Setup
  • Introduction to the build.xml
Tips:
  • Basic idea of Selenium IDE
  • Basic knowledge of Java
  • Basic knowledge of Ant
Pre-requisites:
  • Firefox and Selenium IDE installed  http://www.mozilla.org/en-US/firefox/new/
  • Download Ant package from http://ant.apache.org/. You will get a zip file.
  • Extract Ant package zip file in some place for example "C:\".
  • Set 'C:\apache-ant-*\bin' in environment path.
  • Create a new environment variable 'ANT_HOME' and provide it value 'C:\apache-ant-*'.
  • Download 'selenium-server-standalone-*.jar' from http://seleniumhq.org/download/
  • download testng.jar from http://testng.org/doc/download.html. Follow TestNG install
  • Install TestNG plugin into Eclipse. (This is required if you want to run TestNG scripts from Eclipse)
  • Open Eclipse IDE, click 'Help >> Install New Software'.
  • Write 'http://beust.com/eclipse' in 'Work With' field and save this with name as 'TestNG'.
  • Select 'TestNG' checkbox and finish installation of plugin.
Screenshot showing the directory structure:
















Commands used in example:
  • click()
  • waitForPageToLoad()
  • type()
  • start()
  • open()
  • stop()
Exercise Steps:
  1. Open Eclipse and create a java project i.e. TestNgProject.
  2. Create package 'com.example.tests' inside the project.
  3. Right click on project name and click 'New >> Folder' with name 'lib'.
  4. Put all the downloaded jar files into lib directory.
  5. Create a new java class under newly created package with name 'QuickTest.java' and paste code from below mentioned java script.
  6. Right click on project name and click 'Build Path >> Configure Build Path'.
  7. Select all jar files from project's 'lib' folder under 'libraries' tab.
  8. Right click on project name and click 'New >> File' with name 'build.properties'.
  9. Right click on project name and click 'New >> File' with name 'build.xml'.
  10. Right click on project name and click 'New >> Folder' with name 'config'.
  11. Right click on 'config' and click 'New >> File' with name 'single.xml'
  12. Start Selenium - open command prompt, go to the path where selenium-server-standalone is, and execute the command "java -jar selenium-server-standalone-*.jar" (replace * with version). 
  13. Execute the created Project via Ant tool -  open command prompt, go to the path upto project root folder, execute command "ant".
  14. See the Result

Conclusions
  • TestNg Framework and see it working with a java selenium Example
  • Familiar with end to end advanced Selenium Script Design for TestNg Framework.
  • Familiar with how to record a script using Selenium IDE and convert it to Java
Script: save the below code in build.properties file under TestNgProject folder. Replace the ws.home value as per your system path.

ws.home=D:/workspace/TestNGProject
test.dest=${ws.home}/bin 
test.src=${ws.home}/src
save the below code in build.xml in TestNgProject folder



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="S4" default="all" basedir=".">

<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property file="./build.properties"/>

<!-- Deletes the existing classes and report directory-->

 <target name="clean">
  <delete dir="{test.dest}" />
 </target>

 <!-- Creates the  build, docs and dist directory-->

 <target name="makedir" depends="clean">
  <mkdir dir="${test.dest}" />
 </target>
 
 <!--Create a classpath container which can be later used in the ant task-->

 <path id="test.classpath">
  <pathelement location="${test.dest}" />

  <fileset dir="${ws.home}/lib">
   <include name="**/*.jar"/>
  </fileset>
 </path>

 <!-- Compiles all the java files (including the usage of library for JUnit -->

 <target name="compile" depends="makedir">
  <javac srcdir="${test.src}" destdir="${test.dest}">
   <classpath refid="test.classpath" />
  </javac>
 </target>

 <taskdef name="testng" classpathref="test.classpath" classname="org.testng.TestNGAntTask" />

<!-- run -->

<target name="single" depends="compile">
<testng classpath="${test.classpath}:${test.dest}" suitename="SingleTestExecution">
<xmlfileset dir="${ws.home}/config" includes="single.xml"/>
</testng>
</target>


<target name="all" depends="single">
<echo>
ant run will execute the test
</echo>
</target>

</project>


save the following code in single.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="5"  verbose="1" name="SingleTestExecution" annotations="JDK">
<test name="Search_Book" junit="false">
<classes>
<class name="com.example.tests.QuickTest">
 <methods>
  <include name="testQuickTest" />
 </methods>
</class>
</classes>
</test>
</suite>

save the following code in QuickTest.java

package com.example.tests;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.regex.Pattern;

public class QuickTest extends SeleneseTestCase {
public DefaultSelenium selenium;


@BeforeMethod
public void startSelenium(){
selenium = new DefaultSelenium("localhost", 4444, "*chrome","http://foundation.time2test.co.uk/");
selenium.start();
selenium.open("http://foundation.time2test.co.uk/");
}

@AfterMethod
public void tearDown() throws Exception {
selenium.stop();
}
@Test public void testQuickTest() throws Exception {
selenium.open("/");
selenium.click("link=Log in");
selenium.waitForPageToLoad("30000");
selenium.type("id=user_login", "admin");
selenium.type("id=user_pass", "time4test");
selenium.click("id=wp-submit");
selenium.waitForPageToLoad("30000");
selenium.click("link=Posts");
selenium.waitForPageToLoad("30000");
selenium.click("link=Add New");
selenium.waitForPageToLoad("30000");
selenium.type("id=title", "This is new Post");
selenium.click("id=publish");
selenium.waitForPageToLoad("30000");
selenium.click("link=Log Out");
selenium.waitForPageToLoad("30000");

}
}


Helpful Video:




Video details:
1. in this video , we are going to record a Selenium IDE script and change it into JAVA code and then run on the TestNG Framework
2. OPen up Selenium IDE
3. click on Red Record Button to record a script
4. navigate to foundation.time2test.co.uk
5. Login using admin/time4test
6. Click on "add new"
7. Enter Title for post and click publish
8. Save the test case as "quicktest.html"
9. Now convert the .html into JAVA format
10. in Selenium IDE , goto File->Export Test Case As...->TestNG (remote Control)
11. Save the file as "quicqtest.java"
12. We have already created a Project Directory based on TestNG framework
D:\TestNGProject\
bin\
config\
Single.xml - contains xml entry to quicktest.java
lib\
selenium-server-standalone-*.jar
testng-*.jar

test\
Quicktest.java - pre-created test framework
build properties file - contains ws.home
build.xml
13. Now copy the test method in the recorded/converted script into \test\quicktest.java
14. now open single.xml and ensure that class name and method name as per the java file
15. Now open build.xml and match up the target name
16. Start the selenium server
17. open up new command prompt
18. cd d:\TestNgProject\
19. enter command >ant single
20. notice the selenium script execute
21. Goto Test-output directory
22. open up the results file
23. Green indicates that the script ran successfully.

4 comments:

  1. If we are using the ANT tool, shouldn't that be listed with the pre-requisites? Also some instructions on how to install it would be needed.

    ReplyDelete
  2. What does it mean by:
    Create any Project Structure based on TstNg Framework.
    Change in Project according to Java file
    Set the path of ant tool into System variable.

    ReplyDelete