Tuesday, May 27, 2014

Ruby: Why a blood-red colored gemstone can be so attractive?


What kind of language Ruby is and what kind of problems is suitable for?


What initially attracted me to Ruby was that it's creator has a focus on helping developers to be productive and happy.
It was a refreshing attitude. As I started using the language I've found that Ruby allows me to write very clean and readable code with minimal levels of syntactic and semantic noise.
It also comes with a huge amount of useful functionality built into it's standard library.
Ruby was first released in 1995 and over the years a large ecosystem of third-party libraries and frameworks grew around it. Particularly in the web development arena.




How does ruby compare to other languages?


Ruby started to be object-oriented has it was inspired by smalltalk. There are few primitives and many things in Ruby are expressed in terms of objects and methods. Is it based on dynamic typing and duck typing.

Dynamic Typing means that a variable can refer to objects of different types during the course of program execution.
Duck Typing means that valid semantics aren't determined by the object type. Instead it's enough for the object to have the methods which the operation uses. This allows you to have polymorphism without inheritance so class hierarchy can be a lot simpler. It also give you the opportunity to write more generic code.

The flip side is that it may require more discipline from the programmers working on the code base.

In addition to object-oriented style of programming, Ruby is flexible enough to allow functional and procedural styles of programming.

Ruby has a lot of facilities for reflection. It can get a lot of information at run-time such as class methods, in-heritage hierarchies and so on...

Another aspect of Ruby is that it provides rich opportunities for meta-programming by a vary of hook methods as well as the ability to create classes and methods dynamically.

The widely used implementations of Ruby are bytecode interpreted. This can put some limitations on performance.
The official implementation is called MRI which stands for Matz's Ruby Interpreter.
Another popular implementation is JRuby which is build on top of JVM (Java Virtual Machine).
On the Mac there's another Ruby implementation called MacRuby that can perform both just in time and ahead of time compilation of Ruby code.

Sunday, May 18, 2014

Creating a simple test framework in plain English, easy to use and easy to understand

Let's start off by creating our framework and then we are gonna create a Unit Testing project and that's gonna be our tests layer.

First we start by creating a new Class Library project and call it "TestFramework".


Now that we have this project, we can go ahead and create another project inside our solution and this one will contain our tests. In this case we are gonna create a "Unit Test Project" and call it "Tests".


The idea here is to separate the Test Framework from our Tests. Remember in our Architecture Diagram we said this was really important. We don't want our tests to depend on Selenium. We want the Test Framework to depend on Selenium. And the Tests to depend on our Test Framework.

So we'll never gonna add Selenium to our Tests project. We're only gonna add to our Test Framework. And we'll make our Tests depend on our Test Framework.

So let's go ahead and add that dependency. We will go to our references in Tests and add a new reference to TestFramework.


The next thing we are gonna do is to add Selenium to our TestFramework. To do that we go to references in the TestFramework project and select the option "Manage NuGet Packages..."

We'll search for "selenium". Select the packages "Selenium WebDriver" and "Selenium WebDriver Support Classes".


So now that we have our projects set up, let's go ahead and create a very simple test.
We are just gonna open up our website "http://plan2finish.net" and log in with a valid credentials (username and password) and verify that the user logged in successfully.

So we go to our UnitTest1.cs class and create a test named Can_Login_User_With_Valid_Credentials


We are gonna write this test first and then we are gonna implement the framework to make this test succeed.
This is a little bit backwards from how you might think about writing an automation framework but I found that this is the best way to do it for two reasons:


  • It makes sure your API is simple and understandable.
  • It make sure you don't build more framework than you need.


So we can start writing the API we want to use inside of our test. Remember we can't depend on Selenium here. We should try to write it in plain English as possible and we are not gonna ever import Selenium.


We start by creating a method Pages.LoginPage.Goto that it's gonna go to LoginPage of plan2finish.net website.
After that we are gonna create another method Pages.LoginPage.LoginUser that will receive a username and password and will insert those values in the username and password fields on the Login Page and push the Login Button.
At last we are gonna Assert that the credentials are valid by checking if we are in the MainPage and the user logged in successfully using the method Pages.MainPage.HasUserLoggedIn that receives the username.

We go ahead and add a new class Pages inside the TestFramework project.



We are gonna make this public and static. You'll find that as we are working on the framework, the way that I like to build the framework is not the way that you should build traditional code. What I mean by this, is that you typically wouldn't make everything static. But you'll find that there is this balance between simplifying the API and making the framework code itself subscribed to use some of the best practices that you would use in a regular application. A framework for testing it's a different type of application than any other type of applications. So you don't want try and build it as a standard application.

So now that we have the Pages class, we need a LoginPage class. We'll make this one static as well.

After creating the LoginPage class we need to create a Goto method inside the LoginPage class. We'll leave the implementation for later. Let's make sure we have our test class flashed out.


So we need to add a LoginUser method to LoginPage class and create a MainPage class with a HasUserLoggedIn method inside. All static.


We can now run our test. It will fail because we don't have any implementation in our methods. So next, we'll add the implementation to our methods.

Let's start by implement the Goto method and imagine a general implementation of a Browser. A general purpose browser. We could create a browser for each page, but if we did that we would be mixing the concepts a little bit. So again, I'm gonna drive this time from the framework to the deeper layers of the framework.

The simplest thing to do would be something like:



Which url would be a constant in our class. Now we need the Browser object implementation.
We'll make a static class called Browser with a Goto method. And the implementation of this Goto method is going to use Selenium. So let's give to our Browser class an instance of our webdriver and we'll set it to a new FirefoxDriver. This implementation could be more flexible to use different types of browsers, but in this case to make things simple let's use firefox only.


Now that we have the Goto implemented we need to implement the LoginUser method.
We should inspect login page in plan2finish.net and think about our page object model as we've mentioned before.


We want to have the login page model as it's own object and it should mirror the functionality that exists when the user is using this page. So for example, you can see two fields here, one for Email and other for Password. And there's also a button Login that we'll need.

So we're gonna start off by modeling the parts we need. So we can insert a email and a password and press the login button.

In order to do that we're gonna use the Page Object Model that's built in Selenium. We can't have a static page class anymore. We have to create an instantiation of the page where we can inject the information from the page and bind the elements we want to use. This will make a little bit more sense in a minute. But first we need to refactor our code a little bit.

So we need to change our LoginPage class so it can be instantiated instead of making it static.
The same with the methods inside the class.

The next thing we need to do is to implement the LoginPage in Pages as a property. Using only the getter, we don't need the setter here.


Going back to our LoginUser method, in order to login a user in the login page, we need to get those fields and button. We are gonna inspect the page and look for the better way to retrieve them.



We can get the email field, the password field and the login button by the tags ids.


  • Email: id="UserName"
  • Password: id="Password"
  • Login Button: id="LoginButton"


We can use the Page Object Model to do this. We will create a set of properties to bind to those elements in the page.


Then we will annotate those properties with FindsBy, and we are gonna say "How" = How.Id, it will find the element by id and "Using" the id name "UserName" for the Email field. We'll do the same for the password field and login button.

Now that we've done this, if we use the page factory that's built into the Selenium support, we can automatically go to the page and select the element according to this criteria and populate the properties with references to those elements. And we can use those binded properties in our tests.

This simplifies things quite a bit here. But in order to make this to work, we have to do something special in our LoginPage instantiation.



What we are gonna parse our page through this page factory. And we need the driver from our browser, so let's make sure we can access our driver from our Browser object.


We are initialising our LoginPage and passing it through this factory that would look for the attributes we annotated in our properties.

So now we can implement our LoginUser method in our LoginPage object.


We'll do something similar to our Main Page, the page where the user would be redirected if the credentials are correct. In the same way we'll send the main page to the page factory to be parsed.


And in the MainPage object we just have to implement the HasUserLoggedIn method:

In the Main Page, there would be a hidden field with the user name, and we'll check if our user is logged in by checking this field. But first we need to create a property to bind this hidden field to our class.


And that's it, we are ready to run our test now.



So let's take a minute now to think what we've done so far and where we might go from here. If we look at our unit test, we can see that we've made them in plain English, very easy to use. Notice we are not instantiating any objects here. It's all very simple. That's why we are using static methods so much. Even dough static methods might not be something that you would use in a traditional application.
We've basically driven the framework from the test. If you read this test, you should be able to understand exactly what's going on. It should be clear as possible.
The framework might be more difficult to build because we had to figure out ways to make the framework obey this API. Because the API came first.

Now if we look at the framework. You can see that we've divided things up by the different pages. This is the Page Object Model concept. We have a LoginPage and a MainPage.
Doing this really makes it easy to understand how your tests should be written. Because it's very intuitive. You can figure out that if there's a page in the system, there's probably gonna be a class in the framework that you can use. This is really important because in many instances the developers working in the Test Framework are not the same people that are writing the tests. You may have your QA team or someone that's not skilled in development skills writing tests. And if it's very simple and straight forward they can easily maintain those tests and it makes it a lot easier for them to figure out what they need to do or what kind of functionality they need in the framework. They can basically write their tests and then a developer that's working the Test Framework can implement the functionality that they want their test to be able to do.

That's the way I recommend working and that's why we should make our tests so simple.

Friday, May 16, 2014

Shaping the perfect Page Object Model for your Automation Framework

Let's talk about the implementation details that we're going to be use when we actually go ahead and create our Automation Framework.

Within your application UI there are areas that your tests interact with. A Page Object Model simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place.

You may have heard the term Page Object ModelThis is a common term in the automation community which basically refers to idea of mapping your Automation Framework model to the model of your application pages.
So your application you may have a series of different pages and those pages are going to be modeled in our framework the same way.



So as an example, if you have a login page, we are going to create a class inside of our test framework called LoginPage and it's gonna have some of the same functionality that your login page has. It's gonna be responsible to get the elements that exist on that login page. 

For example, the username field, the password field and the login button. It will manipulate those elements at the highest level possible. That's what makes a good Automation Framework.

Instead of having a method in the LoginPage class called getUsernameField or insertTextInUsernameField. We are gonna have a Login method on the LoginPage class and we'll pass the username, the password and automatically it's going to know how to insert the username and password into the correct fields and push the login button.

This is going to aid in our automation. It's where that coffee maker example comes into play. Part of this idea of the page object model is to design this high level API. You are thinking about designing your framework for your test in the same way that the user might use the real page.

You can see also that the objects of your Page Object Model don't have to necessarily map to exact pages in your application. In this diagram we have a Navigation Menu example. And maybe you have this Navigation Menu in every single page. It's a section of you application. So it's gonna map to it's own logical object in your framework and you'll have methods that your tests can call and use it just like a real user would.

And by doing it this, you make it very simple to write your tests and understand your tests.


Divide et impera - How to set up your Automation Framework

Let's look at a basic architecture example that you can use to create your Automation Framework.
If you look at this diagram here, looks fairly simple. But there're some very key things going on here.




The biggest thing that's happening is that your Test Scripts are depending on an Automation Framework and they're not depending directly on Selenium.

We are gonna have our Tests Scripts only depending on our Automation Framework that we are gonna create. And then the Automation Framework is gonna depend on Selenium.

Technically we could swap out the Selenium layer and replace it with a different type of Automation Tool besides Selenium. And just modify the connecting pieces from our Automation Framework and our Tests Scripts should still be able to run.



The goal of our Automation Framework is to support our Tests Scripts. You can also think about what concepts are related to this different layers here:

So, the Tests Scripts are gonna be focused on things like, pages in your applications, workflows. Things that are very domain specific to your application. It's actually going to share the same domain knowledge with your application.

At the Automation Framework layer we are gonna be focused on smaller pieces like the DOM (Document Object Model), things that exist in the browser. This is where we're going to be getting elements from the pages and we're going to be focused on how to manipulate pages and how to best navigate through the application.

Than at the Selenium layer we are at a very low level. It's focused on the actual HTML, the elements. It's parsing the pages. It's using the Browser API. It's at the very lowest level.

The fundamental truth about the framework architecture of automated testing

There's a lot of different ways you can build frameworks for automated testing.
The way that I'm going to suggest is a way that I've found by trying and prove it.
It's a way you can always evolve over time and a lot of people are using similar ways to this one.
This is a fundamental truth about the framework architecture of automated testing.
I highly recommend that you at least follow very closely to this approach .

Let's think about an example of Making Coffee, so you can understand what level of the API we are shooting for our automated testing framework.

This is a very important concept! A lot of people miss this and end up writing fragile tests because of not using a framework.

Everybody knows how to make coffee.
There's a couple of different ways you can make coffee.

The Low Level Approach




1. You get the coffee beans
2. You grind your beans in a grinder
3. You heat up some water
4. You put a filter inside of your cup with the coffee beans you ground before
5. You put some hot water into that filter and then what comes out is coffee

Quite a few steps here, because it's a very low level

The High Level Approach




1. You get a capsule of coffee
2. You insert the capsule into your coffee machine
3. You press a button and what comes out of your machine is coffee

Still a lot of steps here, but definitely fewer steps than the low level approach.

This is actually the level we want to write tests at. And the reason why we want to do this it's because:

  • We want them to be very simple to write.
  • We want them to be very maintainable 
  • We want the framework to handle all the low level details
Because you don't want your tests to be complicated. You want them to be as simple as possible so they are easy to maintain, easy to understand and they don't require programmers to build them.

This is one of the key things that will determine the success of you automation framework.

Thursday, May 15, 2014

Why do we need an Automation Framework?

Why should we create an automation framework?
Why all this work?
Why we just don't record our tests with Selenium IDE? This seems like an easy way to go!
Why do we have to write all this code?

There's a few reasons why recording doesn't work, but the main reason is:


Test Fragility


The major reason is Test Fragility. If we look at this simple diagram we can see that if you decided that you are just gonna record your tests and you won't gonna use some kind of a framework. Than your tests will directly depend on your application.



So what happen now, if you application changes in some way? Let say that the structure of the application changes significantly. Or even just minor little changes.

If you have a large number of tests and all those tests depend on a particular element on a page being named a certain way and that changes. All of those tests are suddenly going to break. And you will have to modify all that code in all those tests. That's really the big disadvantage of recording. Because recording is going to grab the elements by the most efficient way that it can find to grab the element instead of doing it in an abstracted way of grabbing that element. So if the element changes, as those tests break. You are copying a lot of code, when you are recording.


How can we reduce this fragility?


Now let's think about what happens, if we put an Automation Framework in place. The only modification I've made to this diagram was adding an abstraction layer. And that's what the Automation Framework is doing for you. It's primarily giving you an abstraction from the actually application.


The idea here, is that your test now depends on the Automation Framework. You are using the Automation Framework as an internal language for writing your tests. So since your tests depend on that Automation Framework. We run away from that scenario, if something changes in your application now, then you have just to update the method in your Automation Framework that manipulates that object in your application.

So let's imagine an id change, or a name of some field that you are using on an element change. Well hopefully, you've only just created one reference to it in your Automation Framework. You've abstracted the idea of how to grab that element or use that element. And so all those tests are dependent on the framework and so you've only have to change in one place. Because there's not that duplication of the dependencies on your application.

So that's the major reason why we don't want to record.



Testing your web application in parallel with multiple machines againstdifferent browsers using Selenium Grid (Part 2)

So this time we are gonna start up selenium server, but we are gonna start it up as a hub.
On my main computer (PC1), I' just gonna do the same command we done before "java -jar selenium-server-standalone-2.41.0.jar" and this time we are gonna use " -role hub" and this is gonna put this instance in hub mode.


Now there's a couple of other things we can do here. One is setting up a port, doing " -port <port_number>", that will change the port number. 
By default that's gonna be 4444. In case you have that in use you can change it. 
And you can also specify a configuration file, a JSON file in, as well a couple of other configuration options.

In most cases, this standard configuration works. So we just gonna keep things simple for now and use only "- role hub".


You can see it's lunching selenium grid server. You can tell that you are in grid mode, because it says "grid server" in this case. And you can see it's connected on port 4444.

Now once we have this up, we want to test to make sure this is actually running. So what we are gonna is open a browser and type this address "http://localhost:4444/grid/console".


And you can see it shows a page with "Grid Console", the version number and we can click "view config" to view the configuration here.

And this is the configuration for the hub, and we you'll see the details of the configuration. Some of those settings are pretty basic, like the port number, timeout. All this settings can be modified, but for the most cases the basic configuration is usually gonna work.

Now when we'll have nodes connected, we are gonna return to this console and we'll be able to see those nodes and manage those nodes. 

So, let's go ahead and create our first node.

Setting up the first node (PC1 - Windows 8)


We just have to open another command prompt and type "java -jar selenium-server-standalone-2.41.0.jar" but this time we are gonna do " -role node" and then we are gonna do " - hub" and specify where our hub is " http://localhost:4444/grid/register". 



And again here, with the node we can specify some configuration, we can set a different port. 
The port by default it's gonna be "5555".

So let's go ahead and run this:


And you can see it's lunching selenium a selenium grid node, and it's actually configured from a JSON object, and will actually send that object to the server. You can see the configuration here. It's registering that node on the grid and it's constantly hitting the grid hub in order to get the status to figure out if it needs to run any test.

So if you look at the console now in "http://localhost:4444/grid/console".


You can see that we've got one Remote Proxy here, and we can see what ip address is listening on. And you can see it supports up to 5 concurrent tests and if you hover over one of the small icons you can see what the configuration is.


In this case the protocol is Selenium, platform is Windows 8, browser name is Firefox and then the max instances is 5.

And you can see we can call Google Chrome on here, 5 instances running at the same time. We can have Internet Explorer with 1 instance. You can also notice we have Remote Control (legacy), this is the old selenium (Selenium 1). So it allows you connect using Web Driver or through the old style selenium RC.

So that's our first node. Now we need to setup our second node.

Setting up the second node (PC2 - Ubunto Linux Virtual Machine)


In this second node, we'll be running a instance of Ubunto (Linux) operating system. To do that, we'll be using Virtual Box. The purpose of this post it's to guide how to setup a distributed testing environment using selenium grid. So we'll skip Ubunto setup and configuration of the virtual machine.

Once the virtual machine is setup with Ubunto and JRE, we go ahead and run a new node on it by running the same exactly process we did for creating the first node. By typing in a terminal windows "java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://172.16.0.45:4444/grid/server".



Notice that the ip address changed because we are not using localhost anymore. Instead we are pointing our new node to the first machine (PC1) which is ip address is 172.16.0.45.



After launching we can see the second node is running in grid mode, same as node 1 in PC1.
And now we can go ahead and refresh our console in PC1 and we can notice we have a second Remote Proxy here. You can see what the address of that proxy is, and then it's on port 5555 running Linux OS.


Now, one thing to notice here also is that you need to make sure that your firewall settings are allowing selenium server to connect through the port here. 
So, for example, our hub (PC1) is on port 4444, on the PC2 I had to make sure I had allowed firewall out on PC2 and then in on the machine PC1 allowing connections coming from port 4444.
And if you don't do this, then you'll see it won't connect.

So next, I'm gonna set up an additional node on my Mac.

Setting up the third node (Mac - OS X 10.9.2 Mavericks)


After running the third node on my Mac, and I did exactly the same thing I did on PC2.

I just ran "java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://172.16.0.45:4444/grid/server".
So let's go ahead and refresh this console again in PC1, and now you can see there's a third node there.


So now we are able to run remote test on a Mac as well.
We have just setup a basic configuration.

Next we'll see how to run some tests, just to verify their configuration works.

If you want to do a more advanced configuration, you can go to http://code.google.com/p/selenium/wiki/Grid2


This is basically the Google code page for selenium and inside the wiki there on grid, you can find here some more information. And you can find all the information we have covered, about how to start up selenium server as a node. But also what you'll find here, how to configure nodes by command lines and they give some instructions here how to do that. It tells you what the optional parameters are. And the basic things you might wanna change here are the things like the maxSession parameter, so how many tests can be running simultaneously and the browser type in parameters to maybe test different versions of a browser. You have actually the ability to chose the version number of the browser where you want to test. You just have to specify where that browser is. If you use one other than the current version.

-browser browserName=firefox,version=3.6,firefox_binary=/home/myhomedir/firefox36/firefox,maxInstances=3,platform=LINUX

Another thing you can see how to do here, is to configure your nodes by JSON. And you can do that on the node and hub. You can have some samples of configurations that might be useful.
It's a pretty good way to do the configuration.

So now let's run our first test in our grid to make sure everything is working.
Actually we don't need to make any changes in our test class, since the hub where the nodes are connected are in my local machine. 

var driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Firefox());

Remember you can you different browsers, but you do have to set them up and configure them in which one of the nodes. So you'll have to setup Chrome or IE or whatever you want to use in that hub configuration.

If we look at our grid, we can see we've got three nodes here, and even dough it shows all of this browsers they are not necessarily all available. 



For example, on my Mac, I don't have Internet Explorer. But this is just the default. Remember you can change that configuration if you want to, in order to specify what browsers, how many are allowed and where the drivers are located.

If we'll run the tests it as it is, it would pick up a node that can run our tests according to the Desired Capabilities we selected. In this case Firefox. It picked up my local machine PC1 for that.

That was successful, but now let's do something to make sure that we get the Mac version.
What we can do here, is to change the Desirable Capabilities. 
So instead of just doing DesiredCapabilities.Firefox() and replace it to new DesiredCapabilities("firefox", "", new Platform(PlatformType.Mac)).

We will not specify the version of Firefox we want leaving the second argument as a empty string. We'll have:

var driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), new DesiredCapabilities("firefox", "", new Platform(PlatformType.Mac)));


So we can run this and watch the Mac executing the tests using Firefox.


Final Considerations


I would like to cover a one more point about selenium grid mode. 
That point is Parallel Execution. We didn't look at doing Parallel Execution, and that's it because it's really not part of Selenium or the grid framework itself. 
It's a little bit confusing to sum when you're expecting that if you use grid, that your tests will just automatically be executed in parallel. And that's not necessarily the case.

You have to do something to make that happen. So, for example, let's say that we wanted to run 10 test cases at a time, and maybe you have a 1000 test cases or so. You would need to, in the way that you run your tests, set up parallel execution. In your test project, perhaps you would spin off threads running your tests and they will all hitting the same hub. And that hub can execute them in parallel. 
But doing that, we are actually controlling the parallelization of our tests at the level where we are running them or where the driver of the test is. 
But basically you are responsible for parallel executing. The grid makes it possible, but you have to initiate it.

Wednesday, May 14, 2014

Testing your web application in parallel with multiple machines against different browsers using Selenium Grid

Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.

Architecture:







Setting up Selenium Grid:



Selenium Server is actually a java program, because it was originally build for java in java. So we will need to have JRE (Java Runtime Environment) installed.

If you don't have that installed already, you can go to http://www.java.com/getjava, and you can download java.

Once you have that installed, we can go to http://seleniumhq.org/download





Here we are gonna grab the latest version of selenium server.

You can see it's a jar file. You can copy it to a location where you can use it and run it.
In this example, I will copy it to "C:\Selenium\Server".




Before you can run this, you do need to have java in your path environment variables.

You may already done this, but  if you haven't you can go into your Control Panel under System, then you want to go to Advanced System Settings.

Under here, you wanna go to Environment Variables... and set up your path here by editing the PATH Variable in System variables and adding to Variable value the path to you java bin folder. In my case "C:\Program Files\Java\jre8\bin".





This maybe be a different install location for you depending on what version of Java you've installed. But basically you just have to get to this bin directory.


Once you have done that, go ahead to the directory where you dropped selenium server JAR file using a command prompt window.




On this command prompt, type "java" to make sure you can access java.

Then type "java -jar selenium-server-standalone-2.41.0.jar"

You can also do " -port <port_number>" to specify the port number you want.


And you can see it's launching selenium server as a standalone server.




What it's doing here. It's creating a simple web server that's gonna listen on a port.

You can see that it says "RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub".

And so now, if we point a Remote Web Driver in our test at this address it will communicate with this server in order to execute the test.


This is all we need to do to get server running, and we need to leave this open because server is running in this process.


So now, while writing the different test scenarios, instead of using your local drivers:


var driver = new ChromeDriver(@"C:\Drivers")


We are gonna change this and  use what is called a "Remote Web Driver":


var driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Firefox());


Here we are gonna specify two things:



  1. The address of our remote selenium server (new Uri("http://localhost:4444/wd/hub")
  2. The capabilities we want (DesiredCapabilities.Firefox())

We can put here some specific desired capabilities. We can say we want to run Chrome Web Browser and specify the version number and the operating system. And if the server was able to fulfil that, it would try to run what we are requesting.

In this case we are choosing a basic one (DesiredCapabilities.Firefox()), and this is just going to tell the remote selenium server to run our test in Firefox.

We can use other browsers besides Firefox when we run selenium server.
Remember that we ran the command "java -jar selenium-server-standalone-2.41.0.jar", we also have to specify an option that would say where that driver is.

For example for Chrome, we would have to specify where that chrome driver is, just like we had to do here "var driver = new ChromeDriver(@"C:\Drivers")", when we tried to use ChromeDriver locally. Because Chrome Driver doesn't come built in Selenium.

You can run your tests now, and you can see in selenium server command window that it's executing a new session for selenium server, and that Firefox loaded up and it ran your tests.

So that's really all you have to do to get a hub configured.

The next step is to setting up the grid.


We can also setup selenium server in "The Grid Mode". And it's gonna be very similar to what we have done so far. You can see the diagram, that's what we are gonna do here.

I'm gonna set up a Hub and a Node running on my main PC with Windows. 
On virtual machine, I'm gonna run my second PC with Linux and set up another Node.
And then I'm gonna run a third Node on a Mac.

And so on, what we see here is that each one of this nodes it's just gonna be a separate instance of selenium server and we are just gonna specify what role it's playing. 

So for the Hub, we will just specify it's in the "Hub Mode". And then for the Nodes we are gonna specify that they're in the "Node Mode" and then we are gonna tell them where to connect to the Hub.

And by doing that, we'll have all this nodes connected to the hub and we can actually see that the nodes are connected to the hub.

The purpose of doing this is two-fold:
  • It allows you to distribute your tests so that you can run them in parallel. All through the same coordinator which is the Hub.
  • Allows you to run in different platforms and different configurations and have the hub be smart enough to figure out what node it's connected and which node to use based on what kind of requirement you have for running your test.