Wednesday, December 29, 2010

Locating Elements with Same name


These are the buttons with same name





Here goes the links with same name

Link

Link


Program to locate this are


package file;

import org.openqa.selenium.server.SeleniumServer;

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

public class Twonames extends SeleneseTestCase {

private SeleniumServer seleniumServer;
private Selenium selenium = new DefaultSelenium( "localhost",4444,
"*chrome",
"http://localhost:8080/same.html");

public void setUp() throws Exception
{
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

public void SameName() throws Exception {
selenium.open("/same.html");
selenium.setSpeed("2000");
selenium.click("//input[@id='button'and position()=2 ]");
System.out.println("clicked 2nd button");
selenium.click("//input[@id='button'and position()=1 ]");
System.out.println("clicked 1st Button");
selenium.click("//a[@name='search' and position()=2]");
System.out.println("clicked 2nd Link");
selenium.isTextPresent("HI");
}


public void tearDown()throws Exception
{
selenium.stop();
seleniumServer.stop();
}

}


Tuesday, December 28, 2010

Creating test suites with Junit4

Test Suite can be created in JUnit 4 as

import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
//nothing
}


The @RunWith(Suite.class) gives opportunity to combine both JUnit 4 and JUnit 3 tests and test cases together:


@RunWith(Suite.class)
@Suite.SuiteClasses({
ExampleOfJunit3TestSuite.class,
ExampleOfJUnit3TestCase.class,
ExampleOfJUnit4TestSuite.class,
ExampleOfJUnit4TestCase.class})
public class BothJUnit4and3TestSuite {
}


The Both JUnit4 and 3 TestSuite runs all tests and test suites listed in @Suite.SuiteClasses.

Sunday, December 26, 2010

Configuring Hudson to run Selenium Test Suite

To configure Hudson, follow these steps:
1. Go to Hudson Home.
2. Go to “Manage Hudson”.
3. Click on “Configure System”.
4. Go down to “Selenium Remote Control”; in htmlSuiteRunner enter the location of Selenium Remote control.7. Click on “Save”.


Create the job that runs the HTML Suite

You need to create a job that includes a “SeleniumHQ htmlSuite Run” step.

1. Go to Hudson Home.
2. Click on “New Job”.
3. Enter a value for “Job Name”.
4. Select a Job type: “Build multi-configuration project (alpha)”.
5. Click on “Ok”.
6. In “Build”, click on “Add build step”.
7. Select “SeleniumHQ htmlSuite Run”.
8. Enter the variables :
Browser: the browser that will run the test suite. For example: *firefox
startURL: start URL . For example: “http://www.google.com”
suiteFile: suite file location. For example: [location]/TestSuite.html
resultFile: HTML result file location. For example: [location]/TestResults.html

Friday, December 24, 2010

Hudson Configuration Links

http://blog.infostretch.com/?p=328

http://stackoverflow.com/questions/3294386/hudson-step-by-step-guide-to-set-up-master-and-slave-machines

Monday, December 20, 2010

Stop selenium Server(java method and through browser)

There are two ways to stop selenium server,

java method:


public void tearDown() throws Exception {
selenium.stop();
if(seleniumServer != null)
{
try{
selenium.shutDownSeleniumServer();
seleniumServer .stop();
seleniumServer = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


Browser Method

http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer



Hope it helps!!!

Important Links to learn locating objects.

Learn css in locating objects

http://jsfiddle.net/Flack/G8QPB/

Learn XPath

http://zvon.org/comp/r/tut-XPath_1.html#Pages~All_elements%3A_*

Code to start and stop Selenium server with eclipse setup

This is the minimum code to start the server and shutdown.


package file;

import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.SeleniumServer;

public class Reset extends SeleneseTestCase {

private SeleniumServer seleniumServer;
private Selenium selenium = new DefaultSelenium( "localhost",4444,
"*chrome","http://");

public void setUp() throws Exception
{
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}


//Test methods shd go here

public void tearDown()throws Exception
{
selenium.stop();
seleniumServer.stop();
}

}
Eclipse Configuration

create new project->enter project name->next->libraries->add selenium server.jar and junit-4.8.2.jar and click finish.

Way to export the java program to eclipse IDE.

1)After recording test case,click on options->format->Junit 3
2)Copy the method and paste it in between setup and teardown method.
3)Run->run as Junit

You can see your test case running...Enjoy!!!

Sunday, December 19, 2010

Run selenium tests in IE 8 (windows 7)

In windows 7, it is not possible to run selenium tests in IE 8. However, there is a workaround. Go to IE 8, Tools->Compatibility view Settings and then check the Display all sites in compatibility view.

Now if you type *iexplore in your code, it will run perfectly in the browser.

Thursday, December 16, 2010

Creating Test Suite in TestNG

Create a Click class with 2 methods testClick1,testClick2 and Suite called ClickSuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">


import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;


public class Click {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.co.in/");
seleniumserver.start();
selenium.start();
}

@Test
public void testClick()throws Exception {
selenium.open("/");
selenium.windowMaximize();
selenium.click("link=regexp:Advanced [s|S]earch");
selenium.waitForPageToLoad("5000");
selenium.click("id=opt-icon");
System.out.println(selenium.getLocation());
selenium.click("link=Scholar");
Thread.sleep(10000);
}


@Test
public void testClick2() throws Exception {
selenium.open("/");
selenium.type("q", "selenium link locator");
selenium.click("btnG");
}

@AfterClass
public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();


}
}







Now make ClickTestSuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ClickTestSuite" verbose="3">

<test name="testClick">
<classes>
<class name="Click"></class>
</classes>
</test>

<test name="testClick2">
<classes>
<class name="Click"></class>
</classes>
</test>


</suite>




Hope it helps...