Selenium Web Driver in C#: How to continue script on the already opened browser instance.

This was the very first challenge I encounter when I begin to do scripting in Selenium. Sometimes my scripts failed in between due to any unexpected dialog. To continue my scripts from the point where my scripts failed seems to be impossible in Selenium. So every time, my scripts failed, I have to start execution from the very beginning. This was taking a lot of time in debugging my scripts. So I desperately needed a solution.

After a couple of searches and different trial and errors, I devised a strategy.

I noticed that when I initialize the Firefox WebDriver, It opens the new browser instance. If I don’t close that browser instance and I initialize the Remote WebDriver instance, it uses the already opened FireFox browser. When I noticed that, its easy to write code to handle this and use it according to my way. So here is the code.

IWebDriver WebDriver = null; 

try
{
System.Uri uri = new System.Uri("http://localhost:7055/hub");
WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
Console.WriteLine("Executed on remote driver");

}

catch (Exception)
{

WebDriver = new FirefoxDriver(firefoxProfile);
Console.WriteLine("Executed on New FireFox driver");

}

The above code is easy to understand. If my Firefox browser is closed, the try block will fail and catch block will work, the script will open the new Firefox browser using FirefoxDriver.
If Firefox browser is already opened (due to previous script execution) , the try block will work and will initialize the Remote WebDriver. This Remote WebDriver will use the already opened FireFox browser and scripts will start where you want them to start from.

This strategy helps me a lot and saves an ample amount of time in my automation. I hope this post will help you out too.

Coded UI Test : Testing all assertions in a single test case, even any assertion is failed.

Many of us encountered this situation in which we have put more than one assertion in our single test case. The failure of any one assertion cause the script to break. What if we want to test all the assertions and see at the end that which assertion fails and which has passed.

The above can be explained with a simple example. If we want to test the login of google docs, we will test basic functionality of that login box in a single test case. We assume we want to test four simple assertions.

Continue reading

Coded UI Test: How to continue execution after test case is failed.

When we are executing a batch of test cases using ordered test, in some situations we want to continue  the execution even if one of the test cases failed. After execution we want to see the complete report stating passed and failed test cases. This strategy is good for testers who want to execute test cases when they are not on seat.

1. For easy scenarios.

  1. open ordered test case by double clicking on it.
  2. there is a check box “Continue after failure” on the bottom left. Make sure it is checked.

orderedtestcaseEditor

  1. Execute this ordered test. you will notice that test cases are not stop executing even if any test case is  failed.

2. For Difficult Scenarios

Sometimes test cases fail due to an expected message box. In that kind of scenarios, subsequent test      cases will begin to fail one by one.

For that kind of scenario, correct strategy is to check if the test case status is failed, if yes then move the application under test to a base state and subsequent test cases should start from that base state.

We can achieve this in coded ui test by the use of coded ui test. Following are the steps.

  1. Open your testcase.cs file
  • Un-comment the testCleanup method.

  • Write below code in your test cleanup method.

  • <br />[TestCleanup()]
    public void MyTestCleanup()
    {
    
    if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed) //check if the test case is failed
    {
    
    // write some code here to move your application to a base state , for e.g. restart your application
    
    // Process.Kill("yourapplication.exe");
    
    // Playback.Wait(3000);
    
    //Process.Start("yourapplication.exe");
    
    }
    
    }
    
    
    1. Note that testcleanup method executes after each test case.

    I hope the above scenarios will help test engineers to execute test cases in a much better way.

    Cheers.


    Measuring Performance of Testers: James Bach Opinion

    In my previous post, I shared a link of the presentation by Cem Kaner. Today I came across this wonderful article from James Bach on the same topic i.e. measuring performance of testers. Without going into further details, I think I should just share the link here so my readers can read what this man has to say about the topic

    Article Link

    Regards,

    Saad

    Measuring the Effectiveness of Software Testers: Presentation by Cem Kaner

    There is always a question in the minds of software quality assurance managers. How to measure the effectiveness of their team members?

    Is it Bug Count? Hours at work ? Certifications? Peer Ratings? or some other criteria?

    This question is well addressed by Cem Kaner who is perhaps best known as an advocate of software usability and software testing.

    I came across a presentation on his website. Take a look and I hope we will get the answer.

    Presentation Link

    Regards,

    Mohammad Saad

     

     

     

    Coded UI Tests: Tutorials

     

    When I started working on Automation, my first tool was Microsoft Visual Studio 2010 in which Microsoft has provided an excellent platform for functional testing which is called Coded UI Test.

     

    Before working on this tool, I learned about it from my good teacher THE INTERNET and my teacher has provided me valuable information about how to use it. So I thought to share the most useful links here.

     

    Here is the list of links:

     

    Getting Started with Coded UI

     

    Coded UI Basic Walkthrough: very easy and step by step tutorial. Covering only the basics.

     

    Understanding the Generated Code by Coded UI Test Part1

     

    Understanding the Generated Code by Coded UI Test Part2 (This article in two parts helps you to understand the code which is generated by Recording and Playback)

     

    Modifying Generated code in a Coded UI Test: When you generate the code after recording and playback , this article shows you how to modify the generated code.

     

    Test Automation Guidance using VS 2010 Coded UI   (.pdf format) (excellent PDF covering step by step guidance of creating your first coded UI test project.Very detailed and covering 80% of the concepts. My personal favorite. )

     

    Coded UI sample Framework (Visual Studio Solution)  ( this is the sample project to get you started with the real life application testing)

     

    Advanced Concepts:

     

    How does “Coded UI test” finds a control ??  (This article gives concepts about how the playback engine finds the control in your application)

     

    Walkthrough: Using multiple Coded UI maps in test automation  (It is extremely necessary to use separate UI Maps for each module in testing real life applications. This article explains how to do it)

     

    Best Practices for Coded UI Tests: Best Practices while making Coded UI Tests from Microsoft.

     

    Other than above you can visit other posts of this blog in which I have listed some issues which I faced while making Coded UI Tests and came across a solution.

     

    Keep visiting.

     

    Cheers.

     

     

    Horrible Experience of using Test Explorer in VS 2012

    Today I install VS2012 on my virtual machine to see if there any good feature related to coded UI tests.

    The first thing I encounter that there is no test view window available which was present in VS 2010.

    Instead there is a new window called Test Explorer. I build my project and all my tests becomes visible in the Test Explorer. Now the real horrible experience begins. I was trying to group my test according to class name like I used to do it in Test View. Too my surprise there are only two options for group by . Either by duration time or by test outcome.

    This was the not the first issue. I begin to explore Test Explorer and amazed how many features it is missing. some of them are:

    • we cannot “copy” exception messages from the unit test results.
    • we cannot control the columns as we like to see.
    • There is no refresh button to refresh test cases.
    • we cannot see that which test is currently running.
    • The summary page keeps displaying information about the previous run which is very confusing. It should display information about the current run.
    • With VS 2012 I  have to wait at least 18-20 seconds to see the results in the Test Explorer after my test is executed.

    Over all it was a big disappointment. I searched on the internet and found an update of VS 2012 link. They are saying that this update will provide some Group By features in the Test Explorer.

    There is also a suggestion posted that Microsoft should drop Test Explorer and bring back Test View in 2012.

    This was my experience. How about yours?

    Coded UI Tests: Executing a test case while desktop is not active.

    One of the biggest time consuming and productivity killer task for automation engineers is the time when they are executing their scripts. Not only scripts take long time to run but also while they are running, we cannot do any thing else. This is a desire of every automation engineer that he could do some other work (like developing other test cases) while the previous test case executes.

    Same case happens with me and I found a solution for that. Since coded UI tests require an active desktop screen to execute it, there are three ways to achieve this.

    1. Either you install visual studio on your development machine and install Test Agent on any other machine where you want to execute tests. When you will execute a test case on your development machine, it will be executed on the test machine PC and development machine will remain free for doing other tasks.  (This approach requires another PC. That means it is an expensive approach)

    2. You can set up a VM machine of Windows 7 (or any other) on your development machine. On the VM machine install Test Agent. When you will execute a test case on your development machine, it will be executed on the VM machine and development machine will remain free for doing other tasks. (This approach is less expensive)

    3. You can set up a VM machine of Windows 7 (or any other) on your development machine. On the VM machine install Visual Studio 2010 and get the latest version of your source code there with the help of TFS. In this approach you will have two benefits.

    1. You will be able to execute test cases right from VM machine
    2. You will be able to develop or modify test cases in the VM machine.

    In approach number 3, you can run the test cases from visual studio inside VM machine and minimize that window. The code will be executed while this VM window is minimized. You will be free to do other tasks as well. I personally follow approach number 3. It allows me to change my code in both environments and by using TFS I am also able to check in / check out my code at both locations and code remains always updated.

    What approach do you follow? and what are the benefits. I will love to hear it out.

    Cheers.

     

    Coded UI Tests: Error Resolution “COM object that has been separated from its underlying RCW cannot be used”.

    This error may come across a coded UI developer while executing test cases. This error is occurred if you are using a same static class across two test cases.

    For example in my scenario, I have created a static class called “MainCall”. In this class, I have put all the UIMAP class references. So that I can call any test case of any UIMAP from the “MainCall” class.

    The class is as below.

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using SaadAutomation.Maps.UIMap1;

    using SaadAutomation.Maps.UIMap2;

    using SaadAutomation.Maps.UIMap3;

    namespace SaadAutomation

    {


    class
    MainCall

    {


    private static UIMap1 _spMap;
    private static UIMap2 _cMap;
    private static UIMap3 _crMap;


    public static UIMap1 SPMap

    {
    get {
    if (_spMap == null)

    _spMap = new UIMap1();
    return _spMap;

    }

    }


    public static UIMap2 CMap

    {
    get{
    if (_cMap == null)

    _cMap = new UIMap2();

    return _cMap;

    }

    }


    public static UIMap3 CRMap

    {
    get{
    if (_crMap == null)

    _crMap = new UIMap3();
    return _crMap;

    }

    }

    }

    }

     

     

    I was calling functions like this in a test method.

    [TestMethod]


    public void ExampleTestCase1 ()

    {
    MainCall.SPMap.Method1();

    MainCall.SPMap.Method2();
    MainCall.CMap.Method1();
    MainCall.CRMap.CloseAllWindows();

    }

    [TestMethod]


    public
    void ExampleTestCase2 ()

    {
    MainCall.CRMap.Method1();
    MainCall.CMap.Method2();
    MainCall.SPMap.Method1();

    }

    When I run both of the above test methods together, I got this error message.

    System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

    So I searched on the internet and found the solution. It is just a setting change in the local.testsettings file.

    Resolution:

    To resolve this error, open your .testsettings file in any XML editor. This file is located under solution items.

    Add this line (marked bold)

    <Description>These are test settings for Trace and Test Impact.</Description>

    <Execution>


    <ExecutionThread apartmentState=”MTA” />

    Don’t forget to restart visual studio after that. Now you will be able to run your test cases and this error will not come inshallah.

    There is one side effect of adding this line. You will not be able to debug your test. See my another post about the mentioned issue.

    What other errors you encounter? Post here as comments and we will try to find the solution together.


    Coded UI Tests: When debugging, function evaluation time out

    Most of the coded ui developers may encounter this situation. When they want to debug a script and they want to add watch on a particular object or property, this message “function evaluation time out” may display.

    This results in frustration and developers have to manually find the bug.

    To overcome this situation I begin to research about this particular issue. I cannot find anything on the blogs and forums. They all seem to say one thing only that this error occurs due to bad programming practice etc.

    But luckily I found the solution and it is very simple.

    Just open your .testsettings file (Located under solution items folder) . My test setting name was TraceAndTestImpact.testsettings

    Open it with XML Text Editor.

    You will see something like this

    <?xml
    version=1.0
    encoding=UTF-8?>

    <TestSettings
    name=Trace and Test Impact
    id=1494f279-051d-465d-97a9-e57b59372376
    xmlns=http://microsoft.com/schemas/VisualStudio/TeamTest/2010>

    <Description>These are test settings for Trace and Test Impact.</Description>

    <Execution>


    <ExecutionThread
    apartmentState=MTA />

    Just comment out the selected line

    <?xml
    version=1.0
    encoding=UTF-8?>

    <TestSettings
    name=Trace and Test Impact
    id=1494f279-051d-465d-97a9-e57b59372376
    xmlns=http://microsoft.com/schemas/VisualStudio/TeamTest/2010>

    <Description>These are test settings for Trace and Test Impact.</Description>

    <Execution>

    <!–<ExecutionThread apartmentState=”MTA” />–>

    And restart visual studio.

    You will now be able to debug without having function evaluation time out message. Inshallah.

    Cheers J

    Rafaela Azevedo

    Talks about QA, Test Automation, Blockchain and Web3

    Believe !!! There are 101 ways to Automate

    Come let us explore together...

    selenium online training with expert

    Any One can Learn Selenium, It's simple, It's robust

    Dan Ashby

    Blethering About Testing

    Sharing ideas, Sharing experiences

    Jaagrugta Failao - By Sunil Singhal

    Ryan Burnham's Blog

    Just another WordPress.com site

    Emerging .NET Devs

    A Registered User Group under INETA

    DreamXtream's Blog

    Its all about life, a geek's life.

    Ahmed Nasr's Blog

    Talk about ALM, TFS and AGILE

    WordPress.com

    WordPress.com is the best place for your personal blog or business site.