My Blog List

Saturday, March 17, 2012

Selenium - HTTP to HTTPS SSL problems

In the daily life of selenium users, the SSL certificate problem is the constant which irritates the testers again and again. Lot of workarounds and solutions are on the way for this problem. Here are some ways that I came across.
1. Use Browser modes
In Selenium 1.0 and before, the try to use *chrome and *iehta
In Selenium 1.x and later, try to use *firefox and *iexplore
Also we can use *firefoxproxy and *iexploreproxy. But this have limitations on no. of instances and multi window modes.
2. Install your application certificates
If you have certificates for your SSL layer supported application, the best way is to install that certficate provided by your developers.
3. Install CyberVillains certificates
For those who don't have their own security certificates, selenium itself provides some inbuilt certificates in the package called CyberVillains. You can install these certificates to avoid the SSL switching problems.
4. Separate Browser Profiles
Create a separate profile by running firefox -profilemanager and add your sites under trusted sites and add the exceptions list. Whenever launching browser, instruct selenium RC server to use the created firefox proile instead of default one like,
java -jar selenium-server-standalone-1.0.x.jar -firefoxProfileTemplate "pathToTemplate"
5. Use firefox Plug-In Remember Certificate Exceptions(RCE)

Connect QC from QTP and Execute Scripts

Here is the sample code to connect QC and execute scripts stored in QC using QTP.
  1. Dim QTP_App      ' Declare the Application object variable
  2. Dim QTP_Test     ' Declare a Test object variable
  3. Dim QTP_Results  ' Declare a Run Results Options object variable
  4. Dim QC_Path      ' Declare QC Path and assigned actual path
  6. ' Create the QTP Application object
  7. Set QTP_App = CreateObject("QuickTest.Application")
  8. ' Start QuickTest
  9. QTP_App.Launch
  10. ' Make the QuickTest application visible
  11. QTP_App.Visible = True
  13. 'Connect to Quality Center
  14. QTP_App.TDConnection.Connect QC_Path, False
  17. 'Function to open QTPScript and run that script
  18. Function OpenScriptRunTest (ScriptName,TestSetName)
  19.    QTP_App.Open QC_Path&ScriptName,True
  20.    QTP_App.Options.Run.ImageCaptureForTestResults = "OnError"
  21.    QTP_App.Options.Run.RunMode = "Fast"
  22.    QTP_App.Options.Run.ViewResults =True
  23.    Set QTP_Test = QTP_App.Test
  24.    QTP_Test.Settings.Run.IterationMode = "allIterations"
  25.    QTP_Test.Run.StartIteration = 1
  26.    QTP_Test.Run.EndIteration = 1
  27.    QTP_Test.Run.OnError = "NextStep"
  28.    Set QTP_Results = CreateObject("QuickTest.RunResultsOptions")
  29.    QTP_Results.TDRunName="Run_Sample"
  30.    QTP_Results.TDTestInstance=1
  31.    QTP_Results.TDTestSet=QC_Path&TestSetName
  32.    QTP_App.Test.Run qtRunResultsOptions
  33.    QTP_App.Close    
  34. End Function
  36. 'Run a test
  37. Call OpenScriptRunTest (ScriptName,TestSetName)

Regular Expressions - Date, Email

Here are some sample regular expressions for your ref.
Reg Expression for Date:
  1. (0[1-9]|1[012])[//](0[1-9]|[12][0-9]|3[01])[//]200[0-9]$
Reg Expression for Email IDs: 
  1. \b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b
  2. ((\w+)(\.|\_|\-)(\w+)|(\w+))(\@)(\w+)(\.)(w+)

Java - DataBase Connections

For those who are working in selenium and related automation tools using Java API are in need of connecting back end databases to verify the values. Here is the high level info.
Required package:
  1. import java.sql.*;
Code Sample:
  1. //Initializing the driver for JDBC connection
  2. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  3. //Creating properties to be passed for connection
  4. Properties properties = new Properties();
  5. properties.put("user", "shan");
  6. properties.put("password", "pass");
  7. properties.put("characterEncoding", "ISO-8859-1");
  8. properties.put("useUnicode", "true");
  9. //actual connection string
  10. String url = "jdbc:mysql://ServerName:port/DataBaseName";
  11. //Getting the connection object using properties and url
  12. Connection conn = DriverManager.getConnection(url, properties);
  13. //Creating a statement of type CreateStatement
  14. Statement stmt = conn.createStatement(); //for Insert/Update, create of type conn.executeUpdate
  15. String sql = "select * from [Sheet1$]";
  16. RecordSet rs = stmt.executeQuery(sql);
  17. while({
  18.    username = rs.getString("username");
  19.    password = rs.getString("Password");
  20. }
Here are the driver and Connection String for different DBs.
Driver: Class.forName("com.mysql.jdbc.Driver").newInstance();
URL   : jdbc:mysql://serverName:port/DBName
Driver: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
URL   : jdbc:derby:pathToDB:DBName;create=true;
Driver: Class.forName("");
URL   : jdbc:microsoft:sqlserver://serverName:port/DBName


Java Collections - Array, ArrayList, HashTable

Array vs ArrayList in Java:
Array - Fixed Size, Same Data Types only
  1. int[] myArr = new int[10];
  2. myArr[0] = 10;
  3. System.out.println( myArr[0] );
  4. System.out.println( myArr.length );
ArrayList - Dynamic Size, Different data types but not primitive types, Synchronized.
  1. ArrayList<String> myList = new ArrayList<String>();
  2. myList.add("abcd");
  3. myList.add(10);   //Adds in end of list because index not mentioned.
  4. System.out.println( myList.get(0) );

    //Lot of methods are there here.
  5. add(index, Value);
  6. addAll(collection);
  7. clear()
  8. contains(value)
  9. indexOf(value)
  10. get(index)
  11. isEmpty()
  12. lastIndexOf(value)
  13. set(index, value) //Replace the value
  14. size()
  15. toArray()
  16. iterator()   // One-Directional, only access
  17. listIterator() //Bi-Dierectional, allows modification
HashTable vs HashMap vs HashSet:
HashTable - Syncronized, will not allow NULL key/value.
- put, get
HashMap - UnSyncronized, will allow NULL key/value (so maybe good performance than HashTable)
- put, get
HashSet - Will allow duplicate values
- add, contains, get

Friday, March 16, 2012

Make machine unlocked while QTP execution

Lot of guys could have faced this problem and some could able to give some workaround and somebody may still in searching for solution. Here are some ideas you can consider.
Option 1:
1. Go to HKEY_LOCAL_MACHINE\SOFTWARE\Mercury Interactive\QuickTest Professional\MicTest
2. Locate the key SkipEnvironmentChecks
3. Change the value to 1 (Default value is 0)
4. Restart the QTP.
Option 2: 
Consider the API developed by Tarun Lalwani. 

QTP Rare Tips II

1. Optional Step in QTP
As we know, lot of times we need to check the existence of the object before continuing to the actual operation  on that object. Here is nice option which will reduce these kind of If(obj.Exist) lines like,
  1. OptionalStep.Browser("Mercury Tours").Page("Find Flights").WebList("depart").Select "Paris"
Keep in mind that this OptionalStep considers test objcts only. If Object exists, will do the action otherwise will skip the line and continue.

This OptionalStep can be used only with full test object descriptions like above statement.
  1. 'This will not work
  2. Dim objPage
  3. Set objPage = Browser("Mercury Tours").Page("Find Flights")
  4. OptionalStep.objPage.WebList("depart").Select "Paris"
  5. 'and You can't use inside with statement
  6. With Browser("Mercury Tours").Page("Find Flights")
  7.   .WebList("depart").Select "Paris"
  8. End With
  9. 'If you want to use inside with you should give like,
  10. With Browser("Mercury Tours").Page("Find Flights")
  11.   .WebList("depart").Select "Paris"
  12.   OptionalStep.Browser("Mercury Tours").Page("Find Flights").WebEdit("name").Set "abcd"
  13. End With

Run JavaScript from QTP

Yeah, You can run your pure JavaScript from QTP using RunScript method. Lot of times, we are in need of firing events or simulating actions on web page which is not supported by QTP. At that time, you can use your direct DOM methods and directly execute your script on the web page from QTP like,
  1. Dim MyPage, SearchBox
  2. Set MyPage = Browser("title:=Google").Page("title:=Google")  
  3. Set SearchBox = MyPage.RunScript("document.getElementsByName('q')(0);")
  4. SearchBox.Value="testing"

    'if objects available in frames,
    Set SearchBox = MyPage.RunScript("document.frames(4).document.getElementsByName('q')(0);")

    Set objFrame Browser("title:=Google").Page("title:=Google").Frame("title:=something")
Also, you can execute by obtaining actual browser window object if you have IE app like,
  1. Dim IEApp
  2. Set IEApp = CreateObject("InternetExplorer.Application")
  3. IEApp.document.parentWindow.execScript("document.getElementsByName('q')(0);")
In the same way, you have functions to execute a .js file itself. Here they are
  1. 'For browser object
  2. Browser().EmbedScript
  3. Browser().EmbedScriptFromFile
  4. 'For Page and frame objects
  5. Page().RunScript  / Frame().RunScript
  6. Page().RunScriptFromFile  / Frame().RunScriptFromFile