Sunday, February 11, 2018

Selenium Browser Automation

Selenium is for automating web applications.
Selenium has the support of some of the multiple browser vendors. 

You can download the Selenium library from http://www.seleniumhq.org/download/.


Sample Code:


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


FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = "Fire Fox Directory Path";

 IWebDriver driverOne = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));

driverOne.Navigate().GoToUrl("URL");

//You can find the Elements by ID, Tag Name, Class Name etc.


 IWebElement TagElement= driverOne.FindElement(By.Id("CtrlID"));

String Value =  TagElement.Text;

Wednesday, March 8, 2017

WatiN : Automate Web Application Testing

WatiN aims to bring you an easy way to automate Web Application Testing in .Net. Watin offers automation of Internet Explorer and Firefox.

It has Open source library and is known for very easy to read syntax.

You can download the Watin library from https://sourceforge.net/projects/watin/files/WatiN%202.x/.


WatiN interacts with the Internet Explorer using its COM (Component Object Model) interface. The Firefox interaction is done using the JSSh (JavaScript Shell Server) extension for Mozilla Firefox, present in WatiN.Core.dll file.
JSSH is a plug-in that exposes a telnet server that allows automation commands to be pushed to Firefox. When WatiN is downloaded, then it contains archive which have WatiN.Core.dll file.

Following are the features of WatiN:
  • Automate all the HTML elements
  • Find elements by multiple attributes
  • Support Ajax Website Testing
  • Syntax of WatiN is more Object Oriented and intuitive for developers
  • Supports creating screenshot of WebPages
  • Easy to integrate with your favorite Unit Testing Tool
  • Can be used with any .NET language
  • Since it is open source, you can download and add new features yourself
Sample Code:

BrowserIE.cs :

using WatiN.Core;


public sealed class BrowserIE
    {
        static readonly IE _Instance = new IE();
        
        static BrowserIE()
        {
        }

        BrowserIE()
        {
        }

        public static IE Instance
        {
            get { return _Instance; }
        }

        internal static void WaitForComplete()
        {
            throw new NotImplementedException();
        }

        internal static object SelectList(WatiN.Core.Constraints.AttributeConstraint attributeConstraint)
        {
            throw new NotImplementedException();
        }
    }


Watin Code:

using WatiN.Core;

 public partial class form1 : System.Windows.Forms.Form
    {
        public form1 ()
        {
            InitializeComponent();
        }

private void btnSearch_Click(object sender, EventArgs e)
        {

 BrowserIE.Instance.GoTo("WebURL");
 Span Span_ID = Tbl.Span(Find.ById("spanID"));
                        String Text= Span_ID .Text;

       }
}

Sunday, December 13, 2015

Get Geo Location with Google Map

Sample code to show how to use the Google Map API for Geo location details.
Code to get your current location (Latitude & Longitude) and how to add the marker on the Google Map.

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (p) {
        var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
        var mapOptions = {
            center: LatLng,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var marker = new google.maps.Marker({
            position: LatLng,
            map: map,
            title: "Your location: Latitude: " + p.coords.latitude + "Longitude: " + p.coords.longitude
        });

        google.maps.event.addListener(marker, "click", function (e) {
            var infoWindow = new google.maps.InfoWindow();
            infoWindow.setContent(marker.title);
            infoWindow.open(map, marker);
        });
    });

else 
{
    alert('Geo Location feature is not supported in this browser.');
}

Sunday, November 29, 2015

OCR with Microsoft Office Document Imaging

This article will show how to integrate the Office 2007 OCR engine. This will perform the OCR on the scanned image and convert it to the text.

It's necessary that you have installed the Microsoft Office Document Imaging 12.0 Type Library. MS office setup doesn't install this component by default, being necessary to install it later. To do this:
  • Run the Office 2007 installation setup
  • Click on the button Add or Remove Features
  • Make sure that the component is installed

Add reference of Microsoft Office Document Imaging DLL in project by clicking on Add Reference from solution explorer. At the COM tab, select Microsoft Office Document Imaging 12.0 Type Library.

Code:

            MODI.Document md = new MODI.Document(); 
            md.Create("FileName.TIF"); 
            md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); 
            MODI.Image image = (MODI.Image)md.Images[0];

            MODI.Layout layout = image.Layout;
           
            //create text file with the same Image file name 
            FileStream createFile = new FileStream("fileName.txt",FileMode.CreateNew);

            //save the image text in the text file 
            StreamWriter writeFile = new StreamWriter(createFile);
            writeFile.Write(image.Layout.Text); 

            writeFile.Close(); 


if you want to read text word by word then use below,

         for (int i = 0; i < layout.Words.Count; i++)
                    {
                        MODI.Word word = (MODI.Word)layout.Words[i];
                        string strText = word.Text;

                    }

Friday, July 3, 2015

Launch Application exe with Javascript.

Sample code to launch the application exe from the Javascript.

Code:

     var ws = new ActiveXObject("WScript.Shell");

     ws.Exec("C:\\TestApp.exe");

Read & Write a File with JavaScript

Sample code to do the File manipulation in JavaScript.
This code illustrate, how to Create, Read & Write the text in file using the Java Script.

Code:

           // Reading text file.

           var fso  = new ActiveXObject("Scripting.FileSystemObject");
           var fhRead = fso.OpenTextFile("C:\\Test.txt",1);
           var Fltext = fhRead.ReadAll();
           fhRead.Close();
 

            // Creating text file.

            var fh = fso.CreateTextFile("C:\\Test.txt", true);
            fh.WriteLine("Sample");
            fh.Close();
         
                 
        

Monday, June 29, 2015

Dynamically Compile C# Code

Sample code to dynamically compile the C# source code and generate the Exe/DLL. You can write the code at runtime and execute or make a DLL.

Namespace :

using System.CodeDom.Compiler;




Code:

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
            string Output = "Out.exe";

         
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            //Make sure we generate an EXE, not a DLL
            //parameters.GenerateExecutable = true; // set to false to generate DLL.
            parameters.OutputAssembly = Output;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, txtSource.Text);

            if (results.Errors.Count > 0)
            {
                txtStatus.ForeColor = Color.Red;
                foreach (CompilerError CompErr in results.Errors)
                {
                    txtStatus.Text = txtStatus.Text +
                                "Line number " + CompErr.Line +
                                ", Error Number: " + CompErr.ErrorNumber +
                                ", '" + CompErr.ErrorText + ";" +
                                Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                //Successful Compile
                txtStatus.ForeColor = Color.Blue;
                txtStatus.Text = "Success";
                //Launch our EXE
                Process.Start(Output);
            }