Selenium Theory
Selenium Theory
1. Selenium is a free (open source) automated testing suite for web applications
across different browsers, platforms and programming languages.
2. Selenium can be easily deployed on platforms such as Windows, Linux, Solaris and
Macintosh. Moreover, it supports for mobile applications like iOS and android.
3. Languages supported by Selenium include C#, Java, Perl, PHP, Python and Ruby.
4. Browsers supported by Selenium include Internet Explorer, Mozilla Firefox,
Google Chrome and Safari.
5. Automation testing covers both functional and performance test on an application.
Selenium Features
1. Selenium is a free (open source) automated testing suite for web applications
across different browsers, platforms and programming languages.
2. Selenium IDE provides a playback and record feature for authoring tests without the need
to learn a test scripting language.
3. It helps testers to record their actions and export them as a reusable script with a
simple- to-understand and easy-to-use interface.
4. It also supports parallel test execution which reduces time and increases the efficiency
of tests.
5. Selenium can be integrated with frameworks like Ant and Maven for source ne code
compilation, and can be integrated with testing frameworks like TestNG for
application testing and generating reports.
6. Selenium requires fewer resources as compared to other automation test tools.
7. Selenium web driver does not require server installation, test scripts interact Open
directly with
the browser.
Selenium Limitations
1. Selenium does not support automation testing for desktop applications.
2. Selenium requires high skill sets in order to automate tests more effectively.
3. Since Selenium is open source software, you have to rely on community forums to get
your technical issues resolved.
4. We should know at least one of the supported programming languages to create tests
scripts in Selenium WebDriver.
5. Selenium does not have any inbuilt reportingcapability; you have to rely on plug-ins like
JUnit and TestNG for test reports.
6. It is not possible to perform testing on images.
7. No one is responsible for new features usage; they may or may not work properly.
1/47
Selenium Tool Suite
1. Selenium is not just a single tool but a suite of software's, each catering to different testing
needs of an organization. It has four components.
Selenium Integrated Development Environment (IDE)
Selenium Remote Control (RC) [Now Deprecated]
WebDriver
Selenium Grid
Selenium IDE
1. Selenium Integrated Development Environment (IDE) is the simplest framework in the
Selenium suite and is the easiest one to learn.
2. It is a Firefox/Chrome plugin that you can install as easily as you can with other plugins.
3. However, because of its simplicity, Selenium IDE should only be used as a
prototyping tool.
4. Selenium IDE is implemented as Firefox extension which provides record and
playback functionality on test scripts.
5. It allows testers to export recorded scripts in many languages like HTML, Java, Ruby,
RSpec, Python, C#, JUnit and TestNG. You can use these exported script in Selenium RC
or Webdriver.
Selenium IDE-Installation
1. Open Selenium official website
seleniumhq.org
(https://www.selenium.dev/)
2. Then click on Download Section.
3. In the webpage search for Selenium IDE and click on Chrome to install Google
Chrome plug-in or click on Firefox to install Firefox Plug-in.
4. Restart you browser, go to the top right corner on your browser and look for the
Selenium IDE icon.
5. Click on that icon to launch Selenium IDE.
Selenium IDE-Features
1. Very easy to use and install.
2. No programming experience is requied.
3. Export tests to different programming languages.
Selenium IDE-Limitations
1. Available in only Firefox and Chrome.
2. Designed only to create prototypes of tests.
3. Test execution is slow.
Selenium Webdriver
2/47
1. Selenium WebDriver is the most important component of Selenium Tool's Suite.
2. The initial version of Selenium i.e Selenium v1 consisted of only IDE, RC and Grid.
Selenium WebDriver was first introduced as a part of Selenium v2.0. However, with
the release of Selenium v3, RC has been deprecated and moved to legacy package.
3. In WebDriver, test scripts can be developed using any of the supported programming
languages and can be run directly in most modern web browsers. Languages
supported by WebDriver include C#, Java, Perl, PHP, Python and Ruby.
4. Selenium Web driver is most popular with Java and C#.
5. Selenium WebDriver performs much faster as compared to Selenium RC because it
makes direct calls to the web browsers. RC on the other hand needs an RC server
to interact with the browser.
6. Selenium uses drivers, specific to each browser in order to establish a secure
connection with the browser without revealing the internal logic of browser's functionality.
7. WebDriver is supporting dynamic web pages where elements of a page may
change without the page itself being reloaded.
8. The more pain while doing automation is the handling Javascripts alerts & prompts. The
WebDriver very verse with handle the Javascript alerts, prompts and handling multiple
frames, multiple browser windows.
Selenium WebDriver-
Features Multiple Browser
Support Multiple Languages
Support
Speed
Simple Commands
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//InternetExplorer
System.setProperty("webdriver.ie.driver", "(2)\\chromedriver.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("http://google.com");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
//GoogleChrome
System.setProperty("webdriver.chrome.driver", ""(2)\\chromedriver.exe ");
WebDriver driver2 = new ChromeDriver();
driver2.manage().window().maximize();
driver2.get("http://google.com");
System.out.println(driver2.getTitle());
System.out.println(driver2.getCurrentUrl());
//Firefox
System.setProperty("webdriver.gecko.driver", " geckodriver.exe");
WebDriver driver3 = new InternetExplorerDriver();
driver.manage().window().maximize();
driver3.get("http://google.com");
System.out.println(driver3.getTitle());
System.out.println(driver3.getCurrentUrl());
}
}
4/47
For example System.setProperty("webdriver.ie.driver", "E:\\Softwares\\
SeleniumDrivers\\IEDriverServer.exe");
2. In the next step we have created a browser object to access and to perform some
operations on that browser. WebDriver defines common methods which all browser
classes (such as Firefox, Chrome etc.,) use. All these class methods are derived from
WebDriver interface.
For example WebDriver driver = new InternetExplorerDriver();
3. The WebDriver provides the window interface for setting up the browser window
size, state, and so on. When we call the maximize() method, the browser window
will be maximized from normal or minimized state.
For example driver.manage().window().maximize();
4. If we want to open a specific URL on that browser we will use get() method. In that
method we have to pass the specific URL.
For example driver.get("http://google.com");
5. If we want to get current page title we wil use driver.gettitle() method and if we want
to get current page URL we will use driver.getCurrentUrl() method.
5/47
8. By xpath: Locates the web element using its XPaths
WebElement element = driver.findElement(By.xpath("//div[@id=’elementId’]"));
we prefer using id because id of elements are generally unique. But there can be
scenarios where we might not have id attributes of web elements, also other locators like
name, className might not fetch the unique required web element. In those scenarios,
we should use cssSelector and xpath locators.
The sendKeys() method is used to enter the specified value in the textbox.
driver.findElement(By.cssSelector("div#elementId")).sendKeys(“TestSelenium “);
Click() method is used to click on the web element present on the web page.
driver.findElement(By.linkText("Click Here")).click();
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class LocatorsDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://fb.com");
driver.findElement(By.id("email")).sendKeys("9052938526");
driver.findElement(By.name("pass")).sendKeys("password");
driver.findElement(By.className("inputtext")).sendKeys("abhishek@gmail.com");
driver.findElement(By.tagName("input")).sendKeys("751111112222");
driver.findElement(By.partialLinkText("Forgotten")).click();
driver.findElement(By.linkText("Forgotten Password?")).click();
driver.findElement(By.cssSelector("#u_0_q")).sendKeys("99999999");
driver.findElement(By.xpath("//*[@id=\"reg_pages_msg\"]/a")).click();
}
}
Browser Commands
get() method: The driver.get() method is used to navigate to a web page by passing the
string URL as parameter.
driver.get("http://facebook.com");
getTitle() method: In WebDriver, this method fetches the title of the current web page.
System.out.println(driver.getTitle());
6/47
getCurrentUrl() method: In WebDriver, this method fetches the string representing the
Current URL of the current web page.
System.out.println(driver.getCurrentUrl());
close() method: The driver.close() command is used to close the browser having focus.
driver.close();
quit() method: The driver.quit command is used to close all the browser instances open.
driver.quit();
Navigation Commands
navigate().to() method: The driver.navigate().to() method does the task of opening a web
page like driver.get() method.
driver.navigate().to("http://youtube.com");
navigate().back() method: Selenium provides navigate().back() command to move
backwards in the browser's history.
driver.navigate().back();
navigate().forward() method: Selenium provides navigate().forward() command to move
forward in a browser.
driver.navigate().forward();
navigate().refresh() method: It is used to refresh a page in Selenium WebDriver.
driver.navigate().refresh();
sendKeys(Keys.F5) method: It is used to refresh a page in Selenium WebDriver on any
textbox on the webpage
driver.findElement(By.id("id123")).sendKeys(Keys.F5);
WebElement Commands
click() method: The click() method in Selenium is used to perform the click operation on
web elements.
driver.findElement(By.id("button1")).click();
sendKeys() method: The sendKeys() method can be used for writing in a textbox or any
element of text input type.
driver.findElement(By.id("firstname")).sendKeys("Abhishek");
clear() method: The clear() method can be used to clear the text written in a textbox or
any web element of text input type.
driver.findElement(By.name("surname")).clear();
getText() method: In automation, many a times we need to fetch the text written over a
web element for performing some assertions or debugging. For this, we have getText()
method in selenium webDriver.
driver.findElement(By.id("email")).getText();
isDisplayed() method: If the element is displayed it will return TRUE otherwise it will
return FALSE.
driver.findElement(By.id("UserName")).isDisplayed();
isEnabled() method: It is used to test whether the element is enabled to perform some
action or not.
driver.findElement(By.id("UserName")).isEnabled();
isSelected() method: It will display TRUE if the element is selected otherwise it will print
FALSE.
7/47
driver.findElement(By.id("Sex-Male")).isSelected();
submit() method: It will submit the webpage to the server.
driver.findElement(By.id("SubmitButton")).submit();
8/47
Handling Dropdown in Selenium
1. The 'Select' class in Selenium WebDriver is used for selecting an option in a
dropdown. The objects of Select type can be initialized by passing the dropdown
webElement as parameter to its constructor.
2. To perform any action, the first task is to identify the element group. I am saying it a
group, as DropDown /Multiple Select is not a single element. They always have a single
name but and they contain one or more than one element in them.
Select dropdown = new Select(driver.findElement(By.name("country")));
3. WebDriver provides three ways to select an option from the drop-down menu.
4. We can use selectByIndex() to select an option based on its index, beginning with
0. dropdown.selectByIndex(5);
5. We need to use selectByValue() to select an option based on its 'value'
attribute. dropdown.selectByValue("India");
6. We can use selectByVisibleText() to select an option based on the text over the
option. dropdown.selectByVisibleText("Database Testing");
7. We can use getOptions( ) to get the all options belonging to the Select tag. It takes
no parameter and returns List<WebElements>.
List <WebElement> options = dropdown.getOptions();
int size=options.size();
System.out.println(size);
for(int i =0; i<size ; i++){
String optValue = options.get(i).getText();
System.out.println(optValue);
}
9/47
Handling Tables in Selenium
1. There are two types of HTML tables published on the web.
Static tables : Data is static i.e. Number of rows and columns are fixed.
Dynamic tables : Data is dynamic i.e. Number of rows and columns are NOT fixed.
2. Handling static table is easy, but dynamic table is a little bit difficult as rows and
columns are not constant.
3. Below tags are generally defined in html tables
: ’table’ tag defines HTML table.
’tbody’ tag defines a container for rows and columns.
’tr’ defines rows in an HTML table.
’td’/’th’ define the column of an HTML table.
4. First get the entire HTML table and store this in a variable of type web element.
WebElement
htmltable=driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody"));
5. Get all the rows with tag name ‘tr’ and store all the elements in a list of web elements.
Now all the elements with tag ‘tr’ are stored in ‘rows’ list.
List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
11/4