Thursday, February 07, 2013

Custom Wait For - Selenium

Have you been in the following situations:

- Selenium driver fails to find an element because the web page uses AJAX
- The web element is present but selenium driver fails to find the element intermittently

These were two situations which prompted me to write my own custom WaitFor class which has two methods:

- Wait for a javascript code to execute until a specified time-out
- Check if element is present until a specified time-out

Here is the WaitFor Class:
(I did see these solutions in a blog discussions or forum. As soon as I spot the source, I will reference it).
   
    public class WaitFor
    {
        private bool JSEval(IWebDriver driver, string script)
        {
            object result = ((IJavaScriptExecutor)driver).ExecuteScript(script);
            if (result == null)
            {
                return false;
            }
            else
            {
                return (bool)result;
            }
        }

        public void JSWait(IWebDriver driver, string script, long timeout)
        {
            int interval = 500;
            long waited = 0;
            while (!JSEval(driver,script) && waited < timeout)
            {
                Thread.Sleep(interval);
                waited += interval;
            }
        }

        private bool IsElementPresent(IWebDriver driver, By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
        public void WaitForElementPresent(IWebDriver driver, By by, long timeout)
        {
            int interval = 500;
            long waited = 0;

            while (!IsElementPresent(driver,by) && waited < timeout)
            {
                Thread.Sleep(interval);
                waited += interval;
            }
        }
    }

Here are some of the reasons it works for me:
- Even though Webdriver 2 is supposed to wait for the page to load before checking for element to be present, it does not work always. The WaitForElementPresent method waits for the specified timeout and keeps checking every 500 ms until the timeout expires
- There are multiple ways to wait for AJAX functionality to finish loading, JSWait method above works like a charm for me

Here is how to call these methods from code:

   
        WaitFor ws = new WaitFor();
        // Check for element to be present, wait for 10 seconds before timing out
        ws.WaitForElementPresent(driver, By.XPath("//span[text()='Find Element']"), 10000);
        // Wait for the javascript code to return true before executing the next statement
        ws.JSWait(driver, "return Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();", 500);
        ws.JSWait(driver, "return !(Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack());", 500);  


Sunday, January 27, 2013

Selenium, FireFox, Firebug and NetExport

How do you get the firebug enabled in Firefox for your selenium tests?

Step 1: Download Firebug and the add-on net
Step 2: Initialize a new FireFoxProfile and add the extensions to the location of the .xpi files for firebug and net export
Step 3: One key statement that I initially missed was:
profile.SetPreference("extensions.firebug.allPagesActivation", "on"); 

Step 4: Enable the auto export property and give the location information

Below is complete code:


               
        [ClassInitialize]
        public void SetupProfile()
        {
            FirefoxProfile profile = new FirefoxProfile();
            try
            {
                profile.AddExtension(@"C:\LocalMachineUser\AppData\Roaming\Mozilla\Firefox\Profiles\0z8cb853.default\extensions\firebug@software.joehewitt.com.xpi");
                profile.AddExtension(@"C:\LocalMachineUser\AppData\Roaming\Mozilla\Firefox\Profiles\0z8cb853.default\extensions\netexport@getfirebug.com.xpi");
            }
            catch (IOException ioe)
            {
                Console.WriteLine("Exception occurred loading the profile files " + ioe.Message); 
            }
            profile.SetPreference("extensions.firebug.currentVersion", "1.11.1");
            profile.SetPreference("extensions.firebug.previousPlacement", 1);
            profile.SetPreference("extensions.firebug.onByDefault", true);
            profile.SetPreference("extensions.firebug.defaultPanelName", "net");
            profile.SetPreference("extensions.firebug.net.enableSites", true);
            profile.SetPreference("extensions.firebug.allPagesActivation", "on"); 
            profile.SetPreference("extensions.firebug.netexport.defaultLogDir", @"D:\TestFiles\_HAR");
            profile.SetPreference("extensions.firebug.netexport.alwaysEnableAutoExport", true);
            driver = new FirefoxDriver(profile);
            baseURL = "https://companyurl";
            verificationErrors = new StringBuilder();
        }


Wednesday, September 12, 2012

Refresh a page with Postback and Re-submit the form

In one of the applications, the page was doing a postback on a huge data set and sometimes it used to time-out in the Staging environment. The behavior was seen very rarely in Production environment. The application itself stored the data every 8 seconds in a temp table so there was no loss of data. But, from the automation script perspective, I had to write code to refresh the page and do the post-back again if it failed the first time (see below).


Refresh any page using Webdriver and Click "OK" on the pop-up / alert box:
               
driver.Navigate().Refresh();
driver.SwitchTo().Alert().Accept();

When you refresh the page, you may get the alert box (see picture below) and you will have to accept. The second line of code above does this step.


Now, if you want to re-submit the data, search for the WebElement which does the submit and Click again.

Monday, August 06, 2012

Ubuntu, Compilers and Wireless

I recently started preparing for interviews and wanted to brush up my C++ and Perl skills. I had an old laptop that I had not used in many years. I tried to install Ubuntu but the latest version of Ubuntu was not compatible with my laptop configuration. So, I installed Lubuntu instead and so far it is rocking.

Compilers that I needed where easy to get, you can use the following commands to install all the packages that you want:

sudo apt-get install

For example:

sudo apt-get install g++

sudo apt-get install perl

I did have trouble connecting to my Wireless internet and had to do some special setup. I found the solution in the thread, solution posted by wildmanne39 (response #7):

http://ubuntuforums.org/showthread.php?t=1859446

- download the b43.zip file and drop the file in your Desktop
- Disconnect your wired connection if connected
- Right-click the zip file and select Extract here
- open a terminal and run the following commands-


sudo mkdir /lib/firmware/b43
sudo cp Desktop/b43/*  /lib/firmware/b43
sudo rmmod -f b43
sudo rmmod -f ssb
sudo modprobe b43


Worked like a charm.


Friday, July 20, 2012

Test Automation and Selenium

After trying to write an automation test suite for my companies website using Visual Studio Test tools, I have recently started using Selenium to automate the web tests.

I was really excited when Microsoft came up with their testing tools with their new Visual Studio license way back in 2007. My excitement was short-lived because it was not easy to get past all the issues that you need to troubleshoot to get the web test working and time was a big constraint with the projects. We did not have enough time and resource to spend on writing the automated scripts and when we were ready after the application went live, there were already changes being made to the application functionality. Also, all the application that I was working on were totally data driven which also complicated the automation. Since, the tool was new, I could not find enough articles and documentation to troubleshoot the issues I was facing with VS Test.


One cool thing that I did with VS Load Test was to run a performance test to check the database servers. The SQL 2008 database cluster were storing the transaction data and log data on a ISCSI unit. This is how I did it:


  • First I picked the most intensive query that was being executed in our Production database server. We ran a trace on the Production database over a period of time to identify this query
  • Used this query to write a SQL unit test
  • Designed a load test to call this SQL unit test multiple times and concurrently
  • Monitored the performance of the database servers
We did find one issue by this performance testing. The connection between the database server and the ISCSI unit was defaulted to 100 MBPS and it was actually a 1 Gigps connection. The data was getting choked while writing to the storage unit and we changed the default value to fix this issue.

Automation is cool but if the application is constantly changing and there is not enough time to maintain the automation scripts, it will be better to stick with manual testing.


Now, after contemplating for sometime, I have decided to take the automation route again. Thinking more carefully on what I can automate and what I cannot. I have also started using Selenium to write my automation scripts.


Wish me luck!!

Topics I am interested in.....

Writing a blog post after a very long time. I have learned a lot in the past few years and plan to write my experiences working on the following:

  • LINQ to XML
  • Entity Framework
  • Running code in parallel
  • Test Automation
  • Data Warehouse
  • SQL PIVOT

My current passion is Test Automation, so I am going to start with that.

Thursday, November 15, 2007

Adobe 8.1.1 Installation Issue

If you ever have an issue with installing the Adobe 8.1.1 software in your machine, here is a solution you can try:

http://support.microsoft.com/kb/290301

Use the Windows Install Clean up to remove all previous existing Adobe version and try re-installing the 8.1.1 version.

Problems I faced -
  • Internal Error+2753 AND Updater.api_NON_OPT message
  • The installation that you are looking for exisits in a network location not accessible (this is if you have a office laptop and Adobe was installed from a network location)

Saturday, February 10, 2007

MS-Build - My own build clean-up service

After days of working on manual testing projects, I had some time to spend on some interesting project. I am working on creating a custom based build clean-up service. Here are two useful references if you want to develop the tool yourself -

http://notgartner.wordpress.com/2006/05/22/release-build-clean-up-service-for-team-foundation-server/

http://notsosmartbuilder.blogspot.com/

I extended the idea to develop a front-end which will allow you to chose the builds to be deleted from a list apart from the build clean-up service running on the TFS.