March 31, 2011

How to handle https://www.example.com sites

March 31, 2011 2
How to handle https://www.example.com sites




Install this add on in firefox...
https://addons.mozilla.org/en-US/firefox/addon/10246/


Then the https:// site will run in selenium rc


will update it later......

March 26, 2011

Working with AJAX pages or elements

March 26, 2011 0
Working with AJAX pages or elements
AJAX stands for Asynchronous JavaScript and XML

 AJAX allows the Web page to retrieve small amounts of data from the server without reloading the entire page. In AJAX driven web applications, data is retrieved from server without refreshing the page.

Using andWait commands will not work as the page is not actually refreshed. Pausing the test execution for a certain period of time is also not a good approach as web element might appear later or earlier than the stipulated period depending on the system’s responsiveness, load or other uncontrolled factors of the moment, leading to test failures. The best approach would be to wait for the needed element in a dynamic period and then continue the execution as soon as the element is found.


This is done using waitFor commands, as waitForElementPresent , which wait dynamically, checking for the desired condition every second and continuing to the next command in the script as soon as the condition is met.


Below is one example how to handle AJAX applications....


package newone;



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


public class Ajax extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@Before
public void setUp() throws Exception {

seleniumserver = new SeleniumServer();
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testAjax() throws Exception
{
selenium.open("http://ajaximpact.com/");
selenium.windowMaximize();
assertEquals("AJAX Tutorials, Examples, News, Events and much more", selenium.getTitle());
//Click on Products Link
selenium.click("Image12");
verifyEquals("AJAX PRODUCTS", selenium.getTitle());
//Waiting for Element until 60 seconds--we can change it to any value
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=FarPoint Spread for Web Forms")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

//After the link or element is present it will come out of for loop and execute the next command
selenium.click("link=FarPoint Spread for Web Forms");
selenium.waitForPageToLoad("30000");
assertEquals("Ajax Impact : FarPoint Spread for Web Forms", selenium.getTitle());
Thread.sleep(1000);
}
@After
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}

}


Hope this post will help you....while working with AJAX applications..........


GoBack Simulation

March 26, 2011 0
GoBack Simulation
How to Simulate the user clicking the "back" button on their browser??

Here is the code for that..

package newone;




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


public class Goback extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@Before
public void setUp() throws Exception {

seleniumserver = new SeleniumServer();
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testGoback() throws Exception
{
selenium.open("http://www.google.co.in");
selenium.windowMaximize();
assertEquals("Google", selenium.getTitle());
Thread.sleep(1000);
selenium.type("q", "testing");
Thread.sleep(1000);
//Simulates a user pressing and releasing a key (13 if for Enter button).
selenium.keyPress("btnG", "\\13");
Thread.sleep(1000);
/*goBackAndWait()
Generated from goBack()
   Simulates the user clicking the "back" button on their browser*/
selenium.goBack();
Thread.sleep(1000);
assertEquals("Google", selenium.getTitle());
Thread.sleep(1000);
}
@After
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}

}


Hope it will help you in simulating back button on browser....

Simulate Enter key press

March 26, 2011 8
Simulate Enter key press
Here is  a sample script how to simulate the Enter key press...

Here we go ...............

package newone;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;


public class Enter extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {

seleniumserver = new SeleniumServer();
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testEnter() throws Exception
{
selenium.open("http://www.google.co.in");
selenium.windowMaximize();
assertEquals("Google", selenium.getTitle());
Thread.sleep(1000);
selenium.type("q", "testing");
Thread.sleep(1000);
/*keyPress(locator, keySequence)
   Arguments:

       * locator - an element locator
       * keySequence - Either be a string("\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key), or a single character. For example: "w", "\119".
*/
   //Simulates a user pressing and releasing a key (13 if for Enter button).
selenium.keyPress("btnG", "\13");
Thread.sleep(1000);
}
@AfterClass
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}

}


Here 13 means Enter key.

Hope this will help you.............

March 25, 2011

How to Create Reports using JUnit

March 25, 2011 34
Create Reports using JUnit:


After running test scripts in eclipse it would be good if we generate reports. Junit is one the most popular unit testing framework around. 


Below are the steps to generate reports:


1. Right click on project and click on "Export" option




   


2. Expand "General" node and select "Ant Buildfiles", click on "Next" button




3. Select the project (let all default options), 
Junit output Directory ... F:\Automation\First\junit  
click on "Finish" button




4. build.xml file should be created under project folder




5.  Now we need to add junit jar and Tools.jar file in Ant's "Global Entires"




6. Select "Window" menu, click on "Preferences"





7. In "Preferences" window, expand "Ant" root node, click on "Runtime"





8. Click on "Classpath" tab, expand "Global Entires" 





9. Click on "Add External JARs" button



10. Navigate to your "eclipse" directory (where eclipse is installed). Select "plugins ->org.junit_3.8.2.v3_8_2_v20100427-1100" directory. Select junit.jar file. and add Tools.jar which is located under java/JDK/Lib

11. Now right click on build.xml file and select Run As -> Ant Build.."





12. In "Edit Configuration and launch" window, select "build", "tootltip (the script we want to run)" and "junitreprot" checkboxes and click on "Run" button









13. All your test scripts will run and finally report will be generated under the same project directory under "junit" folder. To view JUint folder please refresh.




14. Click on "index.html", you will see the report 



Hope you all enjoy this post.





March 8, 2011

Error while running selenium scripts on Windows 7 and IE8??

March 08, 2011 27
When you run your test script on Windows 7 and firefox it will run. IF you want to run the same test script using windows7 and IE it will give an error...

To avoid that error we need to change the security settings in IE8.

Steps:
Open IE 8
Go to Tools-->Internet options--->Security tab.
Uncheck the Enable protected mode check box.
Click Apply and OK.


Your problem will be solved.......


Hope this will help you...