Selenium Tutorial Selenium is one of the automation testing tools which is an open source tool. Selenium automates web application. In this Free Selenium training tutorial, we cover all Selenium concepts (basic selenium scripts to advanced testing framework) in detail with easy to understand practical examples. This Selenium Tutorial is helpful for beginners to advanced level users who want to learn selenium or learn automation. Why This Selenium WebDriver Tutorial?
In this Selenium Web Driver Tutorial, you will learn how to work with the selenium to create automated tests scripts (selenium scripts) for any Web Browser applications and how to create your own framework. What are the prerequisites?. Basic knowledge of Programming Language (Java basics and Object Oriented Programming OOPs) Learn – Who are the targeted audience of this Selenium Tutorial?
Selenium Training. 2.1 Introduction. 2.2 Different flavors of Selenium. 2.3 Selenium IDE. 2.4 Locators and Object Identification. 2.5 Java For WebDriver. 2.5.1 Introducing the JAVA technology. 2.5.2 Packages. 2.5.3 Object-Oriented Programming Concepts. 2.5.4 String Processing. 2.6 Junit FrameWork. 2.7 TestNG FrameWork. Selenium Training Course Contents. Demo of various testing types with selenium. Selenium Ide; Selenium webdriver; Selenium Rc; Selenium Grid.
Software Testers, QA Engineers Selenium Tutorial – Table of Content: SELENIUM BASICS. Hi Bharat, Synchronization is to sync between an application under test and test automation tool. For Synchronization, we use three types of waits in Selenium.
I have placed the same link for both the topics is just because if someone has knowledge on QTP they could easily identify the waits concept in Selenium using the term Synchronization. Anyway, I will elaborate this post by giving more examples about synchronization in coming days. Keep visiting.
If you have any queries, please feel free to comment. Sambasivan says.
Introducing WebDriver The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems. How Does WebDriver ‘Drive’ the Browser Compared to Selenium-RC? Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.
How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter. For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser.
WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation. Java The easiest way to set up a Selenium 2.0 Java project is to use Maven. Maven will download the java bindings (the Selenium 2.0 java client library) and all its dependencies, and will create the project for you, using a maven pom.xml (project configuration) file. Once you’ve done this, you can import the maven project into your preferred IDE, IntelliJ IDEA or Eclipse. First, create a folder to contain your Selenium project files.
Then, to use Maven, you need a pom.xml file. This can be created with a text editor. We won’t teach the details of pom.xml files or for using Maven since there are already excellent references on this. Your pom.xml file will look something like this. Create this file in the folder you created for your project. 4.0.0 MySel20Proj MySel20Proj 1.0 org.seleniumhq.selenium selenium-server 3.0.1 Be sure you specify the most current version.
At the time of writing, the version listed above was the most current, however there were frequent releases immediately after the release of Selenium 2.0. Check the for the current release and edit the above dependency accordingly. Now, from a command-line, CD into the project directory and run maven as follows.
C# As of Selenium 2.2.0, the C# bindings are distributed as a set of signed dlls along with other dependency dlls. Prior to 2.2.0, all Selenium dll’s were unsigned.
To include Selenium in your project, simply download the latest selenium-dotnet zip file from. If you are using Windows Vista or above, you should unblock the zip file before unzipping it: Right click on the zip file, click “Properties”, click “Unblock” and click “OK”. Unzip the contents of the zip file, and add a reference to each of the unzipped dlls to your project in Visual Studio (or your IDE of choice). Official NuGet Packages. Introducing the Selenium-WebDriver API by Example WebDriver is a tool for automating web application testing, and in particular to verify that they work as expected.
It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium-RC (1.0) API, which will help to make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing project or from a plain old “main” method. This section introduces WebDriver’s API and helps get you started becoming familiar with it. Start by setting up a WebDriver project if you haven’t already. This was described in the previous section,. Once your project is set up, you can see that WebDriver acts just as any normal library: it is entirely self-contained, and you usually don’t need to remember to start any additional processes or run any installers before using it, as opposed to the proxy server with Selenium-RC.
Note: additional steps are required to use, and You’re now ready to write some code. An easy way to get started is this example, which searches for the term “Cheese” on Google and then outputs the result page’s title to the console. From selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expectedconditions as EC # available since 2.26.0 # Create a new instance of the Firefox driver driver = webdriver. Firefox # go to the google home page driver. Get ( ') # the page is ajaxy so the title is originally this: print driver. Title # find the element that's name attribute is q (the google search box) inputElement = driver. Findelementbyname ( 'q' ) # type in the search inputElement.
Sendkeys ( 'cheese!' ) # submit the form (although google automatically searches now without submitting) inputElement. Submit try: # we have to wait for the page to refresh, the last thing that seems to be updated is the title WebDriverWait ( driver, 10 ).
Titlecontains ( 'cheese!' )) # You should see 'cheese! - Google Search' print driver.
Title finally: driver. Use Selenium::Remote::Driver; # Create a new instance of the driver my $driver = Selenium::Remote::Driver - new; # Go to the google homepage $driver - get ( '); # Print original page title my $title = $driver - gettitle ; print '$title n'; # Find the element that's name attribute is q (google search box) my $inputElement = $driver - findelement ( 'q', 'name' ); # type in the search $inputElement - sendkeys ( 'cheese!' ); # submit the form (although google automatically searches now without submitting) $inputElement - submit ; # Set the timeout for searching for elements to 10 seconds (0 by default) $driver - setimplicitwaittimeout ( 10000 ); # then use XPath to search for a page title containing cheese! $driver - findelement ( '/html/head/titlecontains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'cheese!' )' ); # You should see 'cheese! - Google Search' $title = $driver - gettitle ; print '$title n'; $driver - quit ; In upcoming sections, you will learn more about how to use WebDriver for things such as navigating forward and backward in your browser’s history, and how to test web sites that use frames and windows. We also provide a more thorough discussions and examples.
Locating UI Elements (WebElements) Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement. Each of the language bindings exposes a “Find Element” and “Find Elements” method. The former returns a WebElement object matching the query, and throws an exception if such an element cannot be found.
The latter returns a list of WebElements, possibly empty if no DOM elements match the query. The “Find” methods take a locator or query object called “By”.
“By” strategies are listed below. By XPath At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various XPath engines. Driver Tag and Attribute Name Attribute Values Native XPath Support Lower-cased As they appear in the HTML Yes Lower-cased As they appear in the HTML No Case insensitive As they appear in the HTML Yes This is a little abstract, so for the following piece of HTML. FindElements ( By.
Xpath ( '//input' )). Then ( cheeses = console. Log ( cheeses.
Length )); The following number of matches will be found XPath expression //input 1 (“example”) 2 2 //INPUT 0 2 0 Sometimes HTML elements do not need attributes to be explicitly declared because they will default to known values. For example, the “input” tag does not require the “type” attribute because it defaults to “text”.
The rule of thumb when using xpath in WebDriver is that you should not expect to be able to match against these implicit attributes. JavaScript in the HtmlUnit Driver None of the popular browsers uses the JavaScript engine used by HtmlUnit (Rhino). If you test JavaScript using HtmlUnit the results may differ significantly from those browsers. When we say “JavaScript” we actually mean “JavaScript and the DOM”. Although the DOM is defined by the W3C each browser has its own quirks and differences in their implementation of the DOM and in how JavaScript interacts with it. HtmlUnit has an impressively complete implementation of the DOM and has good support for using JavaScript, but it is no different from any other browser: it has its own quirks and differences from both the W3C standard and the DOM implementations of the major browsers, despite its ability to mimic other browsers.
With WebDriver, we had to make a choice; do we enable HtmlUnit’s JavaScript capabilities and run the risk of teams running into problems that only manifest themselves there, or do we leave JavaScript disabled, knowing that there are more and more sites that rely on JavaScript? We took the conservative approach, and by default have disabled support when we use HtmlUnit. With each release of both WebDriver and HtmlUnit, we reassess this decision: we hope to enable JavaScript by default on the HtmlUnit at some point. Internet Explorer Driver The InternetExplorerDriver is a standalone server which implements WebDriver’s wire protocol. This driver has been tested with IE 7, 8, 9, 10, and 11 on appropriate combinations of Vista, Windows 7, Windows 8, and Windows 8.1.
As of 15 April 2014, IE 6 is no longer supported. The driver supports running 32-bit and 64-bit versions of the browser.
The choice of how to determine which “bit-ness” to use in launching the browser depends on which version of the IEDriverServer.exe is launched. If the 32-bit version of IEDriverServer.exe is launched, the 32-bit version of IE will be launched. Similarly, if the 64-bit version of IEDriverServer.exe is launched, the 64-bit version of IE will be launched. ChromeDriver ChromeDriver is maintained / supported by the project iteslf. WebDriver works with Chrome through the chromedriver binary (found on the chromium project’s download page).
You need to have both chromedriver and a version of chrome browser installed. Chromedriver needs to be placed somewhere on your system’s path in order for WebDriver to automatically discover it. The Chrome browser itself is discovered by chromedriver in the default installation path. These both can be overridden by environment variables.
Please refer to for more information. WebDriver-Backed Selenium-RC The Java version of WebDriver provides an implementation of the Selenium-RC API.
These means that you can use the underlying WebDriver technology using the Selenium-RC API. This is primarily provided for backwards compatibility. It allows those who have existing test suites using the Selenium-RC API to use WebDriver under the covers. It’s provided to help ease the migration path to Selenium-WebDriver. Also, this allows one to use both APIs, side-by-side, in the same test code. Selenium-WebDriver is used like this. // You may use any WebDriver implementation.
Firefox is used here as an example WebDriver driver = new FirefoxDriver ; // A 'base url', used by selenium to resolve relative URLs String baseUrl = '; // Create the Selenium implementation Selenium selenium = new WebDriverBackedSelenium ( driver, baseUrl ); // Perform actions with selenium selenium. Open ( '); selenium. Type ( 'name=q', 'cheese' ); selenium.
Click ( 'name=btnG' ); // Get the underlying WebDriver implementation back. This will refer to the // same WebDriver instance as the 'driver' variable above. WebDriver driverInstance = (( WebDriverBackedSelenium ) selenium ). GetWrappedDriver ; //Finally, close the browser. Call stop on the WebDriverBackedSelenium instance //instead of calling driver.quit. Otherwise, the JVM will continue running after //the browser has been closed. DesiredCapabilities capabilities = new DesiredCapabilities ; capabilities.
SetBrowserName ( 'safari' ); CommandExecutor executor = new SeleneseCommandExecutor ( new URL ( '), new URL ( '), capabilities ); WebDriver driver = new RemoteWebDriver ( executor, capabilities ); There are currently some major limitations with this approach, notably that findElements doesn’t work as expected. Also, because we’re using Selenium Core for the heavy lifting of driving the browser, you are limited by the JavaScript sandbox. Next Steps This chapter has simply been a high level walkthrough of WebDriver and some of its key capabilities. Once getting familiar with the Selenium-WebDriver API you will then want to learn how to build test suites for maintainability, extensibility, and reduced fragility when features of the AUT frequently change. The approach most Selenium experts are now recommending is to design your test code using the Page Object Design Pattern along with possibly a Page Factory. Selenium-WebDriver provides support for this by supplying a PageFactory class in Java and C#.
This is presented, along with other advanced topics, in the. Also, for high-level description of this technique, you may want to look at the. Both of these chapters present techniques for writing more maintainable tests by making your test code more modular.