Wednesday, February 2, 2011

Reusability of One Class in another Class in RC

Reusability

Objective:- To create a reusable script for Login and Logout which can be called in a main function

Procedure:- Here I will explain you step by step to create reusable Login and Logout script.


Step 1:

Create Login script As shown Below.

@Test(alwaysRun=true)
public void testlogin() {

selenium.type("Email", "mailid");
selenium.type("Passwd", "Passwd");
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("//*[@id=\":r7\"]"));
}


Step 2:

Create a code to invoke(start) selenium-server under “@BeforeTest” annotation as shown below.

@BeforeClass

@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})

public void startSelenium(String seleniumHost, String seleniumPort, String browser, String webSite) {

this.selenium= new DefaultSelenium(seleniumHost, Integer.parseInt(seleniumPort), browser, webSite);

this.selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");

this.selenium.open(webSite);
}


Step 3:

Create Logout script in same Login script using “@AfterClass” and “@Test” annotation as shown below.

@AfterClass(alwaysRun=true)
@Test
public void testlogout() throws InterruptedException {
selenium.click("//*[@id=\":r7\"]");
selenium.waitForPageToLoad("30000");
}


Step 4:
Create a script for stopping selenium-server and use it under “@AfterClass” annotation and use “dependsOnMethods” so that selenium-server should not stop before executing Logout method, as shown below.


@AfterClass
(dependsOnMethods="testlogout")
public void stopSelenium() {
this.selenium.stop();
}


The entire Reusable script will appear as shown below.

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

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

public class CopyOfGmail_login extends SeleneseTestBase{
Selenium selenium;

@BeforeClass

@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
public void startSelenium(String seleniumHost, String seleniumPort, String browser, String webSite) {
this.selenium= new DefaultSelenium(seleniumHost, Integer.parseInt(seleniumPort), browser, webSite);
this.selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");
this.selenium.open(webSite);
}

@Test(alwaysRun=true)
public void testlogin() {

selenium.type("Email", "");
selenium.type("Passwd", "");
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("//*[@id=\":r7\"]"));
}








@AfterClass(alwaysRun=true)
@Test

public void testlogout() {
selenium.click("signout");
selenium.waitForPageToLoad("30000");

}
@AfterClass
(dependsOnMethods="testlogout")
public void stopSelenium() {
this.selenium.stop();
}
}



Step 5:

Now Create a main script in a new Class. In Class use “extends” method for calling Login script, also use “dependsOnMethods=LoginMethodName” and”alwaysrun=true”,As shown below

dependsOnMethod: Test will not be executed until dependant Test get executed

alwaysRun=true: So that Test should not stop if particular method fails.


Lets take an example for Gmail application the script will be like this.

import org.testng.annotations.Test;


public class CopyOfGmail_Function extends CopyOfGmail_login{

@Test(dependsOnMethods="testlogin",alwaysRun=true)
public void testinbox() {
selenium.click("link=Starred");
selenium.click("link=Sent Mail");
selenium.click("link=Drafts ");
selenium.click("link=Personal");
verifyTrue(selenium.isTextPresent("There are no conversations with this label."));
selenium.click("//div[@id=':co']/div[1]/div");
selenium.click("link=Inbox");
verifyTrue(selenium.isElementPresent("//div[@id=':co']/div[1]/div"));

}

}








Step 6:

we need to create a xml file to run our scripts, it can be shown below.

suite name="My suite" thread-count="1"

parameter name="seleniumPort" value="4444"
parameter name="seleniumHost" value="localhost"
parameter name="webSite" value="http://www.gmail.com"
parameter name="browser" value="*iexplore"
test name="Simple example"

classes

class name="GmailLogout"

test

suite

These all things are to be written using the < > braces.

4 comments:

  1. thanku for posting ..but script is not running

    ReplyDelete
  2. while executing above script i am getting error as
    Parameter 'seleniumHost' is required by @Configuration on method startSelenium,how to rectify this error.

    ReplyDelete
  3. Garet Job, Thank You1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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