Selenium Webdriver C Tutorial



  1. Selenium is a free open source testing tool which caters different testing needs. This tutorial cover Selenium webdriver, IDE, TesNG. Following course will make you Selenium pro.
  2. As seen in the execution snapshot; the element is found, the target page is opened, and the WebDriver session is terminated. In this tutorial, we had a detailed look at explicit.
  3. Selenium WebDriver. Selenium WebDriver is the most important component of Selenium Tool's Suite. The latest release 'Selenium 2.0' is integrated with WebDriver API which provides a simpler and more concise programming interface. The following image will give you a fair understanding of Selenium components and the Test Automation Tools.
Tutorial

WebDriver - log4net C# Examples


log4net is a logging tool for .Net. log4net is a tool to help coders output log messages to different output targets. We will look at how to use log4net with WebDriver using C#. If you are new to log4net then you are at the right place. Below are two very simple & detailed examples of log4net in order to give it a start.
Example 1 of log4net - logging output to console
In Visual Studio (I am using Visual Studio version 12) open a new project (Unit Test Project).
Install NuGet packages for Selenium WebDriver and Selenium WebDriver Support Classes.
Add a reference for log4net.
After adding all the references, the Solution Explorer looks like as seen below:
We will use below code for our example:
Output (console) after running the above code is:
Lets go through the code used above and see what exactly we have done.We are using an ILog interface to log messages. LogManager is being used to obtain logger instances that implement this interface.
LogManager.GetLogger method retrieves or creates a named logger.
Then we are using the BasicConfigurator class's 'Configure' method to initializes the log4net logging system.BasicConfigurator class allows very simple programmatic configuration of log4net. BasicConfigurator.Configure initializes the log4net logging system using a ConsoleAppender that will write to Console.Out.
From the log4net message levels (ERROR, WARN, DEBUG, INFO etc) we are using INFO level, which logs INFO level messages.
Then with the help of Firefox we are opening www.google.com and again printing using logger.info in the last line.
In the output, the log messages are formatted using the PatternLayout layout object with the DetailConversionPattern layout style.Where
r = Equivalent to timestamp. The milliseconds between the start of the application and the time the log entry was made.
t = Used to output the name of the thread that generated the logging event. Uses the thread number if no name is available.
p = Equivalent to level e.g. ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF
c = Equivalent to logger
x = Equivalent to ndc
A Nested Diagnostic Context, or NDC
m = Equivalent to message
n = Equivalent to newline
Another way to write th above pattern is:
%timestamp [%thread] %level %logger %ndc - %message%newline
If you observe the output of the above code, it shows null before text 'Here is a ...'. So in order to make sure that it does not show null in the output we can add the following line in the code.
Output (console) after running the above code is:
Here we are using NDC in the above newly added line of code. NDC, in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play.
Example 2 of log4net - logging 'success' message to console as well as log file
Here, instead of using BasicConfigurator, we are using XmlConfigurator. XmlConfigurator initializes the log4net environment using an Xml tree. In this example we are opening button.html file and asserting if the button with ID as 2 is enabled. If the button is enabled, we are logging a success message else we are throwing an exception.
XML file used with the above code is:
(In Solution Explorer, right-click on the project name, click Add and then choose New Item to add this XML file to the project.)
Button.html when opened in a browser looks as shown below. I right-clicked on button and selected Inspect Element:
Output (console) after running the above code is:
Output (log file) after running the above code is:
For accessing the log file (generated on running the code) right click on the project and select 'Open Folder in File Explorer'. Then go to folder 'bin' and then folder 'Debug'. Here your log file will be created.
Let's talk a little bit about XML file used above:
Firstly we are registering the log4net section handler. The log4net section of the configuration file needs to have a section handler registered. This is the section handler used. It simply returns the XML element that is the root of the section.
Here you have the ability to selectively enable or disable logging requests based on their logger. Log4net allows logging requests to print to multiple destinations. In log4net's language, an output destination is called an appender. Appenders must implement the log4net.Appenders.IAppender interface.
More than one appender can be attached to a logger.Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.The first appender we have used is the RollingFileAppender. RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize. MaxSizeRollBackups gets or sets the maximum number of backup files that are kept before the oldest is erased. If set to zero, then there will be no backup files and the log file will be truncated when it reaches MaxFileSize.StaticLogFileName gets or sets a value indicating whether to always log to the same file.true if always should be logged to the same file, otherwise false.
ConsoleAppender Appends logging events to the console.
The root logger resides at the top of the logger hierarchy.
You need to have one root section to house your top-level logger references. These are the loggers that inherit information from your base logger (root). The only other thing that the root section houses is the minimum level to log. Since everything inherits from the root, no appenders will log information below that specified here. This is an easy way to quickly control the logging level in your application.Only one root logger element may only be defined and it must be a child of 'log4net' element. The root logger is the root of the logger hierarchy. All loggers ultimately inherit from this logger.
As you can see that our code ran successfully and logged the success message both on the console and in the log file.
Example 2-A of log4net - logging an exception to console as well as log file
Below we have used the same code as above. The only difference is in the Id in the FindElement statement. This ID of 3 is some dummy ID that we have NOT actually used in our button.html file. We have used ID as 3 here so as to throw an exception intentionally.
Output (console) after running the above code is:
Output (log file) after running the above code is:

It is designed in such a way that users can learn and master Selenium Webdriver from scratch. If you are beginner and want to start your career as an Automation Testing Expert then you should go through this tutorial step by step.

Introduction

This topic aims to show the basic web driver program in selenium supported languages like C#, Groovy, Java, Perl, PHP, Python and Ruby.

Selenium Webdriver C TutorialWebdriver

Journey includes opening browser driver --> Google Page --> shutdown the browser

C#

The above 'program' will navigate to the Google homepage, and then close down the browser after fully loading the page.

This instantiates a new WebDriver object using the IWebdriver interface and creates a new browser window instance. In this example we are using ChromeDriver (though this could be replaced by the appropriate driver for whichever browser we wanted to use). We are wrapping this with a using statement, because IWebDriver implements IDisposable, thus not needing to explicitly type in driver.Quit();.

In case you haven't downloaded your WebDriver using NuGet, then you will need to pass an argument in the form of a path to the directory where the driver itself 'chromedriver.exe' is located.

Navigating

and

Python

Both of these lines do the same thing. They instruct the driver to navigate to a specific URL, and to wait until the page is loaded before it moves to the next statement.

There are other methods tied to navigation such as Back(), Forward() or Refresh().

After that, the using block safely quits, and disposes the object.

Java

The code below is all about 3 steps.

  1. Opening a chrome browser
  2. Opening google page
  3. Shutdown the browser

The above 'program' will navigate to the Google homepage, and then close down the browser before completing.

The first line tells the system where to find the ChromeDriver (chromedriver.exe) executable. We then create our driver object by calling the ChromeDriver() constructor, again we could be calling our constructor here for any browser/platform.

This tells our driver to navigate to the specified url: http://www.google.com. The Java WebDriver API provides the get() method directly on the WebDriver interface, though further navigation methods can be found via the navigate() method, e.g. driver.navigate.back().

Once the page has finished loading we immediately call:

This tells the driver to close all open browser windows and dispose of the driver object, as we have no other code after this call this effectively ends the program.

Is an instruction (not shown here) to the driver to close only the active window, in this instance as we only have a single window the instructions would cause identical results to calling quit().

Webdriver

Java - Best practise with page classes

Usecase : Login to FB account

Page classes : Login Page & Home Page Login page class :

Home page class:

Python

The above 'program' will navigate to the Google homepage, and then close down the browser before completing.

First we have our main function, our point of entry into the program, that calls get_google().

get_google() then starts by creating our driver instance via set_up_driver():

Whereby we state where chromedriver.exe is located, and instantiate our driver object with this path. The remainder of get_google() navigates to Google:

And then calls tear_down() passing the driver object:

tear_down() simply contains one line to shut down our driver object:

Selenium Webdriver Tutorial For Beginners

This tells the driver to close all open browser windows and dispose of the browser object, as we have no other code after this call this effectively ends the program.