Showing posts with label selenium RC. Show all posts
Showing posts with label selenium RC. Show all posts

December 29, 2011

Handling HTTPS sites using Selenium

December 29, 2011 58
As all we know selenium is used for automating web application. We will not have any difficulty in automating HTTP site...We face some of the below issues while automating HTTPS sites..

HTTPS sites will show some security notifications below are some:

Firefox:



Internet Explorer:





To over come this issue we need to add certification

1. Need to add remote control configuration
RemoteControlConfiguration rcc = new RemoteControlConfiguration ();

2. Set TrustAllSSLCertificates flag to TRUE
rcc.setTrustAllSSLCertificates(true);


3. Pass the rcc Instance to Selenium Server
SeleniumServer server = new SeleniumServer(rcc);


If the above steps also doesnt work you need to install cyber villian certificate to automate HTTPS sites (Especially for IE)


1. Extract selenium-server.jar
2. In selenium server folder Open sslSupport folder
3. It contains cybervillainsCA.cer please install that certificate

Now you can run your selenium rc  scripts with out any problem for HTTPS sites.


Below is the code with out the Above steps:


====================================

package selenium;


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.SeleneseTestCase;

@SuppressWarnings("deprecation")
public class HTTPs extends SeleneseTestCase{

public SeleniumServer ss;
@Before
public void setUp() throws Exception
{
ss=new SeleniumServer();
ss.start();
selenium=new DefaultSelenium("localhost", 4444, "*firefox", "https://www.dibbs.bsm.dla.mil/");
selenium.start();
}

@Test
public void testSelenium() throws Exception
{
selenium.open("/");
selenium.windowMaximize();

Thread.sleep(5000);

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

}
=============================================
If you run the above script it will give you an error..see the below image


To avoid that error please use below code:

================================================

package selenium;


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

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

@SuppressWarnings("deprecation")
public class HTTPs extends SeleneseTestCase{

public SeleniumServer ss;
@Before
public void setUp() throws Exception 
{
RemoteControlConfiguration rcc=new RemoteControlConfiguration();
//trust all ssl certificates
rcc.setTrustAllSSLCertificates(true);

ss=new SeleniumServer(rcc);
ss.start();
selenium=new DefaultSelenium("localhost", 4444, "*firefox", "https://www.dibbs.bsm.dla.mil/");
selenium.start();
}

@Test
public void testSelenium() throws Exception
{
selenium.open("/");
selenium.windowMaximize();

Thread.sleep(5000);

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

}


========================================================
The three lines which are in blue will make your browser to trust all SSL certificates..

Hope this post will be useful..


December 27, 2011

Web Driver Backed Selenium -- Sample script

December 27, 2011 56
Web Driver Backed Selenium -- Sample script
The Java version of WebDriver provides an implementation of the Selenium-RC API. These means that you can use the underlying WebDriver technology using the Selenium-RC API. This is primarily provided for backwards compatablity. It allows those who have existing test suites using the Selenium-RC API to use WebDriver under the covers. It’s provided to help ease the migration path to Selenium-Web driver.


Sample Backed web driver script looks like this:


=================================================================



package webone;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
@SuppressWarnings("deprecation")
public class BackedSelenium extends SeleneseTestCase  {

//web driver --here we are declaring the browser
WebDriver driver = new FirefoxDriver();
@Before
public void setUp() throws Exception {

String baseUrl = "http://www.google.co.in/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}


@Test
public void testBackedSelenium() throws Exception {
//This is using selenium
selenium.open("/");
selenium.click("id=gbi5");
selenium.click("id=gmlas");
selenium.waitForPageToLoad("30000");
selenium.type("name=as_q", "test");
//Here we are using Webdriver
driver.findElement(By.id("as_oq1")).sendKeys("naga");

driver.findElement(By.id("as_oq2")).sendKeys("Webdriver");
Thread.sleep(5000);

}


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

=====================================================

By using the above format you can use both the Selenium API and Web driver API...



September 21, 2011

Page scrolling using Selenium and Java script

September 21, 2011 2
Page scrolling using Selenium and Java script
Here is the post how to scroll page using selenium...Earlier i used FOCUS command as a solution/ work around..but its not that much useful in all cases.


The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page .


Below is the code for scrolling page using selenium...




 selenium.getEval("window.scrollBy(0,200)");


it will scroll the pahe to 200 pixels..if you want you can increase the pixel size.


0 ---X-axis
200--Y-axis


Hope it will be useful for all...




July 25, 2011

Working on New Window

July 25, 2011 2
Working on New Window

Below example is to:
  • open the new window by clicking on one link in Main window, 
  • performing some operations in pop up window,
  • then close the pop up window,
  • and operate some function in main window again.

Program:

package one;

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

public class Newwindowselenium extends SeleneseTestCase {
public SeleniumServer ss;
@Before
public void setUp() throws Exception {
ss=new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.bitmotif.com/");
selenium.start();
}

@Test
public void testNewwindowselenium() throws Exception {
// open the URL http://www.bitmotif.com/test-page-for-selenium-remote-control/
selenium.open("/test-page-for-selenium-remote-control/");
// Maximizing the main window
selenium.windowMaximize();
// Click on the link This link opens a popup
selenium.click("link=This link opens a popup");
selenium.waitForPopUp("popup", "30000");
// Select the popup window 
selenium.selectWindow("name=popup");
// Click the link About on popup window 
selenium.click("link=About");
selenium.waitForPageToLoad("30000");
// Close the popup window 
selenium.close();
// back to the main window
selenium.selectWindow("null");
// Click on the link This link goes to another page
selenium.click("link=This link goes to another page");
selenium.waitForPageToLoad("30000");
Thread.sleep(10000);
}

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

May 21, 2011

Setting up Selenium RC & TestNG using Eclipse - 1

May 21, 2011 7
I found that it is not easy for the beginners to setup Selenium RC and run the GoogleTest.java example after downloading it from www.openqa.org . The purpose of this post is to help beginners (new users to Selenium RC) help setting up Selenium RC with TestNG using Eclipse.


You need eclipse and TestNg Jar files..


Below are the steps to SetUp TestNG in eclipse.


Step1:


Open Eclipse.
Click on Help form Menubar  and select "Install new software".


Step2:


A new window will be opened. Click Add button.
 A new window is opened. and Enter Name as TESTNG and enter site "http://beust.com/eclipse"








Step3:
Select TestNG and Click Next

Step4:

In the next step click finish




Thats it You installed TESTNG in eclipse...

to verify that you need to click on Project -->new -->Other--?>Yoou can see TESTNG foler and expand that folder you cna see TESTNG Class..

See below screen shot..



Then go to second part of the post....it is post regarding 

Setting up Selenium RC & TestNG using Eclipse-II




May 19, 2011

Working with Multiple windows

May 19, 2011 1
Working with Multiple windows
I saw many posts on identifying window Name, ID and Title or Attribute....but all are giving Syntax...We can find that syntax in Selenium document. In this post, I would like to give an example how to identify window Name, ID and Title or Attribute...


Below is the code is to identify the new window....By using TITLE 


package test;


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


public class NewWindow extends SeleneseTestCase {
public SeleniumServer ss;
@Before
public void setUp() throws Exception {
ss= new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.truepanel.com/");
selenium.start();
}


@Test
public void testNewWindow() throws Exception {
selenium.open("/");
   selenium.windowMaximize();
   selenium.setSpeed("2000");
   selenium.click("//div[@id='right']/div[2]/a[1]");
   selenium.setSpeed("2000");
   //Click on Facebook icon
   selenium.click("//div[@id='right']/div[2]/a[2]");
   selenium.setSpeed("2000");
   //Click on Linkedin icon
   selenium.click("//div[@id='right']/div[2]/a[3]");
   selenium.setSpeed("2000");
      String[] winNames = selenium.getAllWindowTitles();
 //All window names should be displayed in console
      for (int i = 0; i < winNames.length; i++) 
   {
    System.out.println("Window Titles are "+winNames[i]);
}
             
   /*selenium.selectWindow("Twitter");
   verifyEquals("Truepanel (Truepanel) on Twitter", selenium.getTitle());*/
   //Selects the new window with Title Truepanel | Facebook
   selenium.selectWindow("TruePanel | Facebook");
   selenium.setSpeed("2000");
   selenium.windowFocus();
   selenium.setSpeed("2000");
   selenium.windowMaximize();
   selenium.setSpeed("2000");
   if(selenium.isElementPresent("//div[@id='globalContainer']/div[2]/div/a/span"))
   {
    selenium.click("//div[@id='globalContainer']/div[2]/div/a/span");
selenium.waitForPageToLoad("30000");
selenium.type("firstname", "test");
selenium.type("lastname", "test");
selenium.type("reg_email__", "tets");
selenium.type("reg_email_confirmation__", "test");
selenium.selectWindow("null");
       selenium.windowFocus();
       verifyEquals("Welcome to Truepanel | Online Video Focus Groups", selenium.getTitle());
       selenium.click("//div[@id='navBtns']/a[2]");
selenium.waitForPageToLoad("30000");
       Thread.sleep(1000);
   }
   
     else
     {
    //this command will select the parent window
       selenium.selectWindow("null");
       selenium.windowFocus();
       verifyEquals("Welcome to Truepanel | Online Video Focus Groups", selenium.getTitle());
       Thread.sleep(1000);
   }
}
@After
public void tearDown() throws Exception {
selenium.stop();
ss.stop();
}
}

May 13, 2011

Handling Alerts...

May 13, 2011 12
Handling Alerts...
How to handle alerts in Registration page..

Below is the command used for handling Alerts...




getAlert()


Returns:
The message of the most recent JavaScript alert
Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts. Getting an alert has the same effect as manually clicking OK. If an alert is generated but you do not consume it with getAlert, the next Selenium action will fail.


Under Selenium, JavaScript alerts will NOT pop up a visible alert dialog.

Selenium does NOT support JavaScript alerts that are generated in a page's onload() event handler. In this case a visible dialog WILL be generated and Selenium will hang until someone manually clicks OK.




Below is the script to handle Alert.....



package reporting;


import com.thoughtworks.selenium.*;


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


public class Alert extends SeleneseTestCase  {
public SeleniumServer ss;


@Before
public void setUp() throws Exception {
ss =new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.rediff.com/");
selenium.start();
}


@Test
public void testAlert() throws Exception {
selenium.open("/");
selenium.windowMaximize();
selenium.click("link=Create a Rediffmail account");
selenium.waitForPageToLoad("30000");


selenium.type("name", "tester");
selenium.click("login");
//Enter Name here
selenium.type("login", "Tester1");
selenium.type("passwd", "selenium");
selenium.select("DOB_Day", "label=04");
selenium.select("DOB_Month", "label=APR");
selenium.select("DOB_Year", "label=1912");
//Click Register button
selenium.click("Register");
//Will get an alert..to handle this alert we will use below command
selenium.getAlert();
Thread.sleep(10000);
//After handling alert enter user name
selenium.type("login", "nagaraju");
Thread.sleep(10000);
}


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



Send a Mail to Friend from Gmail

May 13, 2011 1
Send a Mail to Friend from Gmail
Send mail to any one from Gmail..using the below script..

Gmail is one of the complicated stuff..Record and play back will not work here..because IDs and Names of the elements are changing randomly..Here we need to use ClickAt instead of Click...and we need to use Xpaths...

Below is the script to send mail to one mail ID...

Please change your mail and password  to execute this program...



package reporting;

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

public class Gmail extends SeleneseTestCase {
public SeleniumServer ss;
@Before
public void setUp() throws Exception {
ss=new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://www.google.com/");
selenium.start();
}

@Test
public void testGmail() throws Exception {
selenium.open("/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&bsv=llya694le36z&scc=1&ltmpl=default&ltmplcache=2");
selenium.windowMaximize();
selenium.type("Email", "nagaqtt");
selenium.type("Passwd", "xxxx");
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
//Click the compose button
selenium.clickAt("//div[3]/div[1]/div[1]/div[1]/div/div", "");
//Enter Email ID in To field
Thread.sleep(10000);
selenium.type("to", "nagaselenium@gmail.com");
//Subject of the mail
selenium.type("subject", "testing");
Thread.sleep(1000);
selenium.selectWindow("null");
//Content in the mail
Thread.sleep(1000);
selenium.typeKeys("//html/body[@class='editable  LW-yrriRe']", "nagaraju sending mail to his friend");
//Clicking the send button
selenium.clickAt("//div[@role='button']/b", "");
Thread.sleep(10000);
//Clicking the logout
if(selenium.isElementPresent("gbi4m1"))
{
selenium.click("gbi4m1");
}

selenium.click("link=Sign out");
Thread.sleep(10000);
}

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




How to Scroll the page

May 13, 2011 5
How to Scroll the page
We can scroll the page by using FOCUS command...


focus(locator)
Arguments:
Move the focus to the specified element; for example, if the element is an input field, move the cursor to that field.
Basically there is no specific command for scrolling the page down. I tried keyPress and all other options, but i felt Focus is the best for scrolling the page down. We need to select a element and set focus on that element.

Below is the example...


package reporting;

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

public class Focus extends SeleneseTestCase {
public SeleniumServer aa;
@Before
public void setUp() throws Exception {
aa= new SeleniumServer();
aa.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com/");
selenium.start();
}

@Test
public void testFocus() throws Exception {
selenium.open("/");
selenium.windowMaximize();
selenium.type("q", "selenium wiki");
selenium.click("btnG");
//Waiting for an element
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=Selenium Overview - Wiki - Liferay.com")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
//Set focus on element --then page will scroll down --focus will be on that element
selenium.focus("link=Selenium Overview - Wiki - Liferay.com");
//To capture a screenshot
selenium.captureScreenshot("c:/naga/screenshot.png");
Thread.sleep(10000);
}



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




May 3, 2011

Run Selenium RC using Command Prompt

May 03, 2011 55
Downloading and Installing Selenium RC:
  • Go to http://seleniumhq.org/download/.
  • Under Selenium Server (formerly the Selenium RC Server) section you    can see the selenium server latest version.
  • Click on download, it will be redirected to selenium server download page.
  • Selenium server is a JAR file. Download selenium-server-standalone-2.ob3.jar.
  • Create a Folder selenium in “C” drive.
  • Paste the selenium-server-standalone-2.ob3.jar file in that folder.

Start  and Stop Selenium Server:

  • We need to be able to start and stop the Selenium Server.
  • We can do this at the command line:
  • Start:  (Command line)
  • java -jar selenium-server-standalone-2.0b3.jar
  • Stop: (Browser)
  • http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer
Getting Started with Selenium/ Selenium RC steps:

  • Record your test case using Selenium IDE
  • Play your test case using selenium IDE itself to make sure that your test case works.
  • Export your recording test case - as html file.
  • Create a test suite.
  • Run your exported html test case using selenium rc
Steps:

  • Record a program using IDE:
  • Create a folder in any drive – Ex: C:\tests
Create folders:
  • We need to create two folders.
  • 1.To save our test suite and test case. (tests)
  • 2.To save results (results)
  • Using Selenium IDE record a test case and save it as .html in a tests folder.
  • Make sure your test run correctly.
Create a Test suite:
  • Create a test suite and save it in C:\tests folder
Run your scripts using selenium RC:

  • We can run our tests in any browser by using selenium RC.


Syntax:
java -jar selenium-server-standalone-2.0b3.jar -htmlSuite "*browser" "http://www.example.com" "c:\naga\tests\run.html" "c:\naga\results\results1.html“

RUN THE ABOVE COMMAND FROM COMMAND PROMPT:

Run this command from selenium directory.
Example:
C:\selenium>java -jar selenium-server.jar -htmlSuite "*iexplore" "http://www.google.com" "c:\naga\tests\run.html“ "c:\naga\results\results1.html"

Selenium Test Runner:

Results: