Saturday, March 31, 2012

C# Class and Method Keywords

So I am new to a lot of these concepts and thought I should write down a few things about classes. The basics of a class, it is a template from which object instances can be created. It represents a noun or thing-ness from which other related things can be derived. A class has variables and methods. A variable describes the class, methods describe what a class will do. Classes are used to create instances of objects to be used in your program.

Access Modifiers
public, private, internal, protected

Classes and methods both have keyword / modifiers that define the level of access to the class or method. When a class is declared as public, access is not restricted to any member, unless the member has a more limited access modifier set on it. You can call any public class members from any other class (whether they inherit from the class or not). You cannot call a private class member in a public class from another calls. These methods are only accessible from within the class itself.

A private class prevents its members from being accessed by any other class. This essentially prevents inheritance. A method cannot be declared as public from a private class.A private method in a public class, what would that do?

Private also applies to variables and methods. The scope of the security changes with the scope of the modifier. A private method cannot be accessed by other methods outside the context of the class. Same with a private variable. Declaring these as public ensures other classes can access the method or variable.

Protected is like lazy private. Access is limited to the containing class, or subclasses that inherit from the containing class.That sound shard to understand. What it means is that a class with a protected method / member does not grant other classes access to that method, unless those classes are derived from it. If the whole class is protected no members can be public, but individual members can be private or protected.

Internal limits access of the class members to other classes in the same assembly (think dll). I usually think of this as classes in the same namespace, but I am not betting money that is scientifically accurate.

Protected internal makes no sense to me. It is limited to the current assembly OR types derived from the containing class. Like lazy public, where the only restriction is on classes outside the assembly that are not derived from the current class. Not sure when you would need that.

Static ensures that a variable or method is consistent across all  instances of an object instantiated from a specific class. The value for a static variable can be changed, but it will be changed for all instances of an object. By not declaring something static you are making it an instance method or instance variable. This changes the scope from the class to the object, meaning changing the variable or overriding the method in one object has no effect on other objects. Instance methods and instance variables are not accessible outside the scope of an object. Static methods and static variables are accessible directly from the class and do not require instantiation of an object. 

notes from MSDN
The direct base class of a class type must be at least as accessible as the class type itself.
The return type and parameter types of a method must be at least as accessible as the method itself.
The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.

What the heck concepts:
assembly, containing class, return type

REFERENCES
egghead
object composition

Tuesday, March 27, 2012

Windows 8: Switching From Developer to Consumer Preview

Brief notes. I lost all my apps with the install of Consumer Preview. It is not a migration. This includes everything from Firefox (including my few bookmarks), SQL Server 2012, and my favorite metro apps (5 in a Row and WordHunt). Oddly the Metro apps are missing from the previously unavailable app store. So, have your installers ready. I did retain my documents, downloads, and music, which is something.

I wish I had done some performance bench marking. Both OSes feel faster then Windows 7 on my old dual core.Installation felt very slow though. Reboot after configuring the settings.

Look and feel is slightly smoother than the dev preview. However, some Microsoft standard navigation is hidden from the user (Start and Show Desktop controls are present in the Taskbar, but you only see them on hoverover). The Show Desktop area doubles as the means to navigate to the menu bar (Search, Settings, Devices, Start, Share). Everything else seems the same this far.

Tuesday, March 20, 2012

O Selenium I Love Thee

//When I want to do a mouse gesture, I can build it as such      

            var builder = new OpenQA.Selenium.Interactions.Actions(Driver);
            IAction signInButton = builder.MoveToElement(Driver.FindElement(By. (SignInButton)))
                .Click(Driver.FindElement(By.CssSelector(SignInButton)))
                .Release(Driver.FindElement(By.CssSelector(SignInButton)))
                .Build();

            signInButton.Perform();

//If I cannot click on an object, even when it is enabled       
       if (Assert.IsTrue(SignInButton.Enabled))
                {SignInButton.Click();}
      else { Thread.Sleep(100);
              SignInButton.Click();}

// I should do a submit.
       if (Assert.IsTrue(SignInButton.Enabled))
                {SignInButton.Submit();}
      else { Thread.Sleep(100);
              SignInButton.Submit();}

//make driver wait by default for async controls. note that controls must have a specified id. This maybe could be changed to take an IWebElement type
        public void waitForItemById(string id)
        {
            WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
            IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
                                                                       {
                                                                           return d.FindElement(By.Id(id));
                                                                       });
        }
//nice switch to allow you to dynamically allocate the browser specific driver
        public string BrowserType = "FF";
        private string _iEDriverPath = AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\..\\..\\Libraries\\IEDriverServer_x64\\";

        public void GetDriver()
        {
            switch (BrowserType)
            {
                case "FF":
                    FirefoxProfile fp = new FirefoxProfile();
                    fp.EnableNativeEvents = false;
                    Driver = new FirefoxDriver();
                    break;
                case "IE":
                    var options = new InternetExplorerOptions();
                    options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
                    Driver = new InternetExplorerDriver(_iEDriverPath, options);
                    break;
               case "CH":
                    Driver = new ChromeDriver();
                    break;
            }
//Nice XPath to find one of several values on a page where the indistinct value you want is nested after a distinct element.
//td[text() = 'Product 1']/following-sibling::td/a[
text() = 'View Download']
- show quoted text -

Tuesday, March 13, 2012

Powershell - Unrestricted Is Not A Security Policy


It is usually an audit violation to run Powershell with an unrestricted Execution Policy. This allows you to use a profile without signing it, and it allows you to use modules without signing or unblocking them. It is also a security hole that 10 minutes can solve. -sr should be currentuser OR you should run the prompt with elevated privileges using use -sr localHost 

Signing a file
View certs: MMC - Add Snapin - Certificates
Select two passwords (or just one if you are not using this cert for anything other than local signing)
Path to the making a cert tool: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\makecert.exe

set the location to run the mkcert command from.

$>  set-location "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\"


create a public private pair of certs. Note the password somewhere. You will need it later.

$> .\makecert -n "CN=PowerShell Local Certificate Root" -a sha1 -eku 1.3.6.1.5.5.7.3.3 -r -sv C:\Users\USER\Documents\Cert\TestRootPrivateSigningCertificate.pvk C:\Users\USER\Documents\Cert\PublicCertificate.cer  -ss Root -sr CURRENTUSER

combine the certs into a PFX file
$> .\pvk2pfx -pvk C:\Users\erica\Documents\Cert\TestRootPrivateSigningCertificate.pvk -spc C:\Users\erica\Documents\Cert\PublicCertificate.cer -pfx C:\Users\USER\Documents\Cert\TestRootPrivateSigningCertificate.pfx

Import this into the local machine cert store using the Certificates snapin of the MMC

if you want to mark the private key exportable. you need the password.

$> .\makecert -pe -n "CN=PowerShell User" -ss MY -a sha1 -eku 1.3.6.1.5.5.7.3.3 -iv C:\Users\USER\Documents\Cert\TestRootPrivateSigningCertificate.pfk -ic C:\Users\USER\Documents\Cert\PublicCertificate.cer

$> mkdir c:\Users\CertStore

$> makecert c:\Users\CertStore\testCert.cer
 
Add the signing cert to the Trusted Publishers folder in MMC

> $cert = @(get-childitem cert:\CurrentUser\my -codesigning)[0]
> Set-AuthenticodeSignature  C:\Users\aldine\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 $cert

> set-ExecutionPolicy RemoteSigned

> get-ExecutionPolicy

Now you just need to right click each Powershell module and unblock it (if you do not see the option, it is unblocked)

Tuesday, March 6, 2012

I Hate Mercurial

Yes I do. But, I have to use it. So...here are some commands for simple daily work. Did not find this exactly in a single reference source.

This is what I need to do to change the folder structure
> mkdir NewSubFolder
> hg mv .\1.SomeFile.sql .\NewSubFolder
> hg commit -A  -m "Move files to new folder"

This is what I needed to review code when I was coming up to speed.
> hg init #make a place to share your code. This will store many branches
> hg clone #copy a full software repo to your machine
> hg branches #what branches do I have to choose from
>hg branch #what branch am I in?
>hg update #update the working dir (or switch revisions)
>hg pull --insecure https://host.com/PROJECT # pull from source to the current dir w/o checking the certificate.
> hg merge
this is what I need to share my changes
> hg branch WebDriverSpike-QP418 # creates a new branch. for some reason mercurial docs say to only do this for release level commit streams, but I do it for spikes and feature development
> hg commit -m WebDriverSpike-QP418 # commit changes to my branch use hg branch here to ensure you are in your Branch.

> hg push

OOPS. I hit the error:
   abort: crosses branches (merge branches or use --clean to discard changes)

When you want to switch between at two branches you use update. However this forces you to either merge or drop changes. I find that silly as branches are independent in TFS and I can easily run two instances of my IDE to compare them. In Mercurial you have to clone your whole repository manually then in one repo you would update and drop the changes.

> hg clone repoOld repoNew
> cd repoOld
> hg update -C default #drops changes prior to changing branches

The long form is
1.       Create the folder structure locally (below is a suggested folder layout) using Windows Explorer.
3.       Dump the existing software project folders under the correct functional test area (e.g. Selenium code goes under UI testing, T-SQL goes under ETL testing)
4.       Create a repository at the top level project folder (where it says <Your Top Level Software Project Folder - N levels deep>)
5.      Add the same files to the HG repository
6.       Commit exisitng code via HG.
7.       Share out your top level folder for source control via Windows Explorer (where it says Source below) with your team via a security group.
8.       Log onto the repository server.
9.       Go to C:\Source
10.   Create the same folder structure as above, up to top level software project folder
11.   Steps from cmd line in Powershell

PS C:\> cd C:\Source\Team1
PS C:\Source\Team1> cd .\ProjectA
PS C:\Source\Team1\ProjectA> hg init .
PS C:\Source\Team1\ProjectA> ls

    Directory: C:\Source\Team1\ProjectA


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         6/15/2012   4:30 PM            .hg - this confirms the repository was created
d----         6/15/2012   3:20 PM            ProjectA

PS C:\Source\Team1\ProjectA> cd .\ImpactDataArchive
PS C:\Source\Team1\ProjectA\FeatureA.1> hg clone \ machine>\Source\Team1   --this is your local development machine\<your 
updating to branch default
34 files updated, 0 files merged, 0 files removed, 0 files unresolved

You should now be able to commit and push changes to the server as a backup. You have to issue a commit AND a push for the changes to show up on the server. There is a Powershell extension to combine those steps.
I can use the same steps as 8-12 to pull or push to anyone on the team (peer to peer).