Selenium # Selenium Implicit and Explicit wait
//Implicit Wait
driver.manage()
.timeouts().implicitlyWait(10,
TimeUnit.SECONDS)
// Explicit wait
WebDriverWait wait = new
WebDriverWait(driver,
20);
wait.until(Expected
Conditions.textTo
BePresent
InElementLocated
Selenium # Selenium Keyboard and Mouse events
keyDown(); keyUp(); sendKeys();doubleClick();
Code Example:
Actions builder = new Actions(driver);
Action actions = builder
.moveToElement(“logintextbox”)
.click()
.keyDown(“logintextbox”, Keys.SHIFT)
.sendKeys(“logintextbox”, “hello”)
.keyUp(“logintextbox”, Keys.SHIFT)
.doubleClick(“logintextbox”)
.contextClick()
.build();
actions.perform() ;
What is Cucumber BDD for Selnium tests
Cucumber BDD (Behaviourdriven development):Cucumber is a testing toolused for Software testingwith behaviour drivendevelopment approach.Cucucmber BDD comes with feature file, whereacceptance, functional,regression tests are written in plain english called Gherkin language…Sample feature file inGherkin language is like below:
Feature: my app login test
Scenario: positve test on login
Given user enters correct credentials
Then user should be able to login
Scenario: login negative test
Given user enters wrong password
Then login is not allowed
in the above feature file,each line below to
Scenario, i. e Given, Then steps are mapped with java methods to perform selenium Automation tests
What is TestRunner in Cucumber:
TestRunner is a programused in Cucumber toaccess Feature fileTestRunner drives theselenium automationfeatures executionFeature file is somethingthat has user requirementscenarios written in Englishwhich gives more readabilityand understandability ofthe requirement which iscalled Gherkin language#shorts
Unit testing mocks, stubs
Mocks replaces external Interface. Mocks are not to check thereturn value but to verify function level call happened,called correctly etc.Stubs are replacement ofreturn value and is to test behavior of code.Stubs generates the pre defined output fakes to replace actual implemention like to replaceweb server with local httpserver or replace db serverwith fake in memory db to generate db response
TestNG Parameterization:
Approach 1: feed input parameters via TestNGand receive using@Parameters annotation at the target method
Approach 2: use Dataprovider when complex parameters like data from database, xls,property file, arraylist etc
What is Single slash ‘/’in Xpath in selenium:
Single slash ‘/’ is usedwhen absolute Xpathis considered for Elementidentification for SeleniumWebDriver
What is Double slash ‘//’in Xpath in selenium:
Double slash ‘//’ is usedwhen relative Xpathis considered for Element identification for SeleniumWebDriverRelative Xpath ‘//’is better Element identification strategy
What is Polymorphism:
Polymorphism is one
of the OOPs concepts
where many forms or
characteristics shown
by single java method
Different types of
Polymorphism are,
1. compile time/static
Polymorphism
2. Runtime/Dynamic
Polymorphism
1. Compile time/static
Polymorphism is
achieved by Method
overloading
2. Runtime/Dynamic
Polymorphism is
achieved by Method
overriding
Method overloading
is feature where
many methods having
same name but different
argument types
Method overriding is
a feature where child
class method override the
parent class method
#Interface:
Interface is like
class having variables
and methods but methods
are abstract by default.
abstract methods mean
no body/implementation
Any class can
implement Interface and
achieve abstraction and
multiple inheritance
#encapsulation#java#selenium#OOPS is binding of data or variableswith code/methodsas a sigle unit from classthis helps hiding datafrom other classes whendata is declared as private.and keeping methods/codeas public, other class canget data.
#shorts#Abstractclass#cpncreatclassAbstract class:a class declaredusing abstract keywordand has abstract methodsAbstract methodsdoes not haveimplementationor bodyConcreat class:Concreat class extendsAbstract class and implements the methodsfrom Super Abstract classConcreta class does notuse Abstract keywordSelenium classes:Selenium classesextends Concreatclass and instantiateConcreat class touse the implementedmethods from Concreat class
#selenium
#captcha
Selenium: Handling Captcha using Sikuli
Purpose of Captcha
is to avoid Automation.
However some level
of Captcha automation
can be done using Sikuli
Sikuli can capture
image captcha having
numbers or letters
as contents and store
as string values
to continue Automation
Sikuli is a library
that works based
on image recognition
Sikuli can read text from
image and that can be
used for some level of
captcha Automation
#shorts
#dybamicobjects
#selenium
Selenium:
Handling
dynamic
elements
Dynamic Objects
are those it’s id or
other properties
are changing
during page load
or other
user actions
How to handle
dynamic objects:
1. use XPath axes
methods :
use child, parent, sibling
elements to write Xpath
2. use dynamic Xpath
having “contains,
or starts with
or ends with etc
#shorts
What is SelectorsHub?
It is a Browser extension
helps to auto suggest
XPath or CSS Selector
How to setup SelectorsHub
1. download and install from
www.selectorshub.com
for browser type
chrome, opera, edge, firefox
2. after adding extention to
browser, it will appear
as browser toolbar item
3. after restarting
browser inspect any
WebElement,
SelectorHub will be
displayed on Elements
tab on right
4. start typing XPath or
CSS Selector, you
would notice SelectorsHub
auto suggesting
Xpath or CSS Selector
Ref : www.selectorshub.com
#shortsSwitch to Frame:driver.switchTo().frame(“frame name/frame index”);Switch backfrom Frame:driver.switchTo().defaultContent();note: commandsare single line
#shorts
#selenium
#locators
Different
Selenium Locators :
By.id()
By.name()
By.tagName()
By.className()
By.xpath
By.cssSelector()
By.linkText()
By.partialLinkText()
#shorts
#selenium
#Exceptions
Different
WebDriver Exceptions :
1.WebDriver
Exception
2.Timeout
Exception
3. NoAlertPresent
Exception
4. Nosuchwindow
Exception
5. NoSuchElement
Exception
#shorts
#selenium
#css
#xpath
#seleniumlocator
XPath:
1. XPath(XML Path)
is used to find element
in HTML DOM
2. XPath is the locator
type used in Selenium
3. High success rate
in finding elemet
4. Xpath is slower in IE
CSS(Cascading Style
Sheet )
1. CSS Selector, a
selenium locator type,
used to find
elements using style
language
2. Faster in all
Browsers but may not
work on certain browser
elements
#shorts
1. W3C standard protocol
is used by Selenium 4
WebDriver to
communicate
to Browser, which makes
communication
standardised
and no API encoding
/decoding required
2. Logging and tracing are
improved for better
monitoring
3. Better documentation on
selenium features
4. enabled two url
opening on two browser
tabs
5. Relative Locators
like. toRightOf(),
above(), near()
6.Opera and
PhantomJs support
are removed
7. Optimized
SeleniumGrid
by fixing open issues
#shorts
for chrome:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(“browserName”, “chrome”);
caps.setCapability(“browserVersion”, “80.0””);
caps.setCapability(“platformName”, “win10”);
WebDriver driver = new ChromeDriver(caps);
// Pass the capabilities
as an argument to driver object
for Firefox:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(“browserName”, “chrome”);
caps.setCapability(“browserVersion”, “80.0””);
caps.setCapability(“platformName”, “win10”);
WebDriver driver = new FirefoxDriver(caps);
// Pass the capabilities
as an argument to driver object
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(“browserName”, “chrome”);
caps.setCapability(“browserVersion”, “80.0””);
caps.setCapability(“platformName”, “win10”);
WebDriver driver = new ChromeDriver(caps);
// Pass the capabilities
as an argument to driver object
#shorts// to retrieve the current URL of the webpagegetCurrentUrl() // to retrieve the current page source of the webpagegetPageSource() //to retrieve the text of the specified web elementgetText() //to retrieve the value specified in the attributegetAttribute() //to retrieve the current title of the webpagegetTitle()getCurrentUrl() getPageSource() getText()getAttribute() getTitle()
Driver will switch to specific
frame using
Name or ID or index
:::
driver.switchTo.frame
(“frameName value”)
or
driver.switchTo
.frame(“ID value”)
or
driver.switchTo.frame(0)
Fluent Wait in Selenium is used for defining the maximum time for the webdriver to wait for a condition, and also the frequency with which we want to check the condition before throwing Exception
Handle multiple Browser windows and tabs
getWindowHandle()
//to retrieve the handle of the current page
getWindowHandles() vs getWindowHandle()
driver.close(); // closes the current browser window
driver.quit(); //This method Closes all browser windows opened by the WebDriver