100% found this document useful (2 votes)
568 views

Selenium Webdriver With Java

The document provides an introduction to test automation using Selenium. It discusses the benefits of test automation such as saving time, repeatability, reusability and increased test coverage compared to manual testing. It also outlines when test cases should and should not be automated. Test cases that involve critical functionality, are repeated frequently or are time consuming are good candidates for automation, while recently designed or frequently changing test cases may not be suitable for automation. The document then provides an overview of Selenium components and how the Selenium WebDriver works.

Uploaded by

Albarg Alymany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
568 views

Selenium Webdriver With Java

The document provides an introduction to test automation using Selenium. It discusses the benefits of test automation such as saving time, repeatability, reusability and increased test coverage compared to manual testing. It also outlines when test cases should and should not be automated. Test cases that involve critical functionality, are repeated frequently or are time consuming are good candidates for automation, while recently designed or frequently changing test cases may not be suitable for automation. The document then provides an overview of Selenium components and how the Selenium WebDriver works.

Uploaded by

Albarg Alymany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 184

Introduction to

Test Automation
Agenda

• Introduction to Test Automation


• Problems of Manual Testing
• Benefits of Test Automation
• When & When not ?
1.
Introduction to Test
Automation
Test Automation

Test automation is the practice


of automatically reviewing and
validating a software product.
Disadvantages of
Manual Testing
Problems of Manual Testing

◎ Time Consuming ex : Form with 25 field

◎ Difficult to test multi language applications

◎ Can become boring

◎ Slow

◎ Can not cover all test coverage


Benefits of Test
Automation
Benefits of Test Automation

◎ Save Time

◎ Repeatability

◎ Reusable

◎ Speed

◎ Increase test coverage


When should we use
Test Automation?
Test Cases to
Automate
◎ Critical Test Cases
◎ Repeatedly Test Cases
◎ Difficult to Manual
◎ Test Cases Time Consuming
Test Cases not
to Automate
◎ Test Case newly design
◎ Test Case requirement changes frequently
◎ Test Cases are executed ad-hoc basis
Thanks!
Introduction to
Selenium
Agenda

• Selenium Introduction
• Selenium Features
• Selenium Components
• How Selenium WebDriver Work
1.
Selenium Introduction
Selenium

◎ Set of tools support test automation for


web applications.

◎ Dealing with HTML , No backend


◎ Allow for locating elements then comparing
expected result and actual result.
2.
Selenium Features
Selenium Features

◎ Open Source

◎ Support Cross Browser

◎ Support Several Programming Language :Java, C#,


Python and Ruby , ..etc.

◎ Record/Playback (Selenium IDE)

◎ Inbuilt Reporting
3.
Selenium Components
Selenium Components

◎ Selenium IDE : Plugin in Firefox only for record/playback.

◎ Selenium Remote Control (RC) : Run code in other server.

◎ Selenium WebDriver : Accept commands then send them


to browsers.

◎ Selenium Grid : Run scripts in multibrowser in parallel.


4.
How Selenium
WebDriver Work
How Selenium WebDriver Work
Thanks!
Selenium Installation
Agenda

• Install JAVA
• Install IDE
• Install Selenium
• Configure Selenium Jars
1.
Install JAVA
Install JAVA

◎ Download Java:
◎ https://www.java.com/en/download/
◎ Install Java JDK
2.
Install IDE
Install IDE

◎ Download IDE:
◎ Eclipse Downloads

◎ Apache NetBeans Releases

◎ Download IntelliJ IDEA

◎ Or any other IDE


3.
Install Selenium
Install Selenium

◎ Download Selenium Java:


◎ https://www.seleniumhq.org/download//

◎ Un Zip Selenium Jars


4.
Configure Selenium Jars
Configure Selenium Jars

◎ Open IntelliJ
◎ File > Project Structure

◎ Modules

◎ Select all Jars in& out lib


Thanks!
Write Your First Script
Agenda

• Write Your First Script Using Selenium WebDriver


Write Your First Script Using Selenium WebDriver

◎ Create Java Project


*Don’t forget configure jars*

◎ Download WebDriver for your


browser
◎ ChromeDriver – WebDriver

◎ Mozilla/geckodriver (github.com)

◎ Microsoft Edge Driver


Write Your First Script Using Selenium WebDriver

◎ Set Property of your browser driver

Browser Driver Path


Write Your First Script Using Selenium WebDriver

◎ Object from WebDriver Class and initiate it to bowser driver.

WebDriver BrowserDriver
Write Your First Script Using Selenium WebDriver

◎ Navigate to URL
Write Your First Script Using Selenium WebDriver

◎ Quit after end your script


Write Your First Script Using Selenium WebDriver

◎ Code
Thanks!
Working with Maven
Agenda
• What is Maven ?
• Why Maven ?
• Create Maven Project
1.
What is Maven ?
What is Maven ?

◎ Maven is a powerful and widely java project.


2.
Why Maven ?
Why Maven ?
◎ Simple project setup that follows best practices.

◎ Adding jars files automatically.

◎ Managing dependency using pom.xml.

◎ Maven download the newer versions


automatically and store them in local repository.
2.
Create Maven Project
Create Maven Project
◎ Download Maven:

◎ Maven – Download Apache Maven

◎ Setup Environment variables 3

2 4

1
Create Maven Project

◎ Create Maven Project:

◎ Maven – Download Apache Maven

◎ Add dependencies

◎ MavenRepository
Thanks!
Selenium Basic Method
Agenda
• get(“url”); • navigate() ;
• getCurrentUrl(); • manage();
• getTitle(); • close();
• getPageSource(); • quit();
• getWindowHandle();
get(“url”);

◎ To launch any Web Application we use Get() to insert the URL as


the below code:

driver.get("https://www.google.com/");
navigate() ;

◎ Navigate() method can be used for many operation in browsers


driver.navigate().
manage();

◎ The control the screen size of the browser

driver.manage().window().
getCurrentUrl();

◎ To get the URL of the current website launched by

driver.getCurrentUrl();
getTitle();

◎ To get the page title of the current website launched

driver.getTitle();
getPageSource();

◎ To get the code or the source of the current website

driver.getPageSource();
getWindowHandle();

◎ To get the Context unique identifier of the current page .

driver.getWindowHandle();
close();

◎ The below method will close the current tab

driver.close();
quit();

◎ The below method will close All tabs

driver.quit();
Thanks!
Web Elements and Locators
Agenda

• Web Elements
• Web Elements Locator (Inspector)
1.
Web Element
Web Element
◎ Any individual item existed in web page is
called element.

◎ Every element has its properties.


2.
Web Elements Locator
(Inspector)
Web Elements Locator

◎ There are many ways to inspect the


Elements to know their properties.
Locators
◎ ID

◎ Name

◎ TagName

◎ Link Text - Partial link text

◎ Class Name

◎ Xpath

◎ CSS
ID Locator

◎ ID is one of the important Locator for Selenium to find elements,


You can use the below code:

driver.findElement(By.id(“Value"));

-Not all elements have IDs
The ID might be auto-generated
Name Locator

◎ Name is one of the important Locator for Selenium to find


elements, You can use the below code:

driver.findElement(By.name(“Value"));

Some elements might have neither ID nor
Name
In this case, we use other selectors
Class Name Locator

driver.findElement(By.Class(“form-control"))

“Compound classes are not allowed in
class name locator ”

◎ Compound Classes :Classes that have Spaces between the words



Class name is not preferred ?
because usually more elements may have
the same class name .
TagName Locator
TagName Locator
Link Text Locator

driver.findElement(By.linkText(“Value"))
Link Text Locator
Link Text Locator
Partial Link Text Locator

driver.findElement(By.partiallinktext(“Value"))
Partial Link Text Locator
Partial Link Text Locator
CSS Locator

◎ CSS using Id

driver.findElement(By.cssSelector(“#id”);

◎ CSS using ClassName


driver.findElement(By.cssSelector(“.className”);

◎ CSS using attribute


driver.findElement(By. cssSelector(“[attribute=value]”);
CSS Locator

◎ CSS with children “space”(Direct or Indirect)

driver.findElement(By.cssSelector(“.parent .child”);

◎ CSS with children “>”(Direct only)


driver.findElement(By.cssSelector(“.parent>.child”);

◎ nth-of-type(index)
driver.findElement(By.cssSelector(“.parent .child:nth-of-type(index)”);
XPath Locator

◎ XPath is one of the important locators for Selenium to find


elements, You can use the below code:

driver.findElement(By.xpath(“//tagName[@attribute = ‘value’]”);

driver.findElement(By.xpath(“//input[@type = ‘search’]”);

Note : you can use * to select any tag


driver.findElement(By.xpath(“//*[@attribute = ‘value’]”);
XPath Locator

◎ Contains
driver.findElement(By.xpath(“//*[contains(@attribute,‘partial value’)]”);

◎ Text
driver.findElement(By.xpath(“//*[text()=‘value’]”);
Thanks!
Interact with browser
Send Keys
◎ Firstly find the element then use SendKeys(“ ”)

driver.findElement(By.xpath(“//*[@attribute,‘value’]”).sendKeys(“ ");
Click
◎ Firstly find the element then use Click(“ ”)

driver.findElement(By.xpath(“//*[@attribute,‘value’]”).click(“ ");
getText
Drop Down
◎ Firstly find the element then use Select Class
WebElement element =driver.findElement(By.cssSelector("[type=submit]"));

Select DropDown = new Select(element);


SelectByVisibleText
SelectByValue
SelectByIndex
getFirstSelectedOption()
Size()
WebTables
WebTables
Checkboxes
Is Displayed
◎ Firstly find the element then use isDisplayed();
driver.findElement(By.cssSelector("[type=submit]")).isDisplayed();

◎ Returns true if element exists in page , false if it doesn’t

exist.
Is Enabled
◎ First find the element then use isEnabled();
driver.findElement(By.cssSelector("[type=submit]")).isEnabled();

◎ Returns true if element editable , otherwise return false


Is Selected
◎ Firstly find the element then use isSelected();
driver.findElement(By.cssSelector("[type=submit]")).isSelected();

◎ Return true if element(Radio Button/Check Box) for

example is selected , otherwise return false


Checkboxes
IsElementPresent
Double Click
◎ Firstly find the element then use Action(); doubleClick();
WebElement element=driver.findElement(By.cssSelector("[type=submit]"));

Actions action=new actions (driver);


action.doubleClick(element).perform();

◎ Perform : for one action


◎ Build : for more actions
Right Click
◎ Firstly find the element then use Action(); contextClick();
WebElement element=driver.findElement(By.cssSelector("[type=submit]"));

Actions action=new actions (driver);


action.contextClick(element).perform();
Move To Element
◎ Firstly find the element then use Action(); moveToElement();
WebElement element=driver.findElement(By.cssSelector("[type=submit]"));

Actions action=new actions (driver);


action.moveToElement(element).perform();
Drag & Drop
◎ Firstly specify Source & Destination the element then use Action();
dragAndDrop();
WebElement source=driver.findElement(By.cssSelector("[type=submit]"));
WebElement destination=driver.findElement(By.cssSelector("[type=submit]"));

Actions action=new actions (driver);


action.dragAndDrop(source, destination).perform();
Click & Hold
◎ Firstly specify Source & Destination the element then use Action();
clickAndHold(); moveToElement;
WebElement source=driver.findElement(By.cssSelector("[type=submit]"));
WebElement destination=driver.findElement(By.cssSelector("[type=submit]"));

Actions action=new actions (driver);


action.dragAndDrop(source). moveToElement(destination).release().build().perform();

◎ Release : as leave mouse


◎ Build : for more actions
Thanks!
Multiple Windows
getWindowHandle();

◎ To get the Context unique identifier of the current page .

String WindowId = driver.getWindowHandle();


getWindowHandles();

◎ To get the Context unique identifier of the all pages you clicked in
set {p1,p2,p3,p…}.

Set<string> allWindows = driver.getWindowHandle();


switchTo();
◎ To switch between windows

driver.switchTo().window(window);
close();

◎ The below method will close the current tab

driver.close();
quit();

◎ The below method will close All tabs

driver.quit();
quit();

◎ The below method will close All tabs

driver.quit();
Thanks!
Frames
frame();
frame();

driver.switchTo().frame(“name”);
frame();

driver.switchTo().frame(“id”);
frame();

Web element = driver.findElement(By.id(“id”))

driver.switchTo().frame(“web element”);
parentFrame();

◎ After finishigh your work in any frame you need to switch to


parent frame.

driver.switchTo().parentFrame()
Nested Frames

Main Frame

2
3
Parent
Frame
Child
Frame
Thanks!
Threads & Waits

Selenium doesn’t wait if any
element take seconds to be
shown in website.
Thread.sleep();

Thread.sleep(milli seconds);
implicitlyWait();

Is used in all session , and be apply only for element that's take time.

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(seconds))
Disadvantages of implicit Wait

◎ Only wait if any element take seconds to be shown in website.


◎ Don’t care if element is visible or is enabled or clickable or …
Explicit Wait

◎ Wait based on condition

new WebDriverWait(driver,Duration.ofSeconds(10)).until(ExpectedConditions.)

Do not mix implicit and explicit waits.
Doing so can cause unpredictable wait
times
Thanks!
Alerts
alert();

driver.findElement(By.id(“id”))

driver.switchTo().alert().accept();
Get text from alert();

driver.switchTo().alert().getText();
Send keys to alert();

driver.switchTo().alert().sendKeys(“Value”);
Thanks!
Testing Framework
(TestNG)
Core functions of a Test Framework

◎ Create & Execute Test Scripts


◎ Generate Test Reports
◎ Generate Logs
◎ Read & Write Test Data

159
1.
What is TestNG

160
What is TestNG

◎ TestNG is a testing framework introducing some new functions


that make automation more powerful and easier to use.

◎ TestNG is designed to cover all categories of tests: unit,


functional, end-to-end, integration, etc....

161
TestNG Functionality
◎ Annotations.
◎ Run your tests in arbitrarily big thread pools (in their own
thread)
◎ Flexible test configuration.
◎ Powerful execution model (no more TestSuite).
◎ Supported by a variety of tools and plug-ins (Eclipse, IDEA,
Maven, etc...).

162
Why TestNG

◎ TestNG in Selenium provides an option testng-failed.xml, If


you want to run only failed test cases.

◎ Each test case executed and evaluated separately.

◎ Create testing suite file where Multiple test cases can be


grouped more easily by converting them into testng.xml file.

163
2.
Why TestNG

164
Why TestNG

◎ Can make priorities which test case should be executed first.

◎ Parallel testing is possible using TestNG

◎ Parametrization provided by TestNG is more convenient and


easier to use through data provider

165
3.
Adding TestNG to POM.xml

166
Adding TestNG to POM.xml

◎ 1-Go to https://mvnrepository.com
◎ 2-Go to the latest version stable
◎ 3-Add dependency in pom.xml file

167
4.
TestNG Annotations

168
TestNG Annotations

◎ An annotation is a tag that provides information about the


method, class, and suite. It helps to define the execution
approach of your test cases and the different features
associated with it.

169
Pre-conditions

◎ @BeforeSuite: run before all tests in this suite have run.

◎ @BeforeTest: run before any test method belonging to the classes inside the tag is run.

◎ @BeforeClass: runs before the execution of test methods in a current class.

◎ @BeforeMethod: annotated method will be run before each test method.

170
Post-conditions

◎ @AfterClass: run after all the test methods in the current class have been run.

◎ @AfterSuite: run after all tests in this suite have run.

◎ @AfterTest: run after all the test methods belonging to the classes inside the tag

have run.

◎ @AfterMethod: annotated method will be run after each test method.

171
Execution Order
1. Before Suite
2. BeforeTest
3. BeforeClass
4. BeforeMethod
5. Test
6. AfterMethod
7. AfterClass
8. AfterTest
9. AfterSuite

172
5.
TestNG Assertions

173
TestNG Assertions

◎ TestNG provides multiple level assertions to validate your


actual results against your expected results.

◎ If the actual result is equal to expected result: Pass

◎ If the actual result is not equal to expected result: Fail

174
TestNG Assertions

◎ assertTrue: Verifies whether the defined condition is true or not. If true, it will pass

the test case. If not, it will fail the test case

Assert.assertTrue(condition, Message);

◎ assertFalse: Verifies whether the defined condition is false or not unlike

assertTrue.
Assert.assertFalse(condition, Message);
175
TestNG Assertions

◎ assertEquals: Compares the expected value with actual value If both are the

same, it passes the test case. If not, it fails the test case.

Assert.assertEquals(actual, expected, Message(optional) );

◎ assertNotEquals: It’s just opposite to what assertEquals does

Assert.assertNotEquals(actual, expected, Message(optional) );


176
6.
Assertion Types

177
Assertion Types

Soft Assert Hard Assert

178
Hard Assert

◎ If assert condition gets failed in Hard Assert :


◎ TestNG throws an exception immediately after the assert fails
◎ The remaining statements inside the method will be aborted.
◎ TestNG carries out with the next test case of the suite.

179
Soft Assert
◎ If assert condition gets failed in Soft Assert :

◎ TestNG collects errors during execution and doesn’t throws an exception

◎ The remaining statements inside the method will be executed

◎ If there is any exception and you want to throw it then you need to use assertAll()

◎ TestNG will carry out with the next test case of the suite.

180
7.
TestNG Priority

181
TestNG Priority

◎ Prioritization in TestNG is a way to provide a sequence to the methods so that they


do not run out of order.

182
TestNG Priority
◎ Priority in TestNG test cases is a parameter with attribute value
○ E.X: @Test(priority=1)

◎ If no priority mentioned, Testng will execute the @Test methods based on

alphabetical order of their method names irrespective of their place of


implementation in the code.

183
TestNG Priority Notes

◎ Definition of Priority in TestNG test methods can only be the @Test methods.

◎ TestNG will execute @Test annotation with the highest priority (priority = 1) to the

lowest one

◎ One method is allowed to have only one priority in TestNG.

184

You might also like