Selenium-webdriver documentation: PhantomJS C# Example. PhantomJS is a fully featured headless web browser with JavaScript support. Before you start you will need to download a PhantomJS driver, and make sure to put this in the beginning of your code. Using OpenQA.Selenium; using OpenQA.Selenium.PhantomJS. Selenium is a free automation testing tool for web applications. It is able to work with different browsers like Chrome, Firefox, IE, Opera and simulate human like behavior. Selenium is able to interact with all the different elements in a webpage. It can click on them, input text, extract text and much more.
Introduction
This topic aims to show the basic web driver program in selenium supported languages like C#, Groovy, Java, Perl, PHP, Python and Ruby.
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.
Selenium Webdriver Tutorial For Beginners
Navigating
and
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.
- Opening a chrome browser
- Opening google page
- 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()
.
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:
Guru99 Selenium Webdriver Tutorial
tear_down()
simply contains one line to shut down our driver object:
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.