Wednesday, June 12, 2013

C#: Inserting duplicates into sorted List

I wanted to have a sorted list, with the ability to have items duplicate, built in sorted collections doesn’t allow duplicate values, so I used a List, and for effeciancy, I maintained the items sorted in it while insertion, instead of calling the sort method with each insertion
To insert in the list sorted, I used the binary search method, to find the first item less then or equal to the inserted item

Suppose we have a sorted list “list”, and we want to insert item i keeping it sorted


    List<int> list= new List<int>();

    int index = list.BinarySearch(i);
    if (index < 0)
       list.Insert(~index, i);
    else
       list.Insert(index, i);

Another way of having sorted list with duplicates is to use a normal sorted list and a custom comparer

    public class DuplicateKeyComparer<TKey>:IComparer<TKey> where TKey : IComparable
    {
        public int Compare(TKey x, TKey y)
        {
            int result = x.CompareTo(y);

            if (result == 0)
                return 1;   // Handle equality as being greater
            return result;
        }
    }



Only draw back of this method is that we can't find elements or remove, we can only iterate over them, as using the above comparer would always return 1 and won't match any elements.

Monday, April 8, 2013

Running Selenium tests from Visual Studio – Step by step

From Firefox, record new selenium test, for example for Google search

2013-04-07_13-14-39

Export the test to C# web drive

2013-04-07_13-15-06

This is how it looks like after export

2013-04-07_13-15-50

Open visual studio, create new test project

2013-04-07_13-16-36

By default the file UnitTest1.cs is created

2013-04-07_13-25-47

Create a new cs file, name it for example GoogleSearch

2013-04-07_13-25-54

Copy the code from the cs file generated by selenium and paste it in the file GoogleSearch.cs

2013-04-07_13-26-42

Add reference in the project to Selenium .Net WebDriver, WebDriver.dll and WebDriver.Support.dl

2013-04-07_13-29-322013-04-07_13-29-07

Now the references added, you can see that the editor recognizes the selenium name spaces

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

2013-04-07_13-29-40

Find and replace “TestFixture” to “TestClass”, “SetUp” to “TestInitialize()”, “Test” to “TestMethod”, “TearDown” to “TestCleanup()”

2013-04-08_00-31-32

To run with Chrome, download chromedriver.exe from https://code.google.com/p/chromedriver/downloads/list, place it in the bin folder

2013-04-08_09-45-49

Change the driver in the code to Chrome then run the test

driver = new OpenQA.Selenium.Chrome.ChromeDriver();

2013-04-08_09-45-09

Running the test, chrome will open, and Google search page will be displayed, displaying search results then closed

2013-04-08_09-51-36

This was a step by step example on running selenium test from visual studio

Wednesday, December 19, 2012

jmeter: preparing data or recycling data with JDBC PreProcessor or JDBC PostProcessor

I was doing testing on some records, and to be able to run the test again, the status of the record must be set to specific value

To be able to do that, I thought of doing this by trigger on the DB, but unfortunately, I was working on the client DB and I didn’t have access to create triggers

So I thought of doing that from jmeter, and it worked perfectly

First I added JDBC Connection Configuration as in the image below

2012-12-19_17-35-56

Configured this as below

Variable Name: conn

(Variable name should be any name, that will be used later)

Database URL: jdbc:db2://127.0.0.1:50000/QAT_MOI_DB1T;

(Database URL is the JDBC URL for the database)

JDBC Driver class: com.ibm.db2.jcc.DB2Driver

(Make sure to place the drivers in the lib directory inside jmeter directory. you will need to restart your jmeter to for them to work correctly, in my case I was working with DB2 database, so I copied the files db2jcc.jar and db2jcc_license_cisuz.jar to the lib directory)

Username: user

Password: password

2012-12-19_17-37-27

Then I added JDBC PostProcessor at the first request in the script

2012-12-19_17-39-47

Configured it,

Variable Name: conn

(variable name is same as above in the configuration element)

Query Type: update statement

Query: update records set status=1 where id=${id};

I was able to insert parameters in the query same way I do in the requests

By this way I was able to reset the data before every cycle

You can also add JDBC PostProcessor at the last request, to do some other post processing

Sunday, December 16, 2012

jmeter with spring webflow

I have been using jmeter for a while with spring webflow application, I have some experience with it now that I would to share

Studying the recording of spring webflow application, I noticed the following

There are requests that doesn’t have anything inside, that are followed by requests with execution parameter as in the images below

Request without any parameters, let’s call them initial requests

2012-12-16_09-46-29

Request with the “execution” parameter, let’s call them execution requests

2012-12-16_09-46-38

Or it could be request with execution parameter in the query string, and the view state parameter, execution in query requests

2012-12-16_09-52-47

To be able to replay the scenario,  with multiple iterations, and multiple users, the execution parameters and view state needs to be parameterized, to be extracted and used in next requests to maintain the execution flow correctly

I found out by trials and from the results from the results tree, that initial requests are followed by sub samples that contains the execution parameter for the consequences request

Also found out that new execution is generated at the end of execution in query requests

2012-12-16_16-20-20

But there was a difference in the extraction of the execution parameter in that case, it comes in the body

So for my scenario, in order to work correctly, I added 2 types of extraction rules

One after initial requests, that checks the sub sample URL only with the below settings

Apply To: Sub-samples only

Response Field to Check: URL

Regular Expression: execution=(.+?)$

2012-12-16_16-25-04

Another one at the end of each request with execution in the query string, with the below settings

Apply To: Main sample only

Response Field to Check: Body

Regular Expression: execution=([^"]+)"

2012-12-16_16-30-03

Then in consecutive requests, replace the execution parameter with the extracted parameter, in my case here I named it ${e3s2}

2012-12-16_16-39-15

2012-12-16_16-42-56

Sunday, December 2, 2012

Method not found exception with WCF

I had a WCF client for a web service, it was working fine from my machine, when I deployed my application to other machines, I was getting the below exception

System.MissingMethodException: Method not found: 'Void System.ServiceModel.Channels.SecurityBindingElement.set_AllowInsecureTransport(Boolean)'.

When I searched online I found out that this could happen when .NET framework is not installed. I tried installing .NET framework 3.5 SP1 on these machines, but it was always failing since this machines OS is windows 7, and I was getting error that I should use “Turn windows features on or off” to install .NET framework 3.5, which was already installed by this way

I searched more, and I found out that there is hot fix for WCF that adds AllowInsecureTransport  property to the SecurityBindingElement class, and i was using this on my WCF client

http://social.msdn.microsoft.com/Forums/en-US/wcfprerelease/thread/c12cf16f-20b9-4204-b05c-342fc33d0727/

I found couple of updates for .NET framework 3.5 SP1, non of them installed successfully, there were errors in setup, either product not found, or things like this

Finally I found one that I was able to install from the link below

http://support.microsoft.com/kb/976462/en-us

I downloaded the x86 file Windows6.1-KB976462-v2-x86.msu from https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=23806 , installed it on the machines, then the application worked without any exceptions

Thursday, November 29, 2012

Changing DB2 codepage

While executing sql command in IBM db2 control center editor, I was getting error

SQL0332N Character conversion from the source code page "" to the target code page "" is not supported. SQLSTATE=57017

Searching for this, I found that I need t change the code page of the client to be the same as the table I am selecting from

To do this:

Open a command prompt

Go to C:\Program Files\IBM\SQLLIB\BIN\ or just make sure it exists in the windows %PATH%

Type db2set db2codepage=1208 then press enter

In my case the target codepage was 1208, it needs to be checked first before setting it

Monday, November 26, 2012

Funny error message

“An internal error occurred while showing an internal error” , very indicative

image

Saturday, November 24, 2012

WCF client to java web service with username token, timestamp and nonce

I was trying to call a java web service that is implementing username token
My first attempt to call the service, I was getting errors about missing username and password
I followed the steps in several articles online to add it like below
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
doing so, I was getting another error about missing timestamp
I followed some other steps from another article  here to add it
#region Begin Magic
BindingElementCollection elements = bean.Endpoint.Binding.CreateBindingElements();
elements.Find<SecurityBindingElement>().IncludeTimestamp = true;
bean.Endpoint.Binding = new CustomBinding(elements);
#endregion
Still I was getting errors
Lastly i found very useful information here and here
I downloaded the sample code from MSDN blog, from this link
Then I used 2 projects from this sample code, the Common project, and the UsernameTokenLibrary, called the java service as exactly in the client project
                Binding userNameBinding = BindingHelper.userNameBinding();
                EndpointAddress serviceAddress = new EndpointAddress("http://localhost/servicemodelsamples/service.svc");

                // Create a client with given client endpoint configuration
                channelFactory = new ChannelFactory<IEchoService>(userNameBinding, serviceAddress);

                // configure the username credentials on the channel factory 
                UsernameClientCredentials credentials = new UsernameClientCredentials(new UsernameInfo("User1", "P@ssw0rd"));

                // replace ClientCredentials with UsernameClientCredentials
                channelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials));
                channelFactory.Endpoint.Behaviors.Add(credentials);

                client = channelFactory.CreateChannel();


Doing that, I was getting another error this time “PasswordDigest was supplied, but only http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText is supported”

I then modified the code little bit to send password instead of password digest, in the function WriteTokenCore in UsernameSecurityTokenSerializer.cs in UsernameTokenLibrary project

Commented the lines that writes the password digest

writer.WriteStartElement(Constants.UsernameTokenPrefix, Constants.PasswordElementName, Constants.UsernameTokenNamespace);
writer.WriteAttributeString(Constants.TypeAttributeName, Constants.PasswordDigestType);
writer.WriteValue(c.GetPasswordDigestAsBase64());
writer.WriteEndElement();

And added new line to write the password

so the code would look like this

                writer.WriteStartElement(Constants.UsernameTokenPrefix, Constants.UsernameTokenName, Constants.UsernameTokenNamespace);
                writer.WriteAttributeString(Constants.WsUtilityPrefix, Constants.IdAttributeName, Constants.WsUtilityNamespace, token.Id);
                writer.WriteElementString(Constants.UsernameElementName, Constants.UsernameTokenNamespace, c.UsernameInfo.Username);
                writer.WriteElementString(Constants.PasswordElementName, Constants.UsernameTokenNamespace, c.UsernameInfo.Password);
                writer.WriteElementString(Constants.NonceElementName, Constants.UsernameTokenNamespace, c.GetNonceAsBase64());
                writer.WriteElementString(Constants.CreatedElementName, Constants.WsUtilityNamespace, c.GetCreatedAsString());
                writer.WriteEndElement();

After doing the above modifications, I was able to call the java web service successfully complying with username token

Thursday, October 18, 2012

Folder options: Setting “show hidden files and folders” doesn’t work after virus infection

After removing a virus from my friends machine, showing hidden files wasn’t working, whenever I set the folder options settings for hidden files and folders, to show all files, it is reverted back to hide them

I found this post that solves this problem

http://techsalsa.com/fix-the-problem-of-show-hidden-files-and-folders-getting-reset/

Steps to fix the problem:

1.    Now open registry editor and navigate to the entryHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folders\Hidden\SHOWALL

2.    On the right side see the entry named “CheckedValue”, if the value is set to ’0′ change it to ’1′.

3.    The “DefaultValue” under the “CheckedValue” should be set to ’2′.

I followed the steps, then I was able to view hidden files

Thursday, August 2, 2012

jmeter: Adding the same item for several requests

I wanted to add a regular expression extractor to several requests, I was delighted to discover that the jmeter interface is very smart in a way that it understands when you past an object copied from it..

So all I did was to copy for the regular expression extractor, and did a paste on each request I wanted to add this extractor to it.

You can do copy and paste with right click then selecting copy/paste, or you can do it by the keyboard shortcuts, ctrl+c, ctrl+v

I found also that I can do copy/paste for different things, like the HTTP Requests, Recording Controller, etc…

One more thing also that was nice for me, the test plan is saved as a XML file, so when I wanted to modify some value for all my regular expression extractor, I just opened the plan with a text editor and did a Find and Replace for the value I wanted to change, and did a Replace All command, all values was changed with just couple of steps