Friday, 14 October 2011

Replace value in XPath with Variable

Below is the sample piece of code for submitting the variable into XPath.
Selenium IDE
<tr>
    <td>store</td>
    <td>2</td>
    <td>a</td>
</tr>

Verify the A value
<tr>
    <td>echo</td>
    <td>${a}</td>
    <td></td>
</tr>

//Replace the hardcoded value with a
<tr>
    <td>click</td>
    <td>//*[@id='ctl00_Main_DropDown']/div/ul/li[${a}]</td>
    <td></td>
</tr>

In Selenium RC
String a = "2";
System.out.println(a);
selenium.click("//*[@id='ctl00_Main_DropDown']/div/ul/li[" + a + "]");

Monday, 10 October 2011

dragAndDropToObject


Selenium IDE doesn't support Drag and Drop by recording but it can support if we add manually. Below is the sample piece code for dragAndDropToObject.

Selenium IDE
<tr>
    <td>dragAndDropToObject</td>
    <td>//*[@id='ctl00_chMain_Period']/span</td>
//Locator value of element to be dragged
    <td>//*[@id='ctl00_chMain_Period']/ul/li[5]/span</td>
//Locator value of destination object
</tr>
Selenium RC
selenium.dragAndDropToObject("//*[@id='ctl00_chMain_Period']/span", "//*[@id='ctl00_chMain_Period']/ul/li[5]/span"); 




Friday, 7 October 2011

StoreAttribute

It is very important in automation to store the specific attribute to verify in next iteration.

Below script will explain how to use the  StoreAttribute command

<tr>
    <td>storeAttribute</td>
    <td>//*[@id='lst-ib']@name</td>
    <td>Attribute</td>
</tr>
<tr>
    <td>echo</td>
    <td>${Attribute}</td>
    <td></td>
</tr>

Let me know if you have further doubts.






Monday, 12 September 2011

Using Multiple @Test TestNG Annotations

Below is the script which is using multiple @Test TestNG Annotations..

import static org.testng.Assert.assertEquals;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import org.testng.annotations.*;
public class Second {
  
    public static Selenium selenium;
  
    @Test
      
        public static void sample_1() throws InterruptedException {
      
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
      
        selenium.start();
      
        selenium.open("http://www.google.co.in");
      
        System.out.println(a);
      
        selenium.type("id=lst-ib", "Testing");
          
        selenium.click("name=btnG");
      
        Thread.sleep(1000);

        }

    @Test
        public static void sample_2() throws InterruptedException  {

        assertEquals(selenium.getTitle(), "Testing - Google Search");

        Thread.sleep(1000);

        selenium.click("css=em");

        Thread.sleep(1000);

        assertEquals(selenium.getTitle(), "Software testing - Wikipedia, the free encyclopedia");

        Thread.sleep(3000);

        selenium.click("css=#ca-talk>span>a");      

        }
    }


Sample JUnit Test Suite

Test Suite using JUnit....
 

Test1.java
------------------------------------------------------------
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.*;

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

public class Test1 {

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

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

@Test
public void testText1() throws Exception {
selenium.open("URL");
//* Do the Operation

*/

}
}

/***********************
Test2.java
***************/

public class Test2 {

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

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

@Test
public void testText1() throws Exception {
selenium.open("URL");
//* Do the Operation

*/

}
}


/***********************

Finally The Test Suite

***************/

package com.Testscripts;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

public class TestSuite1 extends TestCase {

public static Test suite()
{
TestSuite nag = new TestSuite();

nag.addTestSuite( Test1.class);
nag.addTestSuite( Test2.class);
return nag;
}

public static void main(String arg[])
{
TestRunner.run(suite());

}
}
------------------------------------------------------------
Run the TestSuite.java script ...it will automatically run the tests(Test1 and Test2)

Selenium RC + JUnit Sample Script

Below is the sample script using JUnit 

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

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

public class yahoo{

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
    selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://");
    seleniumServer = new SeleniumServer();
    seleniumServer.start();
    selenium.start();
    }
@Test
    public void testText1() throws Exception {
    selenium.open("http://www.yahoo.com");
    selenium.windowMaximize();
    }
@After
    public void tearDown() throws Exception {
    selenium.stop();
    seleniumServer.stop();
    }
}

Different flavours of browsers

  *pifirefox
  *piiexplore
  *firefox
  *mock
  *firefoxproxy
  *chrome
  *iexploreproxy
  *iexplore
  *firefox3
  *safariproxy
  *googlechrome
  *konqueror
  *firefox2
  *safari
  *piiexplore
  *firefoxchrome
  *opera
  *iehta
  *custom