Pages

Search This Blog

Monday, May 17, 2010

When selenium.waitForPageToLoad is not working

When selenium.waitForPageToLoad() is not working then we can use alternate solution.


boolean Condition = false;
for (int second = 0; second < 60; second++) {
try {
if ((selenium.isElementPresent("xpath of your element on which you want to click"))) {

selenium.click("xpath of your element on which you want to click");
Condition = true;
break;
}
}
catch (Exception ignore) {
}
pause(1000);
}
assertTrue(Condition);

How to setup Selenium RC with TestNG in eclipse

TestNG is a framework to work in JAVA. Junit and TestNG almost same on the surface but there are major differences between their frameworks and their nature of testing. JUnit is designed to do the unit testing, and it's doing it very well while the TestNG has designed to do the functionality testing at high level. So there are many features available in the TestNG that you will not find in JUnit.


TestNG Annotation


@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@Parameters: Describes how to pass parameters to a @Test method.


Test case

goolgesearch.java

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class googlesearch extends SeleneseTestCase{

Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
private SeleniumServer seleniumServer;


@BeforeClass
@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
public void setUp(String host, String port, String browser , String url ) throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();

rc.setSingleWindow(true);

seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
seleniumServer.start();
selenium.start();

}

@Test
@Parameters({"search","expected"})
public void googling(String search, String expected) {
selenium.open("/");
selenium.waitForPageToLoad("6000");
selenium.type("q", search);
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent(expected));

}
@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop(); 
seleniumServer.stop();

}
}


TestNG Suite

googlesearchsuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="googlesearch" verbose="3">
<parameter name="selenium.host" value="localhost"></parameter>
<parameter name="selenium.port" value="4444"></parameter>
<parameter name="selenium.browser" value="*iexplore"></parameter>
<parameter name="selenium.url" value="http://www.google.com"></parameter>

<test name="googlesearch">
<parameter name="search" value="automationtricks.blogspot.com"></parameter>
<parameter name="expected" value="automationtricks.blogspot.com"></parameter>
<classes>
<class name="googlesearch"></class>
</classes>
</test>
</suite>


In this method you dont have to start your Selenium RC server through command line . The java code will start the server , run the test and stop the server.
Before you run the Test , make sure all path and build path are correct.

  • Install TestNG plug-in in your eclipse.
  • Set the java build path and pointing to RC sever.
    Go to Java project and right click on it then select properties then select java build path then select library tab and select external jar pointing you selenium RC server.
  • Run the test case as TestNG test suite.
    Go to googlesearch.java page right click on the page the click on Run as... and select the suite in the dialog box which you created above.

  • How to pass parameters in selenium RC using TestNG

    We can parametrize our test cases using TestNG in Selenium RC.

    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import com.thoughtworks.selenium.*;
    import org.openqa.selenium.server.*;
    import org.testng.annotations.*;
    
    import java.io.*;
    import java.sql.*;
    
    
    
    public class TestExcel extends SeleneseTestBase {
    
    @DataProvider(name="DP1")
    public Object[][] createData(){
    Object[][] retObjArr = {{"testuser1","password1"},
    {"testuser2","password2"},
    {"testuser3","password3"},
    {"testuser4","password4"},
    {"testuser5","password5"},
    };
    return(retObjArr);
    }
    
    
    private SeleniumServer seleniumServer;
    Selenium selenium;
    
    @BeforeClass
    public void setUp()throws Exception{
    
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.trustAllSSLCertificates();
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.yahoo.com");
    seleniumServer.start();
    selenium.start();
    }
    
    @Test (dataProvider = "DP1")
    public void testEmployeeData(String username, String password){
    selenium.open("https://login.yahoo.com/config/mail?.src=ym&.intl=us/");
    selenium.type("username", username);
    selenium.type("passwd",password);
    selenium.click(".save");
    selenium.waitForPageToLoad("30000");
    assertTrue(selenium.isTextPresent("Hi,"+username));
    selenium.click("_test_sign_out");
    selenium.waitForPageToLoad("30000");
    
    }
    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop();
    seleniumServer.stop();
    
    }
    
    

    How to pass parameters to JUNIT or TestNG test case.

    You can parametrize your test cases using excel sheet. With the help you TestNG we can pass different set of data to our test cases by following steps

    1. create a data file excel rename column as username and fill below data like
    
    username  
    test1         
    test2
    test3
    test4
    
    

    2. create a dsn through control pannel--> administrative tool--> Data source (ODBC) --> select system dsn --> click add
    then select "dirver do microsoft excel" select workbook your data file which you created above.

    now your data source and provider is ready now connect this in your test cases using java class for

    Class.forName("sun.jdbc.odbc.
    JdbcOdbcDriver");


    3. Write the test cases
    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import com.thoughtworks.selenium.*;
    import org.openqa.selenium.server.*;
    import org.testng.annotations.*;
    public class TestExcel extends SeleneseTestBase {
    @BeforeClass
    public void setUp()throws Exception{
    
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.trustAllSSLCertificates();
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.yahoo.com");
    seleniumServer.start();
    selenium.start();
    }
    @Test
    public  void testread()throws Exception{
    // Connection connection = null;
    
    
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection( "jdbc:odbc:nk" );
    // Connection con = DriverManager.getConnection( "jdbc:odbc:nk" ); here you write your driver which you created using ODBC connecting excel workbook.
    
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );
    
    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();
    
    while (rs.next()) {
    
    for (int i = 1; i <= numberOfColumns; i++) {
    if (i > 1) System.out.print(", ");
    String columnValue = rs.getString(i);
    System.out.print(columnValue);
    selenium.open("/");
    
    pause(5000);
    selenium.click("link=Sign in");
    selenium.waitForPageToLoad("30000");
    
    
    try {
    if (selenium.isElementPresent("//a[@id='overridelink']"))
    selenium.click("//a[@id='overridelink']");
    }
    catch (Exception e) {}
    pause(30000);
    selenium.type("userid", columnValue);
    selenium.type("pass","password");
    selenium.click("//button[@class='bfbt' and @type='submit']");
    pause(8000);
    String wc = selenium.getTable("//div[@id='dynamicmenu-hdrCtr']/table[1].0.1");
    System.out.print(wc);
    
    selenium.click("link=Sign out");
    selenium.waitForPageToLoad("20000");
    selenium.isTextPresent("Welcome!");
    
    }
    System.out.println("");   
    
    }
    
    st.close();
    con.close();
    
    } catch(Exception ex) {
    System.err.print("Exception: ");
    System.err.println(ex.getMessage());
    }
    }
    }
    
    

    How to use GOtoIf and while in selenium IDE

    Selenium IDE has some programing capacity upto some extend. There we can use If conditions and loop statements.

    How to use GOtoIf and while
    
    <tr>
    <td>store</td>
    <td>1</td>
    <td>looptimes</td>
    </tr>
    
    <tr>
    <td>while</td>
    <td>storedVars.looptimes < 11</td>
    <td></td>
    </tr>
    
    <tr>
    <td>store</td>
    <td>javascript{storedVars.looptimes++;}</td>
    <td></td>
    </tr>
    
    <tr>
    <td>gotoIf</td>
    <td>javascript{(storedVars.looptimes==5);}</td>
    <td>lbltest</td>
    </tr>
    
    <tr>
    <td>open</td>
    <td>http://www.yahoo.com</td>
    <td></td>
    </tr>
    
    <tr>
    <td>echo</td>
    <td>${looptimes}</td>
    <td></td>
    </tr>
    
    <tr>
    <td>endWhile</td>
    <td></td>
    <td></td>
    </tr>
    
    <tr>
    <td>label</td>
    <td>lbltest</td>
    <td></td>
    </tr>
    
    
    If your selenium IDE does not support while or if you don't have while js in your extension Please download below js and add in your extension.


    Click here to download while.js

    Start selenium server through eclipse

    You can start selenium RC server using JAVA code in eclipse.
    
    import com.thoughtworks.selenium.*;
    import org.openqa.selenium.server.SeleniumServer;
    import org.openqa.selenium.server.RemoteControlConfiguration;
    
    
    public class Cartpayment extends SeleneseTestCase {
    
    Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";
    private SeleniumServer seleniumServer;
    
    
    
    
    public void setUp() throws Exception {
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.setAvoidProxy(true);
    rc.setSingleWindow(true);
    rc.setReuseBrowserSessions(true);
    
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium(localhost, 4444, *firefox, "http://www.yahoo.com");
    seleniumServer.start();
    selenium.start();
    }
    
    
    

    How to avoid invalid certificate in selenium RC (IE)

    If you are running selenium RC test with IE a site which already has invalid certificate then IE will give a warning for "invalid certificate". You can have selenium
    automatically click on "Continue" link in it(which has ID of overridelink) by adding
    below command in your IE specific test case.

    if ((selenium.isElementPresent("//a[@id='overridelink']"))) {
    selenium.click("//a[@id='overridelink']");
    //selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    selenium.waitForPageToLoad("30000");
    }