The tutorial is a Complete Guide to How to Write your First Webdriver script. Examples of Selenium Webdriver Scripts in our JAVA Program. First Selenium Webdriver Script: JAVA Code Example. It is recommended that you locate GUI elements using IDE and once successfully identified export the code to webdriver. Here is a sample code that.
In the previous two tutorials, we made you acquainted with the basic architecture and features of WebDriver and the infrastructure required to get started with Selenium WebDriver. Assuming that you all might have set up the system with all the necessary utilities and packages, we will move further with the implementation of our first WebDriver test script.
Therefore, motioning ahead with the consequent Selenium WebDriver tutorial, we would be creating WebDriver script. We would also scatter the light on the basic and commonly used WebDriver commands. We would also learn about the locating strategies of UI elements and their inclusion in the test scripts. We would also study Get Commands in the detail.
What You Will Learn:
Script Creation
For script creation, we would be using “Learning_Selenium” project created in the previous tutorial and “gmail.com” as the application under test (AUT).
Scenario:
Step 1: Create a new java class named as “Gmail_Login” under the “Learning_Selenium” project.
Step 2: Copy and paste the below code in the “Gmail_Login.java” class.
The above code is equivalent to the textual scenario presented earlier.
Code Walkthrough
Import Statements:
Prior to the actual scripting, we need to import the above packages:
import org.openqa.selenium.WebDriver – References the WebDriver interface which is required to instantiate a new web browser.
import org.openqa.selenium.firefox.FirefoxDriver – References the FirefoxDriver class that is required instantiate a Firefox specific driver on the browser instance instantiated using WebDriver interface.
import org.openqa.selenium.WebElement – References to the WebElement class which is required to instantiate a new web element.
import org.openqa.selenium.By – References to the By class on which a locator type is called.
As and when our project would grow, it is evident and logical that we might have to introduce several other packages for more complex and distinct functionalities like excel manipulations, database connectivity, logging, assertions etc.
Object Instantiation
WebDriver driver = new FirefoxDriver();
We create a reference variable for WebDriver interface and instantiate it using FirefoxDriver class. A default Firefox profile will be launched which means that no extensions and plugins would be loaded with the Firefox instance and that it runs in the safe mode.
Launching the Web browser
driver.get(appUrl);
A get() method is called on the WebDriver instance to launch a fresh web browser instance. The string character sequence passed as a parameter into the get() method redirects the launched web browser instance to the application URL.
Maximize Browser Window
driver.manage().window().maximize();
The maximize() method is used to maximize the browser window soon after it is re-directed to the application URL.
Fetch the page Title
driver.getTitle();
The getTitle() method is used to fetch the title of the current web page. Thus, the fetched title can be loaded to a string variable.
Comparison between Expected and Actual Values:
The above code uses the conditional statement java constructs to compare the actual value and the expected value. Based on the result obtained, the print statement would be executed.
WebElement Instantiation
WebElement username = driver.findElement(By.id(“Email”));
In the above statement, we instantiate the WebElement reference with the help of “driver.findElement(By.id(“Email”))”. Thus, username can be used to reference the Email textbox on the user interface every time we want to perform some action on it.
Clear Command
username.clear();
The clear() method/command is used to clear the value present in the textbox if any. It also clears the default placeholder value.
sendKeys Command
username.sendKeys(“TestSelenium “);
The sendKeys() method/command is used to enter/type the specified value (within the parentheses ) in the textbox. Notice that the sendKeys() method is called on the WebElement object which was instantiated with the help of element property corresponding to the UI element.
The above block of code enters the string “TestSelenium” inside the Email textbox on the Gmail application.
sendKeys is one of the most popularly used commands across the WebDriver scripts.
Click Command
SignInButton.click();
Like sendKeys(), click() is another excessively used command to interact with the web elements. Click() command/method is used to click on the web element present on the web page.
The above block of code clicks on the “Sign in” button present on the Gmail application.
Notes:
Full free dos games download. Close the Web Browser
driver.close();
The close() is used to close the current browser window.
Terminate the Java Program
System.exit(0);
The Exit() method terminates the Java program forcefully. Thus, remember to close all the browser instances prior terminating the Java Program.
Test Execution
The test script or simply the java program can be executed in the following ways:
#1. Under the Eclipse’s menu bar, there is an icon to execute the test script. Refer the following figure.
Make a note that only the class which is selected would be executed.
#2. Right click anywhere inside the class within the editor, select “Run As” option and click on the “Java Application”.
#3. Another shortcut to execute the test script is – Press ctrl + F11.
At the end of the execution cycle, the print statement “Test script executed successfully.” can be found in the console.
Locating Web Elements
Web elements in WebDriver can be located and inspected in the same way as we did in the previous tutorials of Selenium IDE. Selenium IDE and Firebug can be used to inspect the web element on the GUI. It is highly suggested to use Selenium IDE to find the web elements. Once the web element is successfully found, copy and paste the target value within the WebDriver code. The types of locators and the locating strategies are pretty much the same except for the syntax and their application.
In WebDriver, web elements are located with the help of the dynamic finders (findElement(By.locatorType(“locator value”))).
Sample Code:
driver.findElement(By.id(“Email”));
Locator Types and their Syntax
Conclusion
In this tutorial, we developed an automation script using WebDriver and Java. We also discussed the various components that constitute a WebDriver script.
Here are the cruxes of this Selenium WebDriver Tutorial:
Moving ahead, in the next tutorial, we would shift our focus towards a framework that aids to Automation testing known as TestNG. We would have a detailed study on the various kinds of the annotations provided by the framework.
Next tutorial #11: Before diving deep into Frameworks we will see details about JUnit – an open source unit testing tool. Most of the programmers use JUnit as it is easy and does not take much effort to test. This tutorial will give an insight about JUnit and its usage in selenium script.
A remark for the readers: While our next tutorial of the Selenium series is in the processing mode, readers can start creating their own basic WebDriver scripts. For more advance scripts and concepts, we will have various other Selenium WebDriver tutorials coming up in this series.
Let us know in comments if you have any problem creating or executing the WebDriver scripts.
Recommended Reading
Active5 months ago
I tried this
But i'm getting the error as
Failed tests: setUp(com.TEST): The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see code here . The latest version can be downloaded from Link
How can I make Chrome to test the Selenium-WebDriver test cases?
PrincePrince
2,8511010 gold badges5757 silver badges9595 bronze badges
11 Answers
You need to download the executable driver from: ChromeDriver Download
Then all you need to do is use the following before creating the driver object (already shown in the correct order):
This was extracted from the most useful guide from the ChromeDriver Documentation.
Toby
6,25266 gold badges2828 silver badges5252 bronze badges
aimbireaimbire
3,48911 gold badge1111 silver badges2727 bronze badges
Download update version of chrome driver from Chrome DriverPlease Read the Release note as well HereIf chrome Browser is updated then you need to downloaded new chormedriver from the above link because it would be compact-able with new browser version.
Dio
19122 gold badges33 silver badges1414 bronze badges
Ankit jainAnkit jain
3,37833 gold badges1515 silver badges2424 bronze badges
You should download the chromeDriver in a folder, and add this folder in your PATH variable.You'll have to restart your console to make it works.
some_other_guy
Sample Selenium Webdriver Scripts Online
2,88444 gold badges3030 silver badges5252 bronze badges
Fabrice31Fabrice31
If you're using homebrew on a MacOS, you can use the command:
(EDIT):
brew tap homebrew/cask && brew cask install chromedriver
It should work fine after that with no other configuration.
swanhellaswanhella
You need to install chrome driver. You can install this package using nugget as shown below
Prathap KudupuPrathap Kudupu
Find the latest version of tagomatagoma
chromedriver here.Once downloaded, unzip it at the root of your python installation, eg C:/Program Files/Python-3.5 , and that's it.You don't even need to specify the path anywhere and/or add chromedriver to your path or the like.I just did it on a clean Python installation and that works.
2,35733 gold badges2727 silver badges4343 bronze badges
You can use the below code to run test cases in Chrome using Selenium web-driver:
Laurel
4,8971010 gold badges2222 silver badges4040 bronze badges
Rakesh ChaudhariRakesh Chaudhari
1,34911 gold badge1515 silver badges2121 bronze badges
Download latest version of chrome driver and use this code :
Anuj TeotiaAnuj Teotia
82811 gold badge1010 silver badges1919 bronze badges
All the answers above are correct, following is the little deep dive into the problem and solution.
The driver constructor in selenium for example
searches for the driver executable, in this case chrome driver searches for chrome driver executable, in case the service is unable to find the executable the exception is thrown
this is where the exception comes from (note the check state method)
Following is the check state method which throws the exception
SOLUTION: set the system property before creating driver object as follows
following is the code snippet (for chrome and firefox) where the driver service searches for the driver executable:
Chrome:
FireFox:
where CHROME_DRIVER_EXE_PROPERTY = 'webdriver.chrome.driver'and GECKO_DRIVER_EXE_PROPERTY = 'webdriver.gecko.driver'
similar is the case for other browsers, following is the snapshot of the list of the available browser implementation
chuha.billichuha.billi
On Ubuntu, you can simply install the
chromium-chromedriver package:
![]()
Be aware that this also installs an outdated selenium version. To install the latest selenium:
serv-incserv-inc
19.2k55 gold badges9191 silver badges9999 bronze badges
Download the exe of chromedriver and extract it on current project location.Here the link, where we can download the latest version of chromedriver.
Here the simple code for the launch browser and navigate to url.
ER.swatantraER.swatantra
protected by Community♦Oct 30 '13 at 8:55Selenium Webdriver Wiki
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead? Sample Selenium Webdriver Scripts For FreeNot the answer you're looking for? Browse other questions tagged seleniumwebdriverselenium-webdriverselenium-chromedriver or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |