AUTOMATION TESTING
what is selenium?
selenium is a tool it interacts you code to UI using JSON protocol wire(webdrive)
what is maven?
Maven is a built automation tool from Apache Software Foundation and is commonly used to
handle Java projects.
It is used to add dependencies.
(or)
It is a software project management tool that provides a new concept of project object model
(POM).
What is TestNG?
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new
functionality that makes it more powerful and easier to use.
what is Drivermanager?
Selenium WebDriver is an automation tool widely popular and is useful to run tests against
multiple browsers like Google Chrome browser, Firefox Browser, Internet Explorer, etc.
Command - [Link]().setup();
Types Of TestNG Annotations
In TestNG, there are ten types of annotations:
@BeforeSuite - The @BeforeSuite method in TestNG runs before the execution of all other
test methods.
@AfterSuite - The @AfterSuite method in TestNG runs after the execution of all other test
methods.
@BeforeTest - The @BeforeTest method in TestNG runs before the execution of all the test
methods that are inside that folder.
@AfterTest - The @AfterTest method in TestNG executes after the execution of all the test
methods that are inside that folder.
@BeforeClass - The @BeforeClass method in TestNG will run before the first method
invokes of the current class.
@AfterClass - The @AfterClass method in TestNG will execute after all the test methods of
the current class execute.
@BeforeMethod - The @BeforeMethod method in TestNG will execute before each test
method.
@AfterMethod - The @AfterMethod method in TestNG will run after each test method is
executed.
@BeforeGroups - The @BeforeGroups method in TestNG run before the test cases of that
group execute. It executes just once.
@AfterGroups - The @AfterGroups method in TestNG run after the test cases of that group
execute. It executes only once.
Brower commands
appUrl
Where appUrl is the website address to load. It is best to use a fully qualified URL.
Command - [Link](appUrl);
getTitle()
This method fetches the Title of the current page. Accepts nothing as a parameter and returns
a String value.
Command - [Link]();
getCurrentUrl()
This method fetches the string representing the Current URL which is opened in the browser.
Accepts nothing as a parameter and returns a String value.
Command - [Link]();
getPageSource()
This method returns the Source Code of the page. Accepts nothing as a parameter and returns
a String value.
Command - [Link]();
close()
This method Close only the current window the WebDriver is currently controlling. Accepts
nothing as a parameter and returns nothing.
Command - [Link]();
quit()
This method Closes all windows opened by the WebDriver. Accepts nothing as a parameter
and returns nothing.
Command - [Link]();
Navigation Commands
driver. get() is used to navigate particular URL(website) and wait till page load. driver.
navigate() is used to navigate to particular URL and does not wait to page load.
Webelement commands
Sendkeys
This method works fine with text entry elements like INPUT and TEXTAREA elements.
Command - [Link]("text");
Example : [Link]([Link], driver, "November
07, 1999 8:00 PM");
Click
Clicking is perhaps the most common way of interacting with web elements like text
elements, links, radio boxes and many more.
Command - [Link]();
Example : [Link]([Link], driver);
isDisplayed
This method determines if an element is currently being displayed or not. This accepts
nothing as a parameter but returns a boolean value(true/false).
Command - [Link]();
Example : if ([Link]()) {
[Link](" SubSubItem1 is passed");
[Link]([Link], "SubSubItem1 is pass",
[Link]([Link](driver)));
} else {
[Link]("SubSubItem1 is fail");
[Link]([Link], "SubSubItem1 is fail",
[Link]([Link](driver)));
}
isEnabled
This will generally return true for everything but I am sure you must have noticed many
disabled input elements in the web pages.
Command - [Link]();
isSelected
This operation only applies to input elements such as Checkboxes, Select Options, and Radio
Buttons. This returns True if the element is currently selected or checked, false otherwise.
Command - [Link]();
Submit
If this causes the current page to change, then this method will wait until the new page is
loaded.
Command - [Link]();
getText
This returns an innerText of the element, including sub-elements, without any leading or
trailing whitespace.
Command - [Link]();
Example : if ([Link]().equalsIgnoreCase("This is a
sample page")) {
[Link]("new tab is passed");
[Link]([Link], "new tab is passed",
[Link]([Link](driver)));
} else {
[Link]("new tab is fail");
[Link]([Link], "new tab is fail",
[Link]([Link](driver)));
}
FluentWait
FluentWait instance defines the maximum amount of time to wait for a condition, as well as
the frequency with which to check the condition.
Command - [Link]();
Example : [Link](widgetsxp.SubSubItem1, driver);
Threadsleep
It always force the browser to wait for a specific time.
Command - [Link](1000);
Scroll
It used to scroll the web page
Command - [Link]();
Example : [Link](widgetsxp.MainItem2, driver);
Locators
ClassName – A ClassName operator uses a class attribute to identify an object.
cssSelector – CSS is used to create style rules for webpages and can be used to identify any
web element. CSS is faster than Xpath.
Id – Similar to class, we can also identify elements by using the ‘id’ attribute.
linkText – Text used in hyperlinks can also locate element
name – Name attribute can also identify an element
partialLinkText – Part of the text in the link can also identify an element
tagName – We can also use a tag to locate elements
xpath – Xpath is the language used to query the XML document. The same can uniquely
identify the web element on any page.
Select
selectByIndex - This method selects the dropdown option by its index number.
Command - selectByIndex(int arg0)
selectByValue - This method selects the dropdown option by its value.
Command - selectByValue(String arg0)
selectByVisibleText - This method enables one to select one option from the dropdown or
multi-select dropdown based on the dropdown text.
Command - selectByVisibleText(String arg0)
How do we identify parent window and child windows?
When a user hits a URL, a webpage opens. This main page is the parent window (i.e) the
main window on which the user has currently landed and will perform any operation.
All the windows which will open inside your main window will be termed as child windows.
Window Handles
[Link](): This method helps to get the window handle of the current window
Command - String mainwindow = [Link]()
[Link](): This method helps to get the handles of all the windows opened
Command - Set<String> s = [Link]()
Iterator: Here we will iterate through all child windows.
Command - Iterator<String> i = [Link]()
set: This method helps to set the window handles in the form of a string.
Command - set<string> set= [Link]()
switch to
This method helps to switch between the windows
Command - [Link]().window(ChildWindow)
Example from demoqa project : String parent = [Link]();
Set<String> s = [Link]();
// Now iterate using Iterator
Iterator<String> I1 = [Link]();
while ([Link]()) {
String child_window = [Link]();
if () {
[Link]().window(child_window);
[Link]([Link], driver);
if
([Link]().equalsIgnoreCase("This is a sample page"))
{
[Link]("new window is passed");
[Link]([Link], "new window is
passed", [Link]([Link](driver)));
} else {
[Link]("new window is fail");
[Link]([Link], "new window fail",
[Link]([Link](driver)));
}
[Link]();
}
}
// switch to the parent window
[Link]().window(parent);
switch to default content
We can switch back from a frame to default in Selenium webdriver
Initially, the webdriver control remains on the main web page.
Command - [Link]().defaultContent();
Action
This method helps to perform certain actions on the windows
Command - Actions action = new Actions(driver);
[Link](element).click().perform();
Example : [Link]([Link]).perform();
Drag and Drop
This method performs left click, hold the click to hold the source element, moves to the
location of the target element and then releases the mouse click.
Command - [Link](source,target).perform();
Example : [Link]([Link](0),
[Link](2)).perform();
DragAndDropBy
This method clicks & holds the source element and moves by a given offset, then releases the
mouse. Offsets are defined by x & y.
Command - [Link](source, xOffset, yOffset).perform();
Example : [Link]([Link](0), 100,
100).perform();
Release
Releases the depressed left mouse button at the current mouse location
Command - [Link]()
Parameters
Parameters pass the values in the runtime.
Command - @Parameters ({"a", "b"})
Example : @Parameters({ "Browser" })
Robot
Robot Class methods can be used to interact with keyboard/mouse events while doing
browser automation.
Command - Robot robot = new Robot(driver);
To press down arrow key of Keyboard [Link](KeyEvent.VK_DOWN)
[Link] (KeyEvent.VK_DOWN)
To press TAB key of keyboard [Link](KeyEvent.VK_TAB)
[Link](KeyEvent.VK_TAB)
To press Enter key [Link](KeyEvent.VK_ENTER)
robot. keyRelease(KeyEvent.VK_ENTER)
Screenshot
A Screenshot in Selenium Webdriver is used for bug analysis. Selenium webdriver can
automatically take screenshots during the execution.
Example : [Link]([Link], "alert is passed",
[Link]([Link](driver)))
Alert/popup
Alerts are small popup boxes/windows which display the messages/notifications and notify
the user with some information seeking some permission on certain kinds of operations.
Command - [Link]( ).alert( );
clicks on the 'OK' button - [Link]( ).alert( ).accept();
Example : Alert alert1 = [Link]().alert();
[Link]();
clicks on the 'Cancel' button - [Link]( ).alert( ).dismiss();
Example : Alert alert3 = [Link]().alert();
[Link]();
captures the message - [Link]().alert().getText();
sends data - [Link]().alert().sendKeys("Text");
Example : Alert alert4 = [Link]().alert();
[Link]("testing");
[Link]();
Inspect
Ctrl+Shift+i – Another way to Inspect in Browser
In case right click inspect is disable in any web page means use the right click inspect
enable web page in browser on the top remove the url link and type the inspect disable web
page name in the text box ,then web page will open also right click inspect also enabled.
Use of href - We can get an attribute value from a href link in Selenium. To begin with, we
have to first identify the element having an anchor tag with the help of any of the locators like
css, id, class, and so on. Next, we shall use the getAttribute method and pass href as a
parameter to the method.
Two types of Xpath
[Link] Xpath(1% use)
Absolute XPath is the direct way to find the element. It begins with the single forward
slash(/), which means you can select the element from the root node.
[Link] Xpath (99% we can use)
The path starts from the middle of the HTML DOM structure. It begins with the double
forward slash (//), which means it can search the element anywhere on the webpage.
Syntax of XPath
XPath = //tag_name[@Attribute_name = “Value of attribute”]
Example : //input[@placeholder='First Name']
id="submit"
Xpath Contains() Syntax
We can use it if part of the value of any attribute changes dynamically.
//tag_name[contains(@attribute,' Partial value of attribute')]
Example : //h5[contains(.,'Alerts, Frame & Windows')]
XPath Starts-with() Syntax
Finds the element which has attribute value starting with the specific given character or
character sequence.
//tag_name[starts-with(@attribute,'Starting value of Attribute value')]
Example : //div[starts-with(.,'Book Store Application')]
Xpath text() Syntax
This function uses the text of the web element for locating the element on the webpage.
//tag_name[text()='text of the web element']
Xpath Following() Syntax
This axis locates all nodes that come after the current node.
//tag_name[@attribute='value of the attribute']//following::tagname of current node
Xpath Following-sibling() Syntax
Following-sibling recognizes only sibling nodes, i.e., the nodes on the same level of the
current node.
//tag_name[@attribute='value of the attribute']//following-sibling::tagname of current node
Xpath Ancestor() Syntax
This axis locates the ancestors of the current node, which includes the parents up to the root
node.
//tag_name[@attribute='value']//ancestor::tagname
Xpath child() Syntax
This axis locates the children of the current node
//tag_name[@attribute='value']//child::tagname
Xpath preceding() Syntax
This axis locates all nodes that come before the current node.
//tag_name[@attribute='value']//preceding::tagname
AND & OR operators Syntax
The "and " operator is used to combining two different conditions or attributes to identify
any element from a webpage
//tag_name[@attribute =’text’ and contains(.,’value of attribute’)]
Example : //span[@class='text' and contains(.,'Practice Form')]
CSS(Cascading Style Sheets) using in Xpath
Performance is the same or faster compared to XPath. Easier to learn than XPath, easier to
use. CSS Selector only allows unidirectional flow. Using a CSS Selector, we can only
traverse from parent to child but not from the child to parent, which is possible with XPath.
(or)
CSS is used to create style rules for webpages and can be used to identify any web element.
Example: @Findby(css = "#demo-tabpane-list .[Link]-group-
item-action")
public List<WebElement> Sortablelist;