Browse Tag: WebDriver

Selenium versus HP LeanFT

Selenium has carved a niche in the software testing tools world, and has a dedicated user base with consistently increasing adoption in the last few years. While this tool was always popular with Open Source enthusiasts since the RC days, we now have increased acceptance in enterprises as well. In the last couple of years, quite a few Fortune 500 companies and banks have diversified their skill-base and tool portfolio with Selenium, in addition to traditional HP toolset.


Let’s look at a recent product offering in the market-place, which could be very relevant to continued growth of Selenium user base. This post describes a brief analysis of HP LeanFT with respect to Selenium. Unsure if Learn-Selenium blog is the right medium for comment on HP tool, but it seems that this tool is HP’s response specifically to counter the increasing popularity of Selenium in the testing world.

Selenium – what has worked

1. Cost. Cost. Cost.

Cost by far remains the biggest differentiator for Selenium. Being open-source, this becomes the automation tool of choice for browser based applications in small and medium enterprises. Vibrant user community and strong support base help mitigate concerns around open source usage in enterprises. HP licensing is disproportionately expensive.

2. Object Identification

As web technologies get advanced, we have third party toolsets that cause issues in object identification during automation with HP QTP-UFT. AngularJS, Ajax, Oracle Forms are examples. While HP keeps refining with every version, there are easy alternatives. Selenium uses XPATH, and identifies objects where we face challenges in detecting unique properties using QTP.

3. The Buzz around Dev-Ops

Dev-Ops is the approach of leveraging test assets and automation in Development and Operations. With increasing agile adoption, the lines blur between traditional roles of developer, functional tester, and test automation specialist.

Application Development Leads and Architects are interested in test-automation for continuous integration, build sanity, and unit-testing. This is a community with expertise in Java/C#, and very comfortable with IDEs like Eclipse. They find it difficult to digest that anything worthwhile can be done with VBscript. These stakeholders are often key influencers in management decision-making on Enterprise Tool Usage, leading to increased acceptability for Selenium in large enterprises.

4. Multi-browser Testing

Even today, Selenium is a clear winner in cross-browser testing against UFT. Multiple UFT add-ins have to be tried for different browser versions and we have compatibility issues. Examples – UFT 11.5 does not support Chrome v40, you need to downgrade to Chrome v36 for automating scripts, which would not be in sync with production. HP license upgrades do not keep pace with browser version changes.

Selenium – where it falters

1. End-To-End Automation

Large Enterprises have multiple applications under test and end-to-end testing flows that traverse more than one application. Any tool restricted to browser testing would limit coverage of automation. Example – A very common scenario in banking systems would be transaction initiated on front-end web application that would have validation step on mainframe and backend database.

2. Object Identification and Script Build Productivity

While XPATH usage helps identify problematic objects, we have lot of instances where QTP can easily get unique property index, which may be cumbersome in Selenium. HP QTP/UFT are feature rich and easy to use. Invariably, script build productivity is higher as compared to Selenium, although this would vary based on application under test.

3. Skill-base and staffing

HP QTP has been the industry leader since ages, and sourcing experienced automation testers skilled in coding with VBscript is relatively easier. In comparison, ramping a project team on Selenium skills may be more of a challenge. Note- this is a snapshot as of mid-2015, things change very fast.

HP LeanFT – What’s on offer

Circa 2015 July, HP has introduced LeanFT along with the UFT 12.5 upgrade. Refer the figure below.

image

    Figure: Reproduced from datasheet on HP home-site

We have detailed below features and observations of LeanFT, which bear relevance to the analysis above.

1. LeanFT provides Support for multiple IDEs (Eclipse, Visual Studio) and coding languages (Java, C#).

HP keeps pushing features every few years to retain its dominance (and premium licensing), BPT was introduced to sell the concept of BA–Tester, now LeanFT is built to whet the interest of the Dev-Tester. IDE and language flexibility would make the tool popular with the Application Developers community.

2. Dev-Ops and CI support. LeanFT supposedly integrates well with standard SCMs, build/deploy tools and approaches, as compared to UFT which is heavily ALM centric. Selenium was an easy choice compared to QTP in building CI/CD solutions closely integrated with dev-workflows. This may change with LeanFT and needs to be investigated.

3. Object identification & Multi-browser support. LeanFT has an object identification engine similar to Spy in UFT which is installed as plug-in to the IDE. This is an advantage and could be quite powerful. HP datasheet indicates LeanFT to be light weight tool with good cross browser support. This could potentially address a gap in HP toolset where Selenium has an edge over QTP.

4. Cost: LeanFT licenses are free for HP UFT12.5 users, this helps penetrate the existing user base in large organizations and halts the move to Selenium.

5. We have clean integration of LeanFT with UFT12.5, which aids in end to end automation. This permits automation beyond browser based applications, a clear advantage over Selenium in organizations like banks and insurance companies.

6. QTP-skilled staff have no learning curve to start automation using LeanFT. Existing resources can be used for automation, as against Selenium projects which need well-thought out staffing and training strategy.

To Conclude:

It seems that LeanFT has been specifically targeted at the Selenium user base – would be very interesting to see how this pans out in the marketplace in the next 12 months. Much of the recent increase in Selenium projects has been because of large organizations seeking to diversify their tools portfolios. Technology trends are extremely dynamic and you prepare today for anticipated changes or risk obsolescence. Would LeanFT stem the tide? – We would watch the events and follow up on this in another six months.

Jenkins and Continuous Test Execution

Continuous test execution is an essential part of automation testing specially in regression run. If you need to start execution manually means your return of investment is not as good as needed. There are multiple ways to do this. Developers in many companies use Continuous Integration (CI) tool like Hudson, Rational Team Concert for building application multiple times in a day where codes are checked in from multiple location. You can use same feature of building application to perform testing as well. You can build your own in-house application to perform the same task. I will give idea for the same at the end of the blog.


I use WebDriver for automation testing in .NET framework.  I heard that many companies use Jenkins for CI and even for testing so thought to implement my tests written in C#. After some research on internet I was able to implement a basic skeleton for continuous testing. In order to understand and to customize Jenkins we will go Step by Step.

  1. Download Jenkins.
  2. Run and verify Jenkins.
  3. Install as a Window Service.
  4. Configure Jenkins as a slave. (Optional. Read only if you want to learn.)
  5. Create Job to execute test.

1.       Download Jenkins:

In order to download Jenkins, Go to http://jenkins-ci.org/ and download ‘Latest and greatest’ version of Java web archive (.war) file and save it to some location.

2.       Run And Verify Jenkins:

Open Command window and navigate to the location where you have downloaded *.war file. Execute following command (I saved Jenkins.war file in D drive)

  • Java –jar jenkins.war

You will see following output

image001

If you read all lines you will see that HTTP Listeners started at port 8080.

image002

3.       Install Jenkins as Window Service:

Node: If you install as window service, your tests will run in background.

Perform Step 2 if not yet done. Click ‘Manage Jenkins’ you will see following scree with various option.

image003

Click on ‘Install as Windows Service link. You will see following screen.

image004

Click on Install button. Once installation complete you will see following:

image005
Go to Window Services and verify if you can see there or not. image006 From now, you will not require to start Jenkins site on command line. You can directly start http://localhost:8080/ if your Jenkins service is running. NOTE: if you are using only window OS then you can directly install native package from http://jenkins-ci.org/ image007 You need to unzip the folder and install msi file. That will install and get ready with step 3.

4.       Configure Jenkins as a Slave:

This is most interesting part of Jenkins. Here you can configure your slave attached to Master which is ready in step 3. You can assign tasks to various slave to perform like test execution etc. Start Jenkins Website and click on link ‘Manage Jenkins’. You will see following option among various other options. image008 Click on the above option which will take you to the following screen. image009 Now you need to create Node that connected to master (Just like GRID (HUB-NODE)). To do that you need to click on ‘New Node’ link that should you to following screen. image010 Give some Name of Node and Click on Dumb Slave radio button that will active the OK button. Click on OK button. This will take you to the following screen. image011 You can click on Help icon image012 to understand more about the fields. I am giving following options # of executor : 1 ,  Remote FS root:  D:\Shared. Launch method: Save this. It will take you the following screen. image013 As you can see above, you can start that node by given three options. It is up to you, to choose but I prefer first option. Click on the Orange button with Launch. This will ask you to download slave-agent.jnlp. Please save this to the location where you have created your slave i.e. D:\Slave. Open command prompt and change directory to the location where you have saved this file and execute  following command Javaws slave-agent.jnlp aafter some processing you may see following windows: image014 This will disappear in few second and you will see Jenkins slave Agent screen with ‘Connected’ text on the screen. image015 If you go back to jenkin site and refresh page in Manage Node screen, you will see following screen. image016 You can install this slave into machine as window services using File menu given in ‘Jenkin salve agent’ screen so in future you will not require doing the whole process of starting slave via jlnp image017 image018 Click ‘OK’. This may ask your permission to run the program. image019 Will see that Jenkins slave agent window disappear and if you check in ‘services’ you will be able to see slave as services. image020 Go back to Jenkins site and Click on slave link the page and make sure that you can see slave running. image021 Now your slave is connected to your master and ready to server your for execution by added execution job assigned by you as given  in next section.

5.       Create JOB to Execute Test:

Once your slave node is running, go to home page of Jenkins and click on ‘New Job’. image022 This will take you to the following window. Provide Job name as per your choice and select radio button. ‘Build a free-style software project’. And hit OK. image023

This will take you the detailed screen and you can fill information as you needed. Few points are very important which you should consider.

  1. Execute test at specific Node.

image024

2. Regression Run repletion :

How frequent you want to execute test.

image025

There are various command you can give to give frequency of test execution ie. Every hour or at ever day at some specific time frame. You can Google the commands or I can write separate article about this if needed.

3. Execute Tests from various options (batch command preferred at least in my case)

image026

4. Post Execution:

Configure to send email or prepare report as NUnit/Junit repot etc.

image027

Once you save this job this will take you to the following screen.

image028

Click on  [Back to Dashboard] link that will take you the following screen.

image029

To execute test you can click on image030  . You will see following screen.

image031

I think much detail covers the basic understanding of Jenkins configuration for test execution. I hope this will give you are direction to configure Jenkins in your project. I use Webdriver in .NET environment and configure Jenkins for continuous execution and  it was successful implementation but later we decided to build our own internal application to specific for our own application which can do continuous execution and other tasks as well which is not possible via Jenkins like customized internal website with customized graph and tables but that’s another story.

I am going to write another article specifically how to execute tests using msbuild, msTest and Nunit and another article for JUnit.

Selenium with C#

Basic WebDriver and C# script for beginners

WebDriver Test using C# and Visual Studio 2013 Express.


One of my video is getting multiple hit for how to write WebDriver or selenium tests using C#. There are many tutorials available on internet for Java but not many for WebDriver and C#. I will teach you how to write test scenario in C#. I have visual Studio 2013 express edition.

Step1: Create WebDriver Project

Create new Project, and choose ‘Unit Test Project’, and give some name says; ‘AddItemToShoppingCart’ and hit OK button.

Create new Unit Test Project

You will see following folder structure and if you open UnitTest1.cs, you will see code

Step1.1C# Code In Visual Studio

 

Rename file, UnitTest1.cs to some logical name and class name inside the code as well.

Step2: Add WebDriver into reference.

Go to Manage NuGet Packages and search for WebDriver.

C# Manage NuGet Package

Select latest version of WebDriver and Selenium WebDriver Support Classes item and click Install button to install.

Webdriver and WebDriver Support files

After installation, you can see WebDriver and WebDriver.support in Reference under Solution Explorer.

Step2.2

Search ‘Selenium chrome’ and install Chrome Driver that will add chromedriver.exe into your project.

Step2.3

Step 3 (optional if you want to use NUnit instead of inbuilt UnitTestFramework in Visual Studio)

Now to you need to install NUnit before writing NUnit. Search NUnit in NuGet Package and Install. To use lean more about NUnit and attributes, please refer Learn Nunit tutorial. In this Tutorial I will Use predefined Unit Test Framework and its attributes.

Step 4: Write WebDriver Code

Now real implementation starts here. This article will talk about Visual Studio Unit Test tool which is even new for me too. This UnitTest namespace has three main test attributes which I am going to use.

  1. The [TestClass] attribute is required in Microsoft Unit Test Framework for any class that contains Test method and you want to run in Test Explore. Class with Attribute [TestClass] must have Test method.
  2. The [TestMethod] attribute is to assign a method inside [TestClass] to run actual test code.
  3. The [TestInitialize] attribute need to assign to the method which runs prior to running any [TestMethod] attribute method.
  4. The [TestCleanup] attribute method is to run after [TestMethod] method.

[TestInitialize] attribute method will have the code to do all setup like, defining any variable required for code, initializing WebDriver, opening url etc.

[TestMethod] attribute method will have actual test code which will run after [TestInitialize] and all kind of action and verification will take place in this method.

[TestCleanup] attribute method will be have code to clean up all kind of setting or action like removing any special setup or logout from application and close application. This method can have the code to preparing the reports etc.

Test Explorer will show only methods with attribute [TestMethod].

Step4.1

Step5: Start Writing Code

This section will have real coding.

We have two scenarios to implement

  1. We want to open an ecommerce website and pick nexus phone and add into cart and verify if cart has that item before proceeding to checkout.
  2. Check if you can open find makeup brush.

 

If you write both scenarios in steps:

 

Scenario 1 Scenario 2
1.       Open Brower2.       Open application under test says: donethedeal.com3.       Click on nexus phone link that will take you to the description page.4.       Click on Add to Cart and wait for confirmation if phone added to cart.5.       Open Cart link from top menu and verify if Phone is present there.

6.       Close the browser.

1.       Open Browser2.       Open application under test says: donethedeal.com3.       Open brush page4.       Verify that if item has description of brush

 

 

5.       Close the browser.

If you check above you will see that we have common steps in both scenarios which we can move to common area for each scenario and that will go to [TestInitialize] and last step in [TestCleanup.]

Before writing actual code, you need to add the reference for selenium into the code as follows;

using OpenQA.Selenium;

using OpenQA.Selenium.Chrome; //to use googlechrome browser.

using OpenQA.Selenium.Support;

[TestInitialize]

In this step you need to initialize the browser, let’s say Firefox using Firefox Driver. You initialize interface IWebDriver in the [TestClass] that controls the browser and have url for application under test so these will be available across the class.

Now you need to initialize the Firefox/chrome browser and once it is open, you need to open the url to cover step 1 and step2.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
using ExcelReader;

namespace AddItemToShoppingCart
{
    [TestClass]
    public class VerifyItems
    {
        IWebDriver driver;
        string url = "http://learnseleniumtesting.com/demo/";
        [TestInitialize]    
        public void TestSetup()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl(url);
        }
}

 

[TestCleanup]

This attribute method will run at the end so we want to close the browser here. You just need to call driver.Quit() to close the browser.

[TestCleanup]

public void Cleanup()

{

driver.Quit();

}

[TestMethod]

Method with this attribute will contains the actual code. Create a public method with some logical name and assign the attribute. For now keep the method blank build the solution.

Overall code should look like this;

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
 
namespace AddItemToShoppingCart
{
    [TestClass]
    public class VerifyItems
    {
        IWebDriver driver;
        string url = "http://learnseleniumtesting.com/demo/";
        [TestInitialize]
        public void TestSetup()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl(url);
        }
        [TestCleanup]
        public void Cleanup()
        {
            driver.Quit();
        }
        [TestMethod]
        public void AddPhoneToCartAndVerifyInCart()
        {
            //This is the placeholder to write actual code.
        }
 
    }
 }


 Test The Code:

Open Test Explorer window and you will see as below. To test the code click on the ‘Run All’ option in the Test Explorer windows. You will see new chrome browser instance launched and loaded www.donethedeal.com website and close the browser. You can add some wait in Test method in case if you want to make sure if it goes to TestMethod or not.

You can run that test in debug mode and you can put break point into your code and step through that. To run in debug mode you need to run test via Test Tool bar. Select the test in Test Explorer and Go to Test->Debug->Selected Test.

Test Code Implementation

Now we will work on implementation of test Steps.

// Click on nexus phone link that will take you to the description page

To perform above step. You just need to click on the application that will eventually take you the details page of the Nexus phone. Since we have only two products on the Test application then it is easy to write.

driver.FindElement(By.PartialLinkText("Nexus")).Click();
var summary = driver.FindElement(By.CssSelector("#product-18 > div.row > div.col-md-7 > div > div.product_meta > span > span")).Text;

You just need to click on the application that will eventually take you the details page of the Nexus phone. Since we have only two products on the Test application then it is quick to execute. If you run that you will notice that product description page appeared and then closes the browser. We need to have some verification framework. For that We can use Assert.

And code will look like this:

driver.FindElement(By.PartialLinkText("Nexus")).Click();
            var sku = driver.FindElement(By.CssSelector("#product-18 > div.row > div.col-md-7 > div > div.product_meta > span > span")).Text;
            Assert.AreEqual("10001", sku);

Assert call with is to verify items.

Later you can add following code to verify cart.

[TestMethod]
        public void AddPhoneToCartAndVerifyInCart()
        {
            
            driver.FindElement(By.PartialLinkText("Nexus")).Click();
            var sku = driver.FindElement(By.CssSelector("#product-18 > div.row > div.col-md-7 > div > div.product_meta > span > span")).Text;
            Assert.AreEqual("10001", sku);
            driver.FindElement(By.CssSelector("#product-18 > div.row > div.col-md-7 > div > form > button")).Click();
            //Verify if item added to cart. 
            driver.FindElement(By.XPath("//*[@id='content']/div/div/div[2][contains(text(), 'was successfully added to your cart')]"));

            //Clikc Go to cart\
            driver.FindElement(By.CssSelector("#content > div > div > div.woocommerce-message > a")).Click();
            var dd = driver.FindElement(By.XPath("//*[@id='content']/div/div/div/form/table/tbody/tr[1]/td[1]")).Text;
            
            //Verify If one phone given the cart.
            Assert.AreEqual("×", driver.FindElement(By.XPath("//*[@id='content']/div/div/div/form/table/tbody/tr[1]/td[1]")).Text);
            Assert.AreEqual("1", driver.FindElement(By.XPath("//*[@id='content']/div/div/div/form/table/tbody/tr[1]/td[5]")).Text);
            Assert.AreEqual("Proceed to Checkout", driver.FindElement(By.CssSelector("#content > div > div > div > form > table > tbody > tr:nth-child(2) > td > a")).Text);
        }

Execute the test either in run mode and or debug mode and see the result. It will be pass. If its failing then try to give some pause wherever required

 

Note: Avoid using any hard wait in script though.

 

In similar way you need to write another Test case where you just click on brush and verify the description and close the browser. And overall code will look like:

 

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
using ExcelReader;
namespace AddItemToShoppingCart
{
    [TestClass]
    public class VerifyItems
    {
        IWebDriver driver;
        string url = "http://learnseleniumtesting.com/demo/";
        [TestInitialize]
        public void TestSetup()
        {
            
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl(url);
        }
        [TestCleanup]
        public void Cleanup()
        {
            driver.Quit();
        }

        [TestMethod]
        public void AddPhoneToCartAndVerifyInCart()
        {
            
            driver.FindElement(By.PartialLinkText("Nexus")).Click();
            var sku = driver.FindElement(By.CssSelector("#product-18 > div.row > div.col-md-7 > div > div.product_meta > span > span")).Text;
            Assert.AreEqual("10001", sku);
            driver.FindElement(By.CssSelector("#product-18 > div.row > div.col-md-7 > div > form > button")).Click();
            //Verify if item added to cart. 
            driver.FindElement(By.XPath("//*[@id='content']/div/div/div[2][contains(text(), 'was successfully added to your cart')]"));

            //Clikc Go to cart\
            driver.FindElement(By.CssSelector("#content > div > div > div.woocommerce-message > a")).Click();
            var dd = driver.FindElement(By.XPath("//*[@id='content']/div/div/div/form/table/tbody/tr[1]/td[1]")).Text;
            
            //Verify If one phone given the cart.
            Assert.AreEqual("×", driver.FindElement(By.XPath("//*[@id='content']/div/div/div/form/table/tbody/tr[1]/td[1]")).Text);
            Assert.AreEqual("1", driver.FindElement(By.XPath("//*[@id='content']/div/div/div/form/table/tbody/tr[1]/td[5]")).Text);
            Assert.AreEqual("Proceed to Checkout", driver.FindElement(By.CssSelector("#content > div > div > div > form > table > tbody > tr:nth-child(2) > td > a")).Text);
        }
        [TestMethod]
        public void VerifyBrushDescription()
        {
            driver.FindElement(By.PartialLinkText("Brush")).Click();
            var description = driver.FindElement(By.CssSelector("#product-28 > div.row > div.col-md-7 > div > h1")).Text;
            Assert.AreEqual("Makeup Brush", description);
        }
    }
}

Final Result

I am using C# and Visual Studio 2013 Express Edition. Code given in video is different than the code given in this blog.

Please change quality of video to HD so you can see the code clearly.

Hopefully this video is helpful for the beginners who want to learn or use WebDriver with .NET framework
Please don’t forget to like/comment on video if you have any suggestion or you like this.

Sample Website: http://learnseleniumtesting.com/demo/