Selenium Get Attribute of Fields
Below is an Example to get Attribute of Text Box using Selenium :
package Selenium;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.SeleniumServer;
public class Main_Test extends SeleneseTestCase {
public void setUp() throws Exception {
/*
* Startting Selenium Server from Test Case itself
* */
SeleniumServer s = new SeleniumServer();
s.start();
/*
* Opening Google web page in Firefox
* */
setUp("http://www.google.co.in//", "*firefox");
}
public void testNew() {
selenium.open("http://www.google.co.in//");
/*
* Getting Property of Text Field like maxlength and size of Text Field
*
* */
String collectTextProperty = selenium.getEval("var property = new Array(); " +
"property[0] = window.document.getElementsByName('q')[0].maxLength;" +
"property[1] = window.document.getElementsByName('q')[0].size; property.toString();");
/* Replace getElementsByName('q') in this 'q' by element id of Textbox*/
String[] Textproperties = collectTextProperty.split(",");
for (int i = 0; i < Textproperties.lenght();i++){
/*
*Textproperties[0] give maxlength of Text Box.
*Textproperties[1] give size of Text Box.
**/
}
selenium.type("q", "selenium");
selenium.stop();
}
}
Open Source Guide Blog Started with thought of sharing Ideas in existing Technology
Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts
Sunday, October 25, 2009
Selenium UI Testing
Selenium UI Testing
Below are methods that can be used to test position of element in web page
UI Testing can be done using these method
1. getElementHeight(java.lang.String locator)
2. getElementPositionLeft(java.lang.String locator)
3. getElementPositionTop(java.lang.String locator)
4. getElementWidth(java.lang.String locator)
Below are methods that can be used to test position of element in web page
UI Testing can be done using these method
1. getElementHeight(java.lang.String locator)
2. getElementPositionLeft(java.lang.String locator)
3. getElementPositionTop(java.lang.String locator)
4. getElementWidth(java.lang.String locator)
Selenium PDF Testing
I came across queries related to Testing PDF in web page . As after clicking on PDF link , PDF file on on same page , But we can not either verify Text or any content on that page.
So I thought of copying that data into text File and comparing data in that Text File with actuall Text File.
As we know that after pressing CTRL+A ,CTRL+C text are stored in clipboard. So we can move that data in Text File .
Example given below is performed on selenium forum site itself.
Below is Java code for that
TextClipBoard Class where actuall data from PDF is moved to text file .
package Selenium;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import java.awt.event.KeyEvent;
import java.io.*;
public class TextClipBoard extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://seleniumhq.org/", "*chrome");
}
public void testNew() throws Exception {
/*
* Opening Selenium Site
*/
selenium.open("/");
/*
* Clicking on link Forums
*/
selenium.click("link=forums");
/*
* Waiting for Elements presence because , while Testing page is getting timeout after
* default timeout period , So I searched for Communities keyword in that page
*/
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("css=h4:contains(\"Communities\")")) break; }
catch (Exception e) {}
Thread.sleep(1000);
}
/*
* Clicking on recently posted link in forum site
*/
selenium.click("link=Clover report for Selenium tests");
/*
* Waiting for Default Timeout period
*/
selenium.waitForPageToLoad("30000");
/*
* Clicking on View as pdf link
*/
selenium.click("link=View as PDF");
/*
* Maximizing window and moving focus to that page
*/
selenium.windowMaximize();
selenium.windowFocus();
pause(10000);
selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL)); //Stands for CONTROL
pause(1000);
selenium.keyPressNative("65"); // Stands for A "ascii code for A"
pause(4000);
selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL));//Stands for CONTROL
pause(1000);
selenium.keyPressNative("67"); // Stands for C "ascii code for C"
/*
* Below code is call to Text Transfer class so as to retrieve data from Clip Board
* As we know that after copying data using ctrl+A and ctrl+C data remain in clip board
* We can move that data into Text File
*/
TextTransfer textTransfer = new TextTransfer();
//display what is currently on the clipboard
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
pause(4000);
selenium.stop(); // Kill the Browser
/*
* Writing data into Text File so as to compare these data with actual data.
* */
Writer output = null;
String text = textTransfer.getClipboardContents();
File file = new File("D:\\write.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
try{
FileInputStream fstream = new FileInputStream("D:\\write.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println ("Reading from File line by line : " +strLine);
}
//Close the input stream
in.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
TextTransfer class to retrieve data from Clipboard
package Selenium;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
public final class TextTransfer implements ClipboardOwner {
public static void main (String... aArguments ){
TextTransfer textTransfer = new TextTransfer();
//display what is currently on the clipboard
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
//change the contents and then re-display
textTransfer.setClipboardContents("blah, blah, blah");
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
}
/**
* Empty implementation of the ClipboardOwner interface.
*/
public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
//do nothing
}
/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
*/
public void setClipboardContents( String aString ){
StringSelection stringSelection = new StringSelection( aString );
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( stringSelection, this );
}
/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an
* empty String.
*/
public String getClipboardContents() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor)
;
if ( hasTransferableText ) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (UnsupportedFlavorException ex){
//highly unlikely since we are using a standard DataFlavor
System.out.println(ex);
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}
}
So I thought of copying that data into text File and comparing data in that Text File with actuall Text File.
As we know that after pressing CTRL+A ,CTRL+C text are stored in clipboard. So we can move that data in Text File .
Example given below is performed on selenium forum site itself.
Below is Java code for that
TextClipBoard Class where actuall data from PDF is moved to text file .
package Selenium;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import java.awt.event.KeyEvent;
import java.io.*;
public class TextClipBoard extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://seleniumhq.org/", "*chrome");
}
public void testNew() throws Exception {
/*
* Opening Selenium Site
*/
selenium.open("/");
/*
* Clicking on link Forums
*/
selenium.click("link=forums");
/*
* Waiting for Elements presence because , while Testing page is getting timeout after
* default timeout period , So I searched for Communities keyword in that page
*/
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("css=h4:contains(\"Communities\")")) break; }
catch (Exception e) {}
Thread.sleep(1000);
}
/*
* Clicking on recently posted link in forum site
*/
selenium.click("link=Clover report for Selenium tests");
/*
* Waiting for Default Timeout period
*/
selenium.waitForPageToLoad("30000");
/*
* Clicking on View as pdf link
*/
selenium.click("link=View as PDF");
/*
* Maximizing window and moving focus to that page
*/
selenium.windowMaximize();
selenium.windowFocus();
pause(10000);
selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL)); //Stands for CONTROL
pause(1000);
selenium.keyPressNative("65"); // Stands for A "ascii code for A"
pause(4000);
selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL));//Stands for CONTROL
pause(1000);
selenium.keyPressNative("67"); // Stands for C "ascii code for C"
/*
* Below code is call to Text Transfer class so as to retrieve data from Clip Board
* As we know that after copying data using ctrl+A and ctrl+C data remain in clip board
* We can move that data into Text File
*/
TextTransfer textTransfer = new TextTransfer();
//display what is currently on the clipboard
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
pause(4000);
selenium.stop(); // Kill the Browser
/*
* Writing data into Text File so as to compare these data with actual data.
* */
Writer output = null;
String text = textTransfer.getClipboardContents();
File file = new File("D:\\write.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
try{
FileInputStream fstream = new FileInputStream("D:\\write.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println ("Reading from File line by line : " +strLine);
}
//Close the input stream
in.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
TextTransfer class to retrieve data from Clipboard
package Selenium;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
public final class TextTransfer implements ClipboardOwner {
public static void main (String... aArguments ){
TextTransfer textTransfer = new TextTransfer();
//display what is currently on the clipboard
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
//change the contents and then re-display
textTransfer.setClipboardContents("blah, blah, blah");
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
}
/**
* Empty implementation of the ClipboardOwner interface.
*/
public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
//do nothing
}
/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
*/
public void setClipboardContents( String aString ){
StringSelection stringSelection = new StringSelection( aString );
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( stringSelection, this );
}
/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an
* empty String.
*/
public String getClipboardContents() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor)
;
if ( hasTransferableText ) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (UnsupportedFlavorException ex){
//highly unlikely since we are using a standard DataFlavor
System.out.println(ex);
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}
}
Selenium SSL Site Testing / Secure [HTTPS] Site Testing
Selenium SSL Site Testing / Secure [HTTPS] Site Testing
Selenium Secure Site [HTTPS] Testing
Below are steps that need to be followed to Test HTTPS – Secure Site :
Selenium uses its own Certificate for all https-connections, the Certificate is Cybervilian Certificate. We have to import the certificate into your Internet Explorer.
Please follow below steps to import Cybervilian Certificate.
1.Open Internet Explorer.
2.Navigate to Tool->Option.
3.Click on content Tab.
1.Now click on certificates button.
5.Click on Import button.
6.Click on Next > button.
7.Click on browse button.
8.Import cybervilliansCA .
9.Click on Next > button.
10.Again Click on Next > button.
11.Click on Finish button.
Image will be displayed as below on succesfull import of certificate.
12.Now open secure site:
Example: https://herald.cybage.com/exchange
13.Security Alert will be shown as below:
14. Click on View Certificate button.
15.Click on Install Certificate button .
16.Click on Next > button.
17.Click on Next > button.
18.Click on Finish button.
On Succesfull import of certificate
19.Click on yes button, After successful import of certificate .
20.Now you can run your Test Case.
First time while running HTTPS Test Case you may find alert box will be shown , click on OK and remember my decision .
Below example is used by be to Test my official Email Site, though password has been changed by me for confidential reason:
Below code is used to connect HTTPS site in Internet Explorer .
package Selenium;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class CertificateTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("https://herald.cybage.com/exchange", "*iexplore");
}
public void testSecureSite() throws Exception {
selenium.open("/exchweb/bin/auth/owalogon.asp?url=https://herald.cybage.com/exchange&reason=0");
selenium.type("username", "XXXXXX");
selenium.type("password", "XXXXXX");
selenium.click("SubmitCreds");
}
}
To run Test Case in Firefox . Please follow below steps:
1.Close all instance of Firefox .
2.Start Firefox from command prompt using command: firefox –ProfileManager
1.Create a new profile. You'll be prompted to choose a directory for the profile.
2.Select the profile and run Firefox using it.
3.Browse to the HTTPS URL (with self-signed certificate) you're going to be testing against.
4.Accept the self-signed certificate when prompted. This creates an exception for it in the profile.
5.Close the browser.
6.Go to the Firefox profile directory.
7.Delete everything in the directory except for the cert_override.txt and cert8.db files.
8.Start Selenium Server using below command:
When you run your Selenium server pass a
-firefoxProfileTemplate /path/to/profile/dir argument to it. This tells Selenium to use your partial profile (with certificate exceptions) as a basis for minting its new profile.
package Selenium;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class CertificateTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("https://herald.cybage.com/exchange", "*firefox");
}
public void testSecureSite() throws Exception {
selenium.open("/exchweb/bin/auth/owalogon.asp?url=https://herald.cybage.com/exchange&reason=0");
selenium.type("username", "XXXXX");
selenium.type("password", "XXXXX");
selenium.click("SubmitCreds");
}
Selenium Secure Site [HTTPS] Testing
Below are steps that need to be followed to Test HTTPS – Secure Site :
Selenium uses its own Certificate for all https-connections, the Certificate is Cybervilian Certificate. We have to import the certificate into your Internet Explorer.
Please follow below steps to import Cybervilian Certificate.
1.Open Internet Explorer.
2.Navigate to Tool->Option.
3.Click on content Tab.
1.Now click on certificates button.
5.Click on Import button.
6.Click on Next > button.
7.Click on browse button.
8.Import cybervilliansCA .
9.Click on Next > button.
10.Again Click on Next > button.
11.Click on Finish button.
Image will be displayed as below on succesfull import of certificate.
12.Now open secure site:
Example: https://herald.cybage.com/exchange
13.Security Alert will be shown as below:
14. Click on View Certificate button.
15.Click on Install Certificate button .
16.Click on Next > button.
17.Click on Next > button.
18.Click on Finish button.
On Succesfull import of certificate
19.Click on yes button, After successful import of certificate .
20.Now you can run your Test Case.
First time while running HTTPS Test Case you may find alert box will be shown , click on OK and remember my decision .
Below example is used by be to Test my official Email Site, though password has been changed by me for confidential reason:
Below code is used to connect HTTPS site in Internet Explorer .
package Selenium;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class CertificateTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("https://herald.cybage.com/exchange", "*iexplore");
}
public void testSecureSite() throws Exception {
selenium.open("/exchweb/bin/auth/owalogon.asp?url=https://herald.cybage.com/exchange&reason=0");
selenium.type("username", "XXXXXX");
selenium.type("password", "XXXXXX");
selenium.click("SubmitCreds");
}
}
To run Test Case in Firefox . Please follow below steps:
1.Close all instance of Firefox .
2.Start Firefox from command prompt using command: firefox –ProfileManager
1.Create a new profile. You'll be prompted to choose a directory for the profile.
2.Select the profile and run Firefox using it.
3.Browse to the HTTPS URL (with self-signed certificate) you're going to be testing against.
4.Accept the self-signed certificate when prompted. This creates an exception for it in the profile.
5.Close the browser.
6.Go to the Firefox profile directory.
7.Delete everything in the directory except for the cert_override.txt and cert8.db files.
8.Start Selenium Server using below command:
When you run your Selenium server pass a
-firefoxProfileTemplate /path/to/profile/dir argument to it. This tells Selenium to use your partial profile (with certificate exceptions) as a basis for minting its new profile.
package Selenium;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class CertificateTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("https://herald.cybage.com/exchange", "*firefox");
}
public void testSecureSite() throws Exception {
selenium.open("/exchweb/bin/auth/owalogon.asp?url=https://herald.cybage.com/exchange&reason=0");
selenium.type("username", "XXXXX");
selenium.type("password", "XXXXX");
selenium.click("SubmitCreds");
}
Subscribe to:
Posts (Atom)