Friday 2 September 2016

Opening new tab using selenium webdriver with C# [Chrome]

Ooops! After a lot of googling came up with this fare solution. Made my day!! 


Previously, I was trying this code:
//IWebElement body = webDriver.FindElement(By.TagName("body"));
//body.SendKeys(Keys.Control + 't');

But, it was neutral for me. 


IWebDriver webDriver = new ChromeDriver();

webDriver.Manage().Window.Maximize();
webDriver.Navigate().GoToUrl(“www.google.co.in”);
              
//IWebElement body = webDriver.FindElement(By.TagName("body"));
//body.SendKeys(Keys.Control + 't');

IJavaScriptExecutor js = webDriver as IJavaScriptExecutor;
js.ExecuteScript("window.open();");           

webDriver.SwitchTo().Window(webDriver.WindowHandles[1]);//Making 2nd Tab Active.
webDriver.Navigate().GoToUrl("https://yahoo.com/");

webDriver.Close();//Closing Active Tab (2nd Tab)
           
webDriver.SwitchTo().Window(webDriver.WindowHandles[0]);// Making Tab Active.




 Hope this will help you!

Cheers!





Wednesday 10 August 2016

C# compiler


Hello there!

This article will help us to develop code which could basically compile our code. This could help us in creating web/ windows based  C# compiler even TTD as well. Enjoy the code.


using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;


namespace WebSharpCompilerBusiness
{
    public class WebSharpCompiler
    {
        public List<string> Compile(string programText)
        {
            List<string> messages = new List<string>();
            if (String.IsNullOrEmpty(programText))
            {
                messages.Add("program text cannot be null or empty");
            }
            CompilerResults compilerResults = ProcessCompilation(programText);
            foreach (CompilerError error in compilerResults.Errors)
            {
                messages.Add(String.Format("Line {0} Error No:{1} - {2}", error.Line, error.ErrorNumber, error.ErrorText));
            }

            return messages;
        }

        public CompilerResults ProcessCompilation(string programText)
        {
            CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            return codeDomProvider.CompileAssemblyFromSource(parameters, programText);
        }
    }
}

Now, for demo purpose I am simply demonstrating an UnitTest over here.

        [TestMethod]
        public void TestCompilerSingleError()
        {
            WebSharpCompiler compiler = new WebSharpCompiler();
            string programText = @"
          using System;
          namespace HelloWorld
          {
              class HelloWorldClass
              {
                  static void Main(string[] args)
                  {
                   
                      //  byte c = 1/0;
                      Console.ReadLine();
                  }
              }
          }";
            List<string> compilerErrors = compiler.Compile(programText);
            Assert.AreEqual(compilerErrors.Count, 0);
        }


That's all!

Cheers!

Wednesday 3 August 2016

How to find out connection string issue


Data Link Properties Dialog Box

The Data Link Properties dialog box is the standard Windows system interface for configuring connection strings to data sources. This dialog box exposes all of the properties that the selected OLE DB provider supports. A link to the resulting data connection is displayed under the Data Connections node in the Server Explorer.
To access this dialog box in Visual Studio .NET, select Connect to Database from the Tools menu or click the Connect to Database icon in Server Explorer. You also can open this dialog box by double-clicking a universal data link (.udl) file in Windows Explorer, and in a variety of other ways, including programmatically.
To use it directly:
Just need to save a blank notepad with ".UDL" extension and double click to open it. There is a Connections table where you can set credentials and test your DB and save connection string as well.
To view connection open this UDL file in notepad.

Tuesday 29 March 2016

Load Base64 Images using jQuery and MVC

Configure RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

             routes.MapRoute(
             name: "Default",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Image", action = "Index", id = UrlParameter.Optional }
              );
    }

}

Add Controller: 

using System;
using System.IO;
using System.Web.Mvc;


namespace Base64Img.Controllers
{
public class ImageController : Controller
{
// GET: Image
public ActionResult Index()
{
return View();
}

[HttpGet]
public ActionResult GetBase64Image()
{
string path = "D:\\IMG_152236552973901.jpeg";

FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] data = new byte[(int)fileStream.Length];
fileStream.Read(data, 0, data.Length);

return Json(new { base64imgage = Convert.ToBase64String(data) }
, JsonRequestBehavior.AllowGet);
}
}
}


Associate a view to this Controller (ImageController)


@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Loading base64 Image</title>


<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<script language="javascript" type="text/javascript">
var imageUrl = "GetBase64Image";
var imgs = null;

$(document).ready(function () {

var displayImage = function (base64Data) {
var imag = "<img "
+ "src='" + "data:image/jpg;base64,"
+ base64Data + "'/>";

$("#dvImageHolder").html(imag)
};

$("#btLoadImage").click(function () {
if (imgs != null) {
displayImage(imgs.base64imgage);
return;
}

$.ajax({
cache: false,
type: "GET",
url: imageUrl,
contentType: 'application/json',
dataType: "json",
success: function (data) {
imgs = data;
displayImage(imgs.base64imgage);
},
error: function (xhr) {
alert("Error occurred while loading the image. "
+ xhr.responseText);
}
});
});

$("#btClearImage").click(function () {
$("#dvImageHolder").html("");
});
});
</script>
</head>
<body>
<div id="dvImageHolder"></div>
<div>
<button id="btClearImage">
Clear Image
</button>
<button id="btLoadImage">
Load Image
</button>
</div>
</body>
</html>


Cheers!!