Selenium Notes - Weekend Batch - 10 To 2 PM - BTM
Selenium Notes - Weekend Batch - 10 To 2 PM - BTM
Selenium Notes - Weekend Batch - 10 To 2 PM - BTM
Selenium Notes
1. What is Selenium ?
Limitation of Selenium :
● It doesn’t support windows based applica on directly. Hsubmitowever, third party tool
(eg: AutoIt) can be integrated with selenium to automate windows based applica ons.
Note :
Note :
All the selenium related resources and documents can be found on the below website.
h p://www.seleniumhq.org
-------------------------------------------------------------------------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 1/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● Php
● Javascript
● Javascript (Node JS)
● Haskell
● R
● Dart
● TCL
● Objec ve - C
6. It supports almost all the browsers.(Firefox, Chrome, Internet Explorer etc) and
hence, cross browser tes ng/compa bility tes ng can be performed using selenium.
7. It supports almost all the Opera ng System (MAC, Windows, LINUX etc) and hence,
--------------------------------------------------------------------------------------------------------
● Selenium Core (Developed by a company called Thought Works way back in 2004)
● Selenium IDE (supports only Mozilla Firefox - supports record and playback feature)
● Selenium RC (Remote Control - Version is 1.x) (Used for parallel execu on of automa on
scripts on mul ple remote systems)
● Selenium WebDriver (Version is 2.x and 3.x)
Note :
Selenium WebDriver version 3.x is no longer capable of running Selenium RC directly, rather it
does through emula on and via an interface called WebDriverBackedSelenium.
Selenium Grid :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 2/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
4. Driver Executables
5. Browsers:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 3/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Note : To stop auto update of firefox browser version, Make sure to disconnect the
internet connec on and then install 54.0 version, now go to Se ng/Op on in firefox
browser and check the below checkbox - Never check for updates.
https://www.actitime.com/download.php
--------------------------------------------------------------------------------------------------------
OR
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 4/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. Since selenium supports multiple languages such as Java, Python, C# etc, we can
develop automation scripts in all the supported languages. This is know as language
binding or Client binding.
2. When we execute the selenium code, request goes to the Selenium Standalone Server
(also known as Selenium WebDriver Server), which further process the request based
on the input received from the client binding and perform specific actions on the
respective browsers using the browser specific driver executables,
3. Driver executables uses a protocol called JSON Wire protocol to communicate with
related
--------------------------------------------------------------------------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 5/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
NOTE :
All the above men oned interfaces and classes are present in a package called
“org.openqa.selenium”.
To view any information about Selenium interfaces, classes and methods, navigate to the
below page.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 6/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://github.com/SeleniumHQ/selenium/tree/master/java/client/src/org/openqa/seleniu
m
-------------------------------------------------------------------------------------------
8. List down all the methods present in below interfaces of Selenium WebDriver.
1. findElement()
2. findElements()
1. close()
2. get()
3. getTitle()
4. getPageSource()
5. getCurrentUrl()
6. getWindowHandle()
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 7/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
7. getWindowHandles()
8. manage()
9. navigate()
10. quit()
11. switchTo()
1. getScreenshotAs(args)
1. executeScript()
2. executeAsyncScript() - we don’t use this for automation
1. clear()
clear() --
2. It is used to clear any value which is present in any element (eg: text box,text area etc)
2. click()
3. getAttribute()
4. getCssValue()
5. getLocation()
getLocation() method :
3. Point class has few non static methods like getX() and getY().
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 8/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
6. getRect()
7. getSize()
3. Dimension class has few non static methods like getHeight() and getWidth().
8. getTagName()
9. getText()
10. isDisplayed()
isDisplayed() -
11. isEnabled()
isEnabled() -
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 9/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
12. isSelected()
isSelected() -
13. sendKeys()
sendKeys() --
2. It is used to enter any value in an element (eg: text box,text area etc)
14. submit()
We can use submit method to click on an element if and only if the html source code of the
same element has an attribute called type = “submit”
9. Why we upcast the browser related child class to WebDriver, and not RemoteWebDriver
class (RemoteWebDriver being the super most class in selenium) ?
Upcasting Example :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 10/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● This is as per the Selenium coding standard set by the Selenium community. As a
tes monial, navigate to the below selenium community site and check for the text as
men oned in the image below.
Url - h p://www.seleniumhq.org/projects/webdriver/
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
1. Launch eclipse and go to package explorer [naviga on path :- Window menu → Show
View → Package Explorer]
2. Create a java project [File → New→ Java Project]
3. Right click on Java Project and add a new folder with name “driver” [File → New→
Folder]
4. copy geckodriver.exe file from your system and paste it into this driver folder
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 11/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
5. Similarly, create another folder with name “jar”and copy Selenium Standalone
Server.jar file into this jar folder.
6. Expand the jar folder and right click on Selenium Standalone Server.jar file → select
Build Path → select Add to Build Path
7. As soon as you add any .jar files to build path, a new folder will be available called
“Reference Libraries” under the package explorer sec on and you can see the .jar file is
added to this “Reference Libraries”
8. To remove the .jar file from the java build path, go to the Reference Libraries → select
the .jar file → right click → select build path → Remove from build path.
9. Other way of adding .jar file to java build path is : right click on the project → build path
→ configure build path → Libraries tab → Add External jars → select the .jar file → Apply
→ ok
Interview Question:
Selenium is an open source web automa on tool that comes in the form of .jar file, we
download this and a ach it to the build path.
Selenium in order to communicate with browser it needs some extra file known as driver
executables, we download this and set the path using System.setProperty(key, value)
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 12/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Line of code is ,
System.setProperty("webdriver.gecko.driver","path of geckodriver.exe")
System.setProperty("webdriver.chrome.driver","path of chromedriver.exe")
System.setProperty("webdriver.ie.driver","path of IEDriverServer.exe")
When we try to communicate with the browser without using the relevant driver executables,
we get this excep on.
12. This program demonstrates Upcasting concept (FirefoxDriver class object to WebDriver
interface) and accessing various methods of WebDriver interface
package qspiders;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 13/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("http://www.google.com");
//Get the title of the google page and print it on the console
//Get the URL of the google page and print it on the console
//Get the source code of the google page and print it on the console
Thread.sleep(2000);
driver.close();
7th Oct
******************************************************************************
***************************
Capturing Screenshot
******************************************************************************
***************************
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 14/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. Create an object of specific browser related class (eg : FirefoxDriver) and then
upcast it to WebDriver object (eg : driver)
2. Typecast the same upcasted driver object to TakesScreenshot interface type.
3. Using the typecasted object, we call getScreenshotAs(OutputType.FILE) which in
turn returns the source file object.
4. Using the File IO operations (i.e FileUtils class), we store the screenshots to desired
location in the project.
package testpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
static {
System.setProperty("webdriver.gecko.driver",
"./drivers/geckodriver.exe");
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
System.setProperty("webdriver.ie.driver",
"./drivers/IEDriverServer.exe");
}
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 15/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
package testpackage;
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.google.common.io.Files;
driver.get("https://www.amazon.com");
Files.copy(srcFile, destFile);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 16/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.close();
}
}
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 17/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
from the temporary location(srcFile) and paste in the desired location(destFile). This is how
we take the screenshot of a page.
Selenium Code :
package pack1;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 18/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
//getting the source file using getScreenshotAs() method and storing in a file
File srcFile = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, destFile);
Question : Why capturing screenshot of the web pages is important in the project ?
Answer :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 19/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● Whenever an automation script is failed, we first manually execute the steps to check
whether there is any issue in the application or the issue is with the script.
● If the script fails due to an issue in the script itself, we fix the script and re-run it till it
is passed.
● If there is an issue in the application due to which the script is failed, then we log
defect against the same issue. In this way, automation team gets credibility in the
project.
******************************************************************************
***************************
******************************************************************************
***************************
Ans:
1. By using Navigation interface.
2. Navigation interface has few non static methods, to access these methods,
we need to create an object of Navigation interface.
3. But, we cant create an object of Navigation interface, so there is a
method called navigate() from WebDriver interface, which returns an
instance of Navigation interface.
4. Using this reference/instance, we can call methods like
to() --> which is used to navigate to any website.
back() --> it is used to navigate to the immediate previous page.
forward() -> it is used to navigate to the immediate next page.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 20/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.navigate().to(“https://www.google.com”);
in this line of code method chaining is happening between navigate() method and to()
method.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 21/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
to() - it is a non-static method present in Navigation interface and it does not return anything.
It accept one argument of string type, where we pass the url of the page in which we want to
navigate.
it doesn’t wait for the page to be completely loaded, we have to explicitly delay the
execution by thread.sleep(2000).
******************************************************************************
***************************
******************************************************************************
***************************
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 22/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Ans:
2. Robot class has few non static methods, to access these methods,
keyPress() --> which is used to press any key from the keyboard.
All these keys are present in one class called KeyEvent, which has static
***************
package test;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 23/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.navigate().to("http://localhost:8080/login.do");
Thread.sleep(5000);
r.mouseMove(300, 500);
r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_F);
r.keyRelease(KeyEvent.VK_F);
r.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(3000);
r.keyPress(KeyEvent.VK_W);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 24/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
r.keyRelease(KeyEvent.VK_W);
Thread.sleep(3000);
//driver.close();
driver.quit();
}}
******************************************************************************
***************************
******************************************************************************
***************************
What is an WebElement ?
3. For testing purpose, we can also create web page using HTML code.
4. In order to create a web page, we need to write HTML code in any text pad (eg : notepad
and save the file with .html extension)
<html>
<body>
</body>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 25/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
</html>
In the above HTML tree, every element should have one of the 3 options.
● html,
● body,
● input,
● a
● type = "text"
● id = “username”
● value = "admin"
● Locators are used to identify the web elements on the web page.
● We have 8 types of locators in Selenium using which findElement() methods identifies
elements on the web page:
1. id
2. name
3. tagName
4. className
5. linkText
6. partialLinkText
7. xpath
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 26/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
8. cssSelector
● findElement() method returns the address of the web element on the web page and
the return type is WebElement.
● If the specified locators matches with multiple elements, then findElement() method
returns the address of the first matching element.
● If the specified locators matches no element, then findElement() method throws an
exception called "NoSuchElementException".
Note :
1. driver.findElement(By.id(""));
2. driver.findElement(By.name(""));
3. driver.findElement(By.tagName(""));
4. driver.findElement(By.class(""))
5. driver.findElement(By.linkText(""))
6. driver.findElement(By.partialLinkText(""))
7. driver.findElement(By.xpath(""))
8. driver.findElement(By.cssSelector(""))
1. (By is an abstract class and all the locators specified/highlighted above are static
methods of By class and hence, we call directly by using <classname.staticConcrete> methods
------
Ans :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 27/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
3. findElement() starts traversing in the html tree right from the root
node and the moment, it finds the first matching elements, it returns the address of the
same element.
-------------------
Ans:
Multiple elements on the webpage can have the same tag or can belong to
the same class, and findElement() method always returns the address
on any other elements and not on the first matching element. In this case,
our purpose may not be served always and hence , we don’t use either of them.
------------------
Below is the code to demonstrate the usage of locators in selenium while identifying the web
elements on the web page:
package pack1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 28/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("file:///C:/Users/admin/Desktop/UN.html");
unTB.clear();
unTB.sendKeys("ajit.biswas@gmail.com");
passTB.clear();
Thread.sleep(2000);
passTB.sendKeys("Qspiders123");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 29/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Thread.sleep(2000);
}}
● Out of all the locators, linkText and PartialLinkText are used to identify only the links
present on the webpage.(elements whose tagname is “a” -- a stands for anchor)
● LinkText locator should be used when the text of the link is constant
● PartialLinkText locator should be used when certain part of the link text is getting
changed everytime the page is loaded. i.e for partially dynamically changing text, we
use partialLinkText locator
● To handle those elements whose text changes completely, we can’t use
partialLInkText locator. It should be handled by another locator called “xpath”
● If we use try to use these 2 locators on other type of elements (except Links), then we
get “NoSuchElementException”
----------------------
Ans:
1. linkText and partialLinkText are the locators using which findElement method identifies
objects on webpage.
4 when the text of the link partially changes, then we use partialLinkText
locator.
-----------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 30/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. We need to install firebug and firepath addons in firefox browser to write cssSelector
expression and then evaluate whether the expression is correct or not.
2. To install firebug addon in firefox browser :
TOOLS -- > ADD-ONS → EXTENSIONS → search firebug -- > and click on Install
-- Restart the browser.
TOOLS -- > ADD-ONS → EXTENSIONS → search firepath -- > and click on Install
-- Restart the browser.
1. Navigate to the web page -- > right click anywhere on the web page → select inspect
element with firebug or Press F12 from keyboard.
2. Go to firepath tab and select CSS option.
3. Type the cssSelector expression and hit Enter key from the keyboard, it will highlight
the corresponding matching element on the web page.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 31/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
2. Navigate to the web page -- > right click anywhere on the web page → Press F12 from
keyboard or select inspect element, it will open the Developer tool section with
Elements tab selected by default.
3. Press Ctrl+F and write the cssSelector expression, it will highlight the source code of
the matching element.
4. Place the cursor on the highlighted source code, it will highlight the corresponding
element present on the web page.
cssSelector locator :
1. It is one of the locator in Selenium using which we identify web elements on the web
page.
2. It stands for Cascading Style Sheet.
3. The standard syntax for cssSelector expression is
tagName[attributeName = ‘attributeValue’]
OR
[attributeName = “attributeValue”]
Following are the 4 different ways of writing cssSelector expression for above mentioned
Login button :
driver.findElement(By.cssSelector(“input[type=’textbox’]”))
------------------------------------------------------------------------------
driver.findElement(By.cssSelector(“input[id=’ID123’]”))
----------------------------------------------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 32/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.findElement(By.cssSelector(“input[class=’inputText’]”))
------------------------------------------------------------------------------------------
driver.findElement(By.cssSelector(“input[value=’Login’]”))
---------------------------------------------------------------------------------------------
Important Note :
While deriving cssSelector expression, we can use either one attribute or multiple attributes
till we found unique matching element on the web page.
eg : input[type=’textbox’][id=’ID123’][class=’inputText’][value=’Login’]
4. CssSelector can also be written using ID and Class. Here, ID is represented by “ # ” and
className is represented by dot operator( . )
4.1 CssSelector expression for the above Login button can be written using ID as
OR
driver.findElement(By.cssSelector(“#ID123”))
4.2 CssSelector expression for the above Login button can be written using ID as
shown below
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 33/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
OR
driver.findElement(By.cssSelector(“.inputText”))
Limitation of cssSelector :
1. It does not support text i.e we can identify element using text of the element.
2. It does not support backward traversing.
3. It doesn’t support index
XPATH :
1. xpath is one of the locator in selenium using which we identify objects or elements on
the web page and perform specific actions to carry out automation testing.
2. xpath is the path of an element in the html tree.
3. xpath are of 2 types.
Absolute xpath :
1. It refers to the path of the element right from the root node to the destination
element.
2. While writing the absolute xpath, We use single forward slash (/) to traverse through
each immediate child element in the html tree.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 34/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
document
|_____html
---- body
------> a
html/body/a
or
./html/body/a
(Note :- here, dot (.) refers to the current document or the current web page, using dot here is
optional)
driver.findElement(By.xpath(“html/body/a”)).click();
5. In xpath, if there are multiple siblings with same tagname, then the index starts from 1.
6.In case of multiple siblings with same tagname, if we don’t use index, then it considers ALL
the siblings.
We use this operator to identify more than one element in different nodes of html tree. find
element method after finding 1st matching element it stores the address of that element in
some reference variable and start traversing from the root node to identify other matching
elements. Once after identifying all the elements it consolidated all the address of matching
elements and returns it.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 35/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Considering the below sample html tree, write Absolute xpath and Relative xpath
expressions.
Fill in the table with Absolute xpath expressions using the sample html tree given above.
html/body/table/tr[1]/td/a[1] A
html/body/table/tr[1]/td/a[2] B
html/body/table/tr[2]/td/a[1] C
html/body/table/tr[2]/td/a[2] D
html/body/table/tr[1] AB
html/body/table/tr[2] CD
html/body/table/tr[1]/td[1]|html/body/table/tr[2]/td[1] AC
html/body/table/tr[1]/td[1]|html/body/table/tr[2]/td[2] BD
html/body/table/tr[1]/td[1]|html/body/table/tr[2]/td[2] AD
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 36/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
html/body/table/tr[1]/td[2]|html/body/table/tr[2]/td[1] BC
html/body/table/tr[1]|html/body/table/tr[2]/td[1] ABC
html/body/table/tr[1]|html/body/table/tr[2]/td[2] ABD
html/body/table/tr[1]|html/body/table/tr[2] ABCD
Relative xpath :
1. In Absolute xpath, we write the path of the element right from the root node and
hence, the expression for absolute xpath is lengthy.
2. In order to reduce the length of the expression, we go for Relative xpath.
3. In Relative xpath, we use double forward slash ( // ), which represents any
descendant.
Fill in the table with relative xpath expressions using the sample html tree given above.
//tr[1]//td[1] A
//tr[1]//td[2] B
//tr[2]//td[1] C
//tr[2]//td[2] D
//tr[1]//td AB
//tr[2]//td CD
//td[1] AC
//td[2] BD
//tr[1]//td[1] | //tr[2]//td[2] AD
//tr[1]//td[2] | //tr[2]//td[1] BC
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 37/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Interview questions :
Answer : “/” refers to the immediate child element in the html tree.
“//” refers to any element in the html tree. It also represent any descendant.
3. Derive an xpath which matches all the links present on a web page ?
Ans : //a
4. Derive an xpath which matches all the image present on a web page ?
Ans : //img
5. Derive an xpath which matches all the links and images present on a web page ?
6. Derive an xpath which matches all the 2nd links present on a web page ?
//a[2]
7. Derive an xpath which matches all the links present inside a table present on a web page ?
//table//a
//table//a → refers to all the links present within all the tables present on the webpage.
xpath by Attribute :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 38/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
2. Based on the situation, we would use either single attribute or multiple attributes to
find an element on a web page.
3. Using single attribute in xpath expression, if it returns one matching element, then we
would use single attribute only.
4. In case, by using single attribute in xpath expression, if it returns multiple matching
elements on the web page, then we would go for using multiple attributes in the
xpath expression till we get one matching element.
url : https://demo.actitime.com/login.do
Write xpath for below few elements on above actiTIME login page :
xpath Syntax :
● //tagName[@AN1=’AV1’][ @AN2=’AV2’]
● //tagName[@AN1=’AV1’] | //tagName[@AN2=’AV2’]
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 39/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.findElement(By.xpath(“//a[@href='javascript:void(0)']
[@>
Assignment :
Write xpath expression for below 7 elements present on actiTIME login page
Elements :
1. UserName
2. Password
3. Login Button
4. Check box
5. Actitime Image
6. View Licence link
7. actiTIME Inc link
Use the below format (Sample example for actiTIME Inc Link):
xpath syntax
//tagname[@AN1 = 'AV1']
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 40/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
//a[@target = '_blank']
1. In the html code of an element, if a ribute is duplicate or a ribute itself is not present,
then use text() func on to iden fy the element.
2. In order to use text() func on, the element should have text in the element’s html code.
Syntax :
OR
Note : Instead of text(), we can use dot (.) , the problem here with using dot (.) is
some mes,
it returns the hidden element also present on the webpage. which might confuse
the
xpath expression using text() func on for below elements present on ac TIME login page.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 41/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. In the html code of an element, if the a ribute value or the text is changing par ally,
then use contains() func on to iden fy the element.
2. In order to use contains() func on, the element should have either a ribute value or
text value.
Syntax :
xpath expression using contains() function for below elements present on actiTIME login
page.
3. We use contains() func on when the text value is very lengthy or the a ribute value is very
lengthy.
eg: xpath to iden fy error message present on ac me login page ( Click on login bu on
without entering username and password to get the error message)
//span[contains(text(),'invalid')]
Program to illustrate xpath by attributes, text() function, contains() function and their usages
with attributes and text values.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 42/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
Thread.sleep(2000);
driver.findElement(By.xpath(xp)).sendKeys("admin");
Thread.sleep(2000);
driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys("manager");
Thread.sleep(2000);
//find an image on the web page whose attributes (src)contains a value called
timer
WebElement clock =
driver.findElement(By.xpath("//img[contains(@src,' mer')]"));
//store the width value of the clock image into a variable called widthValue
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 43/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.findElement(By.xpath("//div[text()='Login ']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//a[@id='loginBu on']//div[contains(text(),'Login')
]")).click();
Thread.sleep(2000);
driver.close();
1. We use starts-with() func on to iden fy those elements whose text value starts with
some specified value.
1. When the text value of the elements is completely changing, then we can’t use func ons
like “contains()”, “starts-with()” etc to handle those elements.
2. In such cases, we iden fy the dynamically changing element using the nearby unique
element. We call this concept as independent dependent xpath.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 44/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. Iden fy the independent element on the webpage and inspect the element to view the
source code and then derive the xpath expression.
2. Place your cursor on the independent element source code and move the mouse pointer
upward ll it highlights both the independent and dependent elements which is the
common parent element.
Add /.. to the xpath of independent element already noted down in step 1 to
3. Use mouse pointer to navigate from common parent to the desired dependent
4. Write the xpath from Independent element to Common parent and then write
Example 1:
Write xpath to iden fy version of Java Language present in Selenium Download page.
//td[.='Java']/../td[2]
Example 2:
Write xpath to iden fy Release data of Java Language present in Selenium Download
page.
//td[.='Java']/../td[3]
Example 3:
page.
//td[.='Java']/../td[4]/a
Note :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 45/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● In case, if the column number of Download link changes, then the above xpath
will fail to iden fy the link as we are hard coding the column posi on as 4 in the
above case.
In order to handle this, we will write xpath in such a way that it works irrespec ve of the
column posi on as shown below.
//td[.='Java']/..//a[.='Download']
Program 1 :
Scenario :
Url : h p://www.seleniumhq.org/download
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://www.seleniumhq.org/download/");
Thread.sleep(3000);
driver.findElement(By.xpath("//td[.='Java']/..//a[.='Download']")).click();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 46/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Group Index :
Ans :
Retrieving one element from a group of matching elements by using index is called
group index.
When the xpath expression matches with multiple elements, then we go for group
index.
syntax :
(xpath expression)[index]
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 47/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
xpaths using Group Index to identify the elements in the above sample tree:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 48/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. In Group index, we write xpath expression within the braces and then we write
the index outside the braces.
2. Internally, it executes the xpath expression first and stores the result in an xpath
array whose index starts with 1
3. last() is a func on that is used to retrieve the last element present in the xpath
array.
Program 2 :
Click on the Set by default link of testing present in type of work (Setting tab) of
ac TIME applica on
Scenario :
3. Login in to ac me applica on
Url : h p://localhost:8080/login.do
4. click on Se ngs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 49/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
//click on login bu on
driver.findElement(By.xpath("//div[.='Login ']")).click();
Thread.sleep(4000);
Thread.sleep(2000);
driver.findElement(By.xpath("//a[.='Types of Work']")).click();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 50/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Thread.sleep(4000);
driver.close();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 51/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 52/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Xpath Axes :
-----------------------------------------
Axes are something using which xpath traverses in the html tree either in upward or downward
direc on.
---------------------------
1. child (/)
a. Using this axes, xpath traverses to the immediate child element in the html tree.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 53/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
----------------------------
2. descendant (//)
a. Using this axes, xpath traverses to any child element in the html tree.
---------------------------------
a. Using this axes, xpath traverses to the immediate parent element in the html tree.
----------------------------------
4. ancestor axes
a. Using this axes, xpath traverses to any parent element in the html tree.
------------------------------
5. preceding-sibling
using this axes, xpath traverses with in the siblings in upward direc on.
-----------------------------
6. following-sibling
using this axes, xpath traverses with in the siblings in downward direc on.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 54/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
---------------------------------------------------------------------
Following are the syntax to use all the xpath axes in Selenium.
child Axes:
descendant Axes:
parent Axes:
ancestor Axes:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 55/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
preceding-sibling Axes:
following-sibling Axes:
Following table illustrates a detailed level understanding of all the xpath axes :
CssSelector Xpath
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 56/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
It is faster It is slower
Imp Note :
-------------
Cssselector is faster than xpath because cssselector traverse in the html tree only in the
downward direction,
------------
1. id
2. name
3. linkText
4. cssSelector
5. xpath
----------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 57/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Ans : When the html source code have an id attribute, then we can use id locator. And what
we pass as an argument to ID locator is the value of ID attribute.
Ans : When the html source code have name attribute, then we can use name locator. And
what we pass as an argument to Namelocator is the value of Name attribute.
Ans : We prefer ID over name locator, because ID is unique and Name can be duplicated.
Ans : By using xpath with contains function when the attribute value or the text partially
changes.
----------------
syntax :
using attribute :
----------------------------
-----------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 58/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
----------------------------------
position() -
the condition is true and it fetches all the matching elements present at
---------------------
starts-with() :
attribute value or the text is present at the beginning of the given attribute value or text of an
element.
----------------------
ends-with function :
it checks whether the specified part of either the attribute value or the text is present at the
end of the given attribute value or text of an element.
Note :
this function doesn’t work with 3.x series of selenium. It has been deprecated in 3.x series.
--------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 59/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
We can use following checkpoints to validate the required page is displayed or not.
Write a program to validate Actitime application home page using TITLE of the page
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//div[.='Login ']")).click();
Thread.sleep(3000);
//If actual tle contains "Enter Time" text then home page is displayed.
if (actualTitle.contains(expectedTitle)) {
} else{
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 60/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a program to validate Actitime application home page using Current URL of the page
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//div[.='Login ']")).click();
Thread.sleep(3000);
if (actualUrl.contains(expectedUrl)) {
} else{
Write a program to validate Actitime application home page using any UNIQUE element on the
page
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 61/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("h p://localhost:8080/login.do");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//div[.='Login ']")).click();
Thread.sleep(3000);
if (logoutBtn.isDisplayed()) {
} else{
---------------------------------
------------------------------------------
1. getLoca on():
--> getX() method returns the xco-ordinate of the given element in the form
--> getY() method returns the yco-ordinate of the given element in the form
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 62/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
---------------------------------------------
2. getSize():
--> getHeight() method returns the height of the given element in the form
--> getWidth() method returns the width of the given element in the form
---------------------------------------------
3. isDisplayed()
---> if the element is displayed, it returns true, and if it is not displayed, it returns
---------------------------------------------
4. isEnabled()
--------------------------------------------
5. sendKeys()
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 63/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
----------------------------------------------
6. clear()
----------------------------------------------
7. isSelected()
---------------------------------------------
8. getA ribute(String)
-- > if the specified a ribute name is found in the html source code of an
--> In case, if the specified a ribute name is not found, it returns an empty string
---------------------------------------------
9. getText()
--> is a method of webelement interface which returns the text of an element in the form of
string
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 64/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
---------------------------------------------
10. getTagname()
--> is a method of webelement interface which returns the tagname of an element in the
---------------------------------------------
11. click()
-------------------------------------------
11. submit()
eg:
<form>
<input type="submit">
</form>
--------------------------------------------
12. getCssValue()
--> it returns the value of the specified style related a ribute in the form of string
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 65/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
---------------------------------------------
13. getRect() --
--> it is used to get the xco-ordinate of elements and also to find the height and width of an
element.
------------------------------------
Write a program to validate Username and Password fields on Actitime login page are aligned
or not ?
driver.get("h p://localhost:8080/login.do");
} else {
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 66/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Assignment :
Write a program to validate Username and Password fields on Facebook login page are aligned
or not ?
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h ps://www.facebook.com/");
System.out.println(username_Ycordinate);
System.out.println(password_Ycordinate);
//check whether the Y-coordinate of username and password field are same
if (username_Ycordinate==password_Ycordinate) {
}else{
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 67/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a program to validate the height and width of Username and Password fields on
Facebook login page are same or not ?
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
System.out.println(username_height);
System.out.println(username_width);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 68/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.out.println(password_height);
System.out.println(password_width);
//check the height and width of username and password fields are same
}else{
Write a script to validate that the username field on Facebook login page is smaller than the
Mobile Number field ?
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h ps://www.facebook.com/");
System.out.println(username_width);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 69/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
WebElement mobileNumTB =
driver.findElement(By.xpath("//input[contains(@aria-label,'Mobile number or
email address')]"));
System.out.println(mobNumWidth);
if (username_width==mobNumWidth) {
}else{
Interview Question :
--------------------------------------
1. ActiveElement method returns the address of the active element on the webpage.
3. In case, there is no active element on the page, and we try to switch to an active element,
we get this exception - NoSuchElementException.
-----------------------------------------
Write a script to enter a text into the focussed element (eg : textbox).
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 70/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.gecko.driver",
".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
driver.switchTo().ac veElement().sendKeys("admin");
How do you remove value present in username text box of Actitime application ?
Selenium code :
driver.get("h p://localhost:8080/login.do");
driver.findElement(By.id("username")).sendKeys("ajit");
Thread.sleep(2000);
driver.findElement(By.id("username")).clear();
Thread.sleep(2000);
driver.findElement(By.id("username")).sendKeys("againEnteredAjit");
Thread.sleep(2000);
driver.findElement(By.id("username")).sendKeys(Keys.CONTROL+"a"+Keys.DELET
E); // this line will actually delete the value if there is no space in the text entered
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 71/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
// if there is a space between two words in the username field, we have to use
the below lines of code
driver.findElement(By.id("username")).sendKeys(Keys.CONTROL+"a") ;
driver.findElement(By.id("username")).sendKeys(Keys.DELETE);
Thread.sleep(2000);
How do you remove value present in username text box of Actitime application without using
clear() method ?
driver.get("h p://localhost:8080/login.do");
driver.findElement(By.id("username")).sendKeys("ajit");
Thread.sleep(2000);
driver.findElement(By.id("username")).clear();
Thread.sleep(2000);
driver.findElement(By.id("username")).sendKeys("againEnteredAjit");
Thread.sleep(2000);
driver.findElement(By.id("username")).sendKeys(Keys.CONTROL+"a"+Keys.DEL
ETE);
Thread.sleep(2000);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 72/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a script to print the tooltip text of the checkbox present on the login page of Actitime
application ?
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://localhost:8080/login.do");
//get the tooltip text using getAttribute() method and store in a variable
System.out.println(tool pText);
driver.close();
}}
Write a script to check “Keep me Logged in” checkbox on the login page of Actitime application
is selected or not ?
driver.get("h p://localhost:8080/login.do");
WebElement KeepMeLogIN_Checkbox =
driver.findElement(By.name("remember"));
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 73/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
KeepMeLogIN_Checkbox.click();
not : if it is already selected, it return true and if not selected, then it returns
false/
if (KeepMeLogIN_Checkbox.isSelected()) {
System.out.println("Checkbox is selected");
}else{
Write a script to check “Username” textbox on the login page of Actitime application is
enabled or not ?
driver.get("h p://localhost:8080/login.do");
WebElement UN = driver.findElement(By.id("username"));
if (UN.isEnabled()) {
}else {
driver.close();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 74/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a script to print the version of actitime on login page of Actitime application
driver.get("h p://localhost:8080/login.do");
Write a script to verify that View License link on login page of Actitime application is a link or
not ?
driver.get("h p://localhost:8080/login.do");
if (tagName.equals("a")) {
} else{
driver.close();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 75/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a script to verify that KeepMeLoggedIn checkbox on login page of Actitime application is
a checkbox or not ?
driver.get("h p://localhost:8080/login.do");
String elementType =
driver.findElement(By.id("keepLoggedInCheckBox")).getA ribute("type");
System.out.println(elementType);
if (elementType.equalsIgnoreCase("checkbox")) {
System.out.println("it is a checkbox");
}else{
}}}
Write a script to demonstrate different options to click on a button or on a link (Or any
element)
1. click()
2. sendkeys()
3. submit()
System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 76/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.findElement(By.xpath(xp)).click();
driver.findElement(By.xpath(xp)).sendKeys(Keys.ENTER);
this method will work only and only if if the element has an attribute called type=
'submit'/
driver.findElement(By.xpath(xp)).submit();
}}
Write a script to verify the color of the error message on Actitime login page when user clicks
on Login button without entering username and password ?
driver.findElement(By.xpath("//span[contains(.,'invalid')]"));
// get the text of the error message
String errtext = errMsg.getText();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 77/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
if(ColorasHex.equals(“#ce0100”)){
}else{
JavascriptExecutor
1. executeScript()
2. executeAsyncScript()
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 78/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Create a sample webpage using the below html source code wherein the second textbox is
disabled as shown below.
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("file:///D:/Ajit/testdata/Day13/DisabledTextBox_prog3.html");
Thread.sleep(2000);
js.executeScript("document.getElementById('t1').value='admin'");
Thread.sleep(2000);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 79/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
js.executeScript("document.getElementById('t2').value=''");
js.executeScript("document.getElementById('t2').value='manager'");
js.executeScript("document.getElementById('t2').type='bu on'");
}}
In Selenium, we don’t have any method to scroll up or down on the webpage, in such case, we
can use JavascriptExecutor.
1. Open the required page in the browser and press F12 from keyboard.
2. Navigate to Console tab, type the javascript statement and press Enter key
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("http://seleniumhq.org/download");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 80/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
js.executeScript("window.scrollBy(0, 1000)");
Thread.sleep(3000);
js.executeScript("window.scrollBy(0, -1000)");
Thread.sleep(3000);
}}}
Write a script to scroll down to a specific element (Applitool webelement ) on Selenium official
website
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://seleniumhq.org/download");
driver.findElement(By.id("close")).click();
WebElement ele =
driver.findElement(By.xpath("//img[contains(@src,'applitools')]"));
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 81/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
js.executeScript("window.scrollBy("+x+", "+y+")");
Thread.sleep(3000);
}}
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("h p://www.seleniumhq.org/download/");
driver.findElement(By.id("close")).click();
js.executeScript("window.scrollBy("+x+","+y+")");
Thread.sleep(3000);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 82/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
element.click();
}}
----------------
Interview Ques on :
Ans: When selenium conven onal methods fails to perform an ac on on the webpage, we go
for javascriptexecutor.
We typecast the driver object to javascriptexecutor interface, once we have the reference, we
call executeScript() method, to which, as an argument, we pass the actual javascript statement.
Right click anywhere on the page, click on inspect , then navigate to console tab.
jse.executeScript("document.getElementById('un123').value='admin'");
******************************************************************************
*******************************************
HANDLING FRAMES
******************************************************************************
*******************************************
What is frame ?
driver.switchTo().frame(arg);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 83/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
frame(index)
frame(id)
frame(name)
frame(WebElement)
6. If the specified frame is not present, we get an excep on called “NoSuchFrameExcep on”
8. Easiest way to verify that an element is present within a frame is to right click on the
element and verify that this frame op on is displayed in the context menu.
Create a sample webpage using the below html source code and save the file as Page1.html
Create another sample webpage using the below html source code and save the file as
Page2.html
The webpage looks like this. Here, t1 is inside the frame and t2 is outside the frame on the
webpage
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 84/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a script to enter a text into an element which is present inside a frame ?
Selenium code:
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("file:///D:/Ajit/Selenium/SeleniumBtm_7thSep17/webpages/Frame_P
age2.html");
//using index of the frame - [ int value] [ index of frames starts with zero]
driver.switchTo().frame(0);
driver.findElement(By.id("t1")).sendKeys("a");
driver.switchTo().defaultContent();
driver.findElement(By.id("t2")).sendKeys("a");
driver.switchTo().frame("f1");
driver.findElement(By.id("t1")).sendKeys("b");
driver.switchTo().defaultContent();
driver.findElement(By.id("t2")).sendKeys("b");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 85/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.switchTo().frame("n1");
driver.findElement(By.id("t1")).sendKeys("c");
driver.switchTo().defaultContent();
driver.findElement(By.id("t2")).sendKeys("c");
WebElement f = driver.findElement(By.className("c1"));
driver.switchTo().frame(f);
driver.findElement(By.id("t1")).sendKeys("d");
driver.switchTo().defaultContent();
driver.findElement(By.id("t2")).sendKeys("d");
driver.close();
--------------------
frame() method returns the frame to which we are switching to, a frame is nothing but an html
page, and in selenium, a page is always referred by driver object, here, driver is an object of
Webdriver and hence the return type of frame() method is Webdriver.
parentFrame() method is used to switch to the immediate parent frame, a frame is nothing but
an html page, and in selenium, a page is always referred by driver object, here, driver is an
object of Webdriver and hence the return type of parentFrame() method is Webdriver.
defaultContent() method is used to switch to the default page or the main page, and in
selenium, a page is always referred by driver object, here, driver is an object of Webdriver and
hence the return type of defaultContent() method is Webdriver.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 86/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
window() method is used to switch to a par cular browser window, a browser window is
nothing but a webpage, and in selenium, a page is always referred by driver object, here, driver
is an object of Webdriver interface and hence the return type of window() method is Webdriver.
----------------------
ACTIONS Class :
→ Ac ons class is used to handle mouse related and keyboard related opera ons.
→ Ac ons class has same non-sta c methods, in order to use these methods, we have to create
an object of the Ac ons class.
→ When we create an object of Ac ons class, we need to pass the driver object as an argument
to the constructor of Ac ons class, so that we can instruct methods of Ac ons class to perform
ac on on a par cular page.
1. moveToElement() :- this method is used to mouse hover on any element on the web
page.
2. contextClick() :- this method is used to right click on any element on the web page
3. dragAndDrop() :- this method is used to drag an element from the source and drop it to
any des na on element.
4. doubleClick() :- this method is used to double click on any element on the web page
5. build() :- this method is used to combine mul ple individual ac ons in to one single
composite ac on.
→ For all the methods of Ac ons class, we need to explicitly call a method called perform(),
un l unless, we call perform() method, none of the Ac ons class methods will perform any
ac on.
We need to specify on which page, methods of Ac ons class to perform specific ac on, and
hence, we pass driver
reference to Ac ons class constructor because driver object will be always poin ng to the
current page.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 87/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
OR
Methods of Ac ons class is used to perform ac on on web elements present on the webpage.
And, On which page, we want methods of Ac ons class to perform specific ac on, we need to
pass the reference of that par cular page and in selenium, reference of any page will be always
stored in driver object.
And this is why we pass driver object as an argument to Ac ons class constructor.
OR
Write a script to right click on “ActiTIME Inc.” link on actitime login page and then open it in
new window ?
Selenium code:
System.setProperty("webdriver.chrome.driver", ".\\driver\\chromedriver.exe");
driver.get("h p://localhost:8080/login.do");
ac ons.contextClick(link).perform();
Thread.sleep(3000);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 88/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
r.keyPress(KeyEvent.VK_W);
r.keyRelease(KeyEvent.VK_W);
driver.quit();
}}
Imp Note :
Whenever we call any method of Actions class, we have to explicitly call perform() method of
Actions class. Otherwise, it will not perform any action on the browser.
Assignment :
Scenario Steps :
1. Login in to gmail
2. Based on the subject of a mail, Right click on the mail
3. Select Archive option
Selenium Code:
System.setProperty("webdriver.chrome.driver", ".\\driver\\chromedriver.exe");
driver.get("h ps://www.gmail.com");
//enter email id
driver.findElement(By.xpath("//input[@type='email']")).sendKeys("enter your
username");
//click on Next bu on
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 89/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.findElement(By.xpath("//span[.='Next']")).click();
Thread.sleep(3000);
//enter password id
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("enter ");
//click on Next bu on
driver.findElement(By.xpath("//span[.='Next']")).click();
Thread.sleep(10000);
//get the address of the mail item which you want to archive
System.out.println(mail.getText());
//using Ac ons class object and contextClick() method, right click on the mail
item
ac ons.contextClick(mail).perform();
Thread.sleep(6000);
}}
Program :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 90/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Scenario Steps :
1. Login in to http://www.actimind.com
2. Mouse hover on “About Company” menu
3. Click on Sub Menu - “Basic Facts”
Selenium Code:
System.setProperty("webdriver.chrome.driver", ".\\driver\\chromedriver.exe");
ac ons.moveToElement(menu).perform();
submenu.click();
}}
Scenario Steps :
4. Login in to http://www.actimind.com
5. Mouse hover on “AREAS OF EXPERTISE” menu
6. Click on Sub Menu - “Cloud Applica onss”
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 91/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code:
WebElement AreaOfExper se =
driver.findElement(By.xpath("//a[contains(text(),'AREAS OF EXPERTISE')]"));
ac on.moveToElement(AreaOfExper se).perform();
ac on.moveToElement(cloudApp).click().perform();
//composite mul ple ac ons can be achieved using the below statement
}}
Program :
Scenario Steps :
1. Login in to http://www.istqb.in
2. mouse hover on Foundation tab
3. mouse hover on Enrollment
4. mouse hover on Corporate Enrollment
5. click on Corporate Enrollment
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 92/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code:
System.setProperty("webdriver.chrome.driver", ".\\driver\\chromedriver.exe");
driver.get("h p://www.istqb.in/");
WebElement founda on =
driver.findElement(By.xpath("//span[.='FOUNDATION']"));
ac ons.moveToElement(founda on).perform();
Thread.sleep(3000);
WebElement enrollment =
driver.findElement(By.xpath("(//span[text()='ENROLLMENT'])[1]"));
ac ons.moveToElement(enrollment).perform();
Thread.sleep(3000);
WebElement corporateEnrol =
driver.findElement(By.xpath("//span[text()='CORPORATE ENROLLMENT']"));
ac ons.moveToElement(corporateEnrol).perform();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[text()='ONLINE ENROLLMENT']")).click();
driver.close();
}}
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 93/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Program :
Selenium Code:
System.setProperty("webdriver.chrome.driver", ".\\driver\\chromedriver.exe");
ac ons.dragAndDrop(block1, block3).perform();
Program :
//hint - first find out the x-coordinate and height of block 3 and then add 10 points to it and
then do it
Selenium Code:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 94/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.chrome.driver", ".\\driver\\chromedriver.exe");
}}
******************************************************************************
*******************************************
HANDLING POP UP
******************************************************************************
*******************************************
1. Javascript Popup
2. Hidden Division popup
3. File Upload popup
4. File download popup
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 95/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. Javascript Pop up :
1. Alert pop up
2. Confirmation pop up
3. Prompt pop up
1. Alert Pop up :
Characteristics features :
In order to handle the alert pop up, we first have to switch to alert window
using the below statement.
driver.switchTo().alert();
After transferring the control to alert window, we can use the following
methods of
“Alert” interface.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 96/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
2. Confirmation Pop up :
Characteristics features :
● In order to handle the alert pop up, we first have to switch to alert window
driver.switchTo().alert();
● After transferring the control to alert window, we can use the following
import org.openqa.selenium.Alert;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 97/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("http://www.tizag.com/javascriptT/javascriptprompt.php");
driver.findElement(By.xpath("//input[@value='Say my name!']")).click();
Thread.sleep(2000);
Thread.sleep(2000);
System.out.println(alert.getText());
Thread.sleep(2000);
//enter your name in the text box present on the alert pop up
alert.sendKeys("ajit");
Thread.sleep(2000);
//click on OK button
alert.accept();
Thread.sleep(2000);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 98/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.out.println(alert.getText());
alert.dismiss();
-------------
What are the Alert popup excep ons that you are aware of ?
1. NoAlertPresentExcep on :
When the alert pop up is physically not available on the page, and we s ll try to either switch
to the alert or try to perform any ac on on alert popup, we get this excep on.
2. UnhandledAlertExcep on :
We get this excep on, when we perform any ac on on the webpage without handling the alert
popup.
-------------------
· Any pop up which will be in hidden mode as soon as we navigate to the page, the
moment we perform action ,pop up will appear and then it goes off . This is all about Hidden
Division Pop up
· One of the examples for Hidden Division Pop up is Calendar pop up.
· If we are able to inspect the element present on the webpage then we can handle
Hidden Division Pop up by using:
If we are not able to inspect the element present on the webpage, then we go for some other
option
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 99/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.chrome.driver",
"./driver/chromedriver.exe");
option.addArguments("--disable-notifications");
option.addArguments("--disable-geolocation");
option.addArguments("--ignore-certificate-errors");
driver.get("https://www.cleartrip.com/");
Thread.sleep(3000);
driver.findElement(By.xpath("(//input[@placeholder='Pick a
date'])[1]")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("24")).click();
public class
Day15_Program2_HiddenDivisionPopup_CalendarPopup_cleartrip_selectTodaysDate extends
BaseClass {
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 100/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.setProperty("webdriver.gecko.driver",
"./driver/geckodriver.exe");
profile.setPreference("geo.enabled", false);
cap.setCapability(FirefoxDriver.PROFILE, profile);
driver.get("https://www.cleartrip.com/");
Thread.sleep(3000);
driver.findElement(By.xpath("(//input[@placeholder='Pick a
date'])[1]")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("24")).click();
A popup using which, we can upload a file from local system to destination server is called file
upload popup.
1. Using any third party tool which can handle window based applications.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 101/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
we pass the path of the file that we want to upload as an argument to sendkeys method.
This is some kind of short cut approach to handle file upload popup. We can use this short
cut, only and only if the html source code of the upload button or browse button have an
attribute called TYPE = "FILE"
eg:
we can handle this file upload popup using Robot class as well.
package test;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("http://nervgh.github.io/pages/angular-file-upload/examples/simple");
Thread.sleep(2000);
driver.findElement(By.xpath("//input[@multiple='']")).sendKeys("D:\\Ajit\\tes
tdata\\Absolute xpath examples.xlsx");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 102/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Thread.sleep(2000);
driver.findElement(By.xpath("//button[@ng-click=\"item.upload()\"]")).click();
Thread.sleep(2000);
driver.close();
}}
Characteristic features:
● In Google Chrome browser, when we click on Download link of Java language present
on Selenium official website, it doesn’t show any file download pop up on the screen,
instead, it automatically starts downloading the file in default location on the system.
(i.e downloads folder)
● But, in firefox browser, on clicking on the same download link, we get a file download
pop up on the screen. In order to handle this pop up, we use setPreference() method
of FirefoxProfile class.
● setPreference() is used to change the settings of Firefox browser.
● setPreference() method is an overloaded method which takes 2 parameters (KEY,
VALUE).
http://kb.mozillazine.org/About:config_entries#Browser
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 103/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Following example demonstrates how to use key and value with setPreference()
method.
// If the file type is .zip, then don’t display the popup, instead, download it
directly.
profile.setPreference(key, value);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\");
In the above example, "application/zip" refers to MIME types. (Multi purpose Internet Mail
Extension), which says what kind of file you want to download.
For a detailed level information on MIME types (or the type of file to be downloaded), visit
the following website.
https://www.freeformatter.com/mime-types-list.html
Interview Questions :
Ans :
File download popup is a window based application, which is not supported by selenium.
In order to handle such popup, we will have to use any third party tool that can handle
window based applications. One such tool we have used in our project is AUTOIT, using which
we have handled window based popups.
Instead of using AUTOIT, we can handle window based popup using FirefoxProfile class, this
class we use to do any profile related settings in firefox browser.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 104/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
FirefoxProfile class has a non static method called setPreference, which take 2 arguments -
KEY and VALUE, Key is always string, and value can be either an int or boolean or string.
As part of the key and value, we specify some profile related settings - like ,, if we are
downloading any .zip file, don't show the popup, Instead directly start downloading.
But, FirefoxDriver class has no constructor defined which takes an argument of FirefoxProfile
class object.
And hence, we create an object of another class called FirefoxOptions, to which we assign the
profile object, which has the modified settings.
So, during the script execution, when it launch the firefox browser, it will get the instruction
like , if user is trying to download any type of file, don’t show the popup.
Program : Write a script to download the selenium-java present on selenium official website
without opening the file download pop up and save it to specific folder in any drive in your
system.
Selenium Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
//Set the Key so that it will not show the file download pop up on the screen
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 105/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
profile.setPreference(key, value);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\Ajit\\Others");
cap.setCapability(FirefoxDriver.PROFILE, profile);
driver.get("http://www.seleniumhq.org/download/");
//Use following-sibling axes in Xpath to find the download link for selenium
java
driver.findElement(By.xpath("//td[text()='Java']//following-sibling::td[3]/a")).c
lick();
Thread.sleep(3000);
}}
Note : After the script is executed, verify that the file is downloaded in the specified folder
location.
How do you download in Chrome Browser where in you will not get the file download pop up
?
package qspiders;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 106/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", "D:\\");
options.setExperimentalOption("prefs", chromePrefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver.get("http://www.seleniumhq.org/download/");
Thread.sleep(3000);
String xp = "//td[.='Java']/following-sibling::td/a[.='Download']";
driver.findElement(By.xpath(xp)).click();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 107/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
}
OR
Ans :
2. getWindowHandles() method returns the window handle id of all the browsers launched
by selenium in the form of Set of String.
3. If we want to perform any action on specific browser window, we first have to switch to
that particular window.
As an argument to window() method, we pass the unique window id of the browser window
to which we want to switch to.
And once our driver control is on any browser window, we can perform any action on any
elements.
Note:·
If there is no such window present on the browser and we are trying to switch to the window
by calling the above statement then we get an exception called NoSuchSessionException.
Characteristic features :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 108/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("http://localhost:8080/login.do");
System.out.println(windowHandle);
}}
Selenium Code :
import org.openqa.selenium.WebDriver;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 109/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("http://localhost:8080/login.do");
System.out.println(windowHandle);
}}
Program :
Scenario :
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 110/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("https://www.naukri.com/");
driver.switchTo().window(windowHandle);
driver.close();
//driver.quit();
}}
Program :
Write a script to close only the main browser window and not the child browser windows.
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 111/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("https://www.naukri.com/");
driver.switchTo().window(windowHandle);
/* compare the window id with the Parent browser window id, if both
are equal, then only close the main browser window.*/
if (windowHandle.equals(parentWindowhandleID)) {
driver.close();
}}}}
Program :
Write a script to close all the child browser windows except the main browser.
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 112/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("https://www.naukri.com/");
driver.switchTo().window(windowHandle);
/* compare the window id of all the browsers with the Parent browser
window id, if it is not equal, then only close the browser windows.*/
if (!windowHandle.equals(parentWindowhandleID)) {
driver.close();
}}}}
Program :
Selenium Code :
driver.get("https://www.naukri.com/");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 113/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
//Set the expected title of the browser window which you want to close
driver.switchTo().window(windowHandle);
//Checks whether the actual title contains the specified expected title
if (actual_title.contains(expected_title)) {
driver.close();
Program :
Write a script to navigate between multiple tabs and perform some action on each tabs ?
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 114/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("http://localhost:8080/login.do");
//enter username
driver.findElement(By.id("username")).sendKeys("admin");
//enter password
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//a[text()='actiTIME Inc.']")).click();
driver.switchTo().window(wh);
driver.close();
driver.switchTo().window(parentwindowHandle);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 115/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.findElement(By.xpath("//div[text()='Login ']")).click();
driver.close();
--------------------
NoSuchWindowException :
when we try to switch to a browser window using a window handle id, which doesn’t match
with the window handle id of any browser windows launched by selenium, we get this
exception.
NoSuchSessionException :
When we try to switch to a browser window using a window handle id, and all the browser
--------------------
● It returns the window handle id of the current browser window in the form of String.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 116/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● It returns the window handle id of all the browser windows launched by selenium in
Window handle id are unique and hence it is SET and why String, because these ids are in the
form of String and hence the return type of getWindowHandles method is set<String>.
Every browser window has a unique ID, using which selenium identifies a browser window
uniquely. Using this window handle id, we perform action on any particular window.
------------
In Selenium, if the pop up displayed on the application doesn’t belong to the following types,
● JavaScript popup,
● We can move some of the window popups and some of them, we can’t.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 117/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● In selenium, there is no option to handle window pop up, hence, we have to use some
third party tool like AUTOIT to handle this kind of pop up. We can also use ROBOT
class to handle this pop up.
● But, by using ROBOT class, we can’t achieve much functionalities, as it has limited
option eg: we can’t identify the object properties present on the window pop up.
● Hence, we use another third party automation tool called AUTO IT.
What is AUTO IT ?
https://www.autoitscript.com/site/autoit/downloads
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 118/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● In the above image, drag the “Finder Tool” option and drop it on any element/control
present on the window pop up for which you want to identify the properties.
● It will display the properties of the same controls such as Class, Name, ID and Text.
● These properties are also known as CONTROL ID, using which AutoIT locates
elements/controls on window pop up.
● General syntax for using single Control ID is :
[Control ID : Value]
● We can use multiple Control IDs as well using semicolon as the delimiter to identify
the controls using below syntax.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 119/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● Navigate to the below path and open the Editor to write the autoIT script
● Write the autoIT script and save the file with .au3 extension
● Go to Tool → Select Compile and compile script. As a result, it will generate an .exe
file
● Navigate to the folder location where .exe file is located and double click on this .exe
file to execute the autoIT script.
● We can also execute the script from eclipse by using RunTime class of Java
Selenium Code:
Write the below lines of code in AutoIT editor, save with .au3 extension.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 120/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Now, write the below selenium code to run the .exe file
System.setProperty("webdriver.gecko.driver", ".\\driver\\geckodriver.exe");
driver.get("http://localhost:8080/login.do");
Thread.sleep(3000);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_P);
r.keyRelease(KeyEvent.VK_P);
r.keyRelease(KeyEvent.VK_CONTROL);
run.exec("D:\\Ajit\\Selenium\\SeleniumBtm_7thSep17\\AutoIT
scripts\\CancelOnPrintWindowPopup.exe");
driver.close();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 121/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
}}
Code below:
Open the auto it script editor and write the below script.
-----------------------
Summary of the different popups in selenium and how to handle those is mentioned below.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 122/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Ans:
Ans :
Selenium can't handle window based popup and hence, on order to handle such popup, we
have to use any third party tool which can handle window based popups. One such tool, we
What we do here is, we develop autoit scripts based on our requirement and then we save
the
file with .au3 extension. This file, we further compile to get an .exe version. How we compile
is -- Tools -- compile..It will generate a compiled autoit script (.exe) in the same location
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 123/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Runtime.getRuntime().exec("./autoIT/printpopup.exe");
what we pass as an argument to exec() method is the path of the autoit compiled script.
When the script executes, it performs action on the window based popup. and this is how it
----------------------------------------------
What is findElements() ?
in Selenium.
● findElements() identifies the elements on the webpage based on the locators used.
● If it does not find any matching web element on the web page, it returns an empty list.
Program :
Write a script to find the total number of links, number of visible links and number of hidden
Selenium Code :
driver.get("http://localhost:8080/login.do");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 124/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
int visibleLinkCount = 0;
int hiddenLinkCount = 0;
//if the link is displayed, then print the text of the link
if (link.isDisplayed()) {
visibleLinkCount++;
}else{
hiddenLinkCount++;
driver.close();
}}
Assignment :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 125/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
● Login in to actitime
● click on Tasks
Selenium Code:
WebTable :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 126/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 127/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Program :
Selenium Code:
driver.get("D:\Ajit\Selenium\SeleniumBtm_7thSep17\webpages\WebTable.ht
ml");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 128/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
int countNumberValue = 0;
int sum=0;
try{
System.out.print(“ ”+number);
countNumberValue++;
sum = sum+number;
}catch (Exception e) {
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 129/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.close();
Assignment :
Write a script to verify that the sum of marks present in the below table is same as the Total
marks.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 130/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 131/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 132/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Program :
Selenium Code :
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("selenium");
Thread.sleep(2000);
List<WebElement> allOptions =
driver.findElements(By.xpath("//*[contains(text(),'selenium')]"));
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 133/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.out.println(" "+text);
if (text.equalsIgnoreCase(expectedValue)) {
option.click();
break;
--------
2. by using select class. We can use select class only and only if the list box is develop using
select tagname.
Select class has multiple non static methods, in order to call them, we need to create an
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 134/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
When we create an object of Select class, we pass the reference of the list box on which we
-- getOptions() -- this method returns the address of all the options present in the list box in
-- getAllSelectedOptions() -- this method returns the address of all the selected options from
If none of the elements are selected in the list box, it returns an empty list object.
-- getFirstSelectedOption() -- this method returns the address of the first selected option in
If none of the elements are selected in the list box, it throws NoSuchElementException.
In order to select any option in the list box, we have 3 methods like,
-- selectByIndex() -- this method is used to select any option in the list box by using the index.
If specified index doesn't match with any element in the list box, it throws
NoSuchElementException.
-- selectByValue() -- this method is used to select any option in the list box by using the value
attribute.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 135/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
If specified value doesn't match with any element in the list box, it throws
NoSuchElementException.
-- selectByVisibleText() -- this method is used to select any option in the list box by using the
If specified text doesn't match with any element text in the list box, it throws
NoSuchElementException.
If the list box is of type multi select, we can also deselect any option in the list box which is
if the list box is not of type multi select, and if we try to call any of the deselect methods, we
In order to check whether the list box is single select or multi select, we can use isMultiple()
method.
This method returns true if the list box is a multi select list box. and it returns false, if it is
-------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 136/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
➔ selectByIndex()
➔ selectByValue()
➔ selectByVisibleText()
➔ deSelectByIndex()
➔ deSelectByValue()
➔ deSelectByVisibleText()
➔ isMultiple()
➔ getOptions()
➔ getAllSelectedOptions()
➔ getFirstSelectedOption()
➔ deSelectAll()
● We can use the following deSelect() methods only on multi select listbox. If we try to
➔ deSelectByIndex()
➔ deSelectByValue()
➔ deSelectByVisibleText()
➔ deSelectAll()
Program:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 137/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code :
driver.get("file:///D:/Ajit/Selenium/SeleniumBtm_7thSep17/webpages/ListBo
x_Breakfast.html");
//Create an object of Select class and pass the address of list box as an
argument
//getOptions() method returns a list of all the elements of the list box
System.out.println(text);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 138/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
//selectByIndex() selects an element based on the Index, here index starts with
0
s.selectByIndex(0);
s.selectByValue("v");
s.selectByVisibleText("Poori");
System.out.println(webElement.getText());
if (multiple) {
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 139/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
s.deselectByIndex(0);
//deselect an item which has an attribute called value and its value is
"v"
s.deselectByValue("v");
s.deselectByVisibleText("Poori");
Program:
Write a script to print the content of the list box in sorted order.
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 140/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.out.println(count);
System.out.println(text);
list.add(text);
Collections.sort(list);
System.out.println(value);
}}}
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 141/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Program:
Selenium Code :
driver.get("file:///D:/Ajit/Selenium/SeleniumBtm_7thSep17/webpages/ListBox_Breakfast.ht
ml");
System.out.println(count);
System.out.println(text);
allElements.add(text);
System.out.println(allElements);
Program:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 142/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a script to print the UNIQUE content of the list box in SORTED order.
Selenium Code :
driver.get("file:///D:/Ajit/Selenium/SeleniumBtm_7thSep17/webpages/ListBox_Breakfast.ht
ml");
System.out.println(count);
System.out.println(text);
allElements.add(text);
System.out.println(allElements);
Program:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 143/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code :
driver.get("file:///D:/Ajit/Selenium/AutomationByBhanuSir_BTM/testdataFile
s/ListBox_Breakfast.html");
System.out.println(text);
allElementText.add(text);
if (count1==count2) {
else{
System.out.println(allElementText);
driver.close();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 144/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
}}}
Program:
Selenium Code :
driver.get("file:///D:/Ajit/Selenium/AutomationByBhanuSir_BTM/testdataFile
s/ListBox_Breakfast.html");
if (!allElementText.add(text)) {
System.out.println(allElementText.size());
System.out.println(allElementText);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 145/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.close();
Program :
Selenium Code
package qspiders;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
driver.get("file:///D:/Ajit/Selenium/SeleniumBtm_7thSep17/webpages/ListBox_Breakfast.html"
);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 146/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
if (hashMapObj.containsKey(text)) {
value++;
hashMapObj.put(text, value);
}else{
hashMapObj.put(text, 1);
if (value>1) {
}}}}
Program:
Selenium Code :
driver.get("file:///D:/Ajit/Selenium/AutomationByBhanuSir_BTM/testdataFile
s/ListBox_Breakfast.html");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 147/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
System.out.println(text);
allElementText.add(text);
if (count1==count2) {
else{
System.out.println(allElementText);
driver.close();
}}}
Program:
Selenium Code :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 148/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.get("file:///D:/Ajit/Selenium/AutomationByBhanuSir_BTM/testdataFile
s/ListBox_Breakfast.html");
if (!allElementText.add(text)) {
System.out.println(allElementText.size());
System.out.println(allElementText);
driver.close();
Program :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 149/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Selenium Code
package qspiders;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
driver.get("file:///D:/Ajit/Selenium/SeleniumBtm_7thSep17/webpages/ListBox_Breakfast.html"
);
if (hashMapObj.containsKey(text)) {
value++;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 150/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
hashMapObj.put(text, value);
}else{
hashMapObj.put(text, 1);
if (value>1) {
}}}}
___________________________________________
Definition :
Page object model is a page factory design pattern. We implement this model in our
automation framework because of the following advantages listed below.
Advantages of POM :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 151/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
-------------------
-------------------
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
//Declaration
@FindBy(id="username")
@FindBy(name="pwd")
@FindBy(xpath="//div[.='Login ']")
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 152/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
//Initialisation
PageFactory.initElements(driver, this);
//Utilisation
unTB.sendKeys(un);
pwTB.sendKeys(pw);
loginBtn.click();
}}
------------------------------------------------------------------
TESTNG Framework
● Data Driven testing can be achieved (Data parameterisation is possible using testng)
● we can execute the test scripts in batch (i.e multiple test scripts at one go)
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 153/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Write a program to check the sequence in which the annotations of Testng class gets
executed.
_________________________________________________________________
package testngpackage;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 154/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@BeforeMethod
Reporter.log("beforeMethod", true);
@AfterMethod
Reporter.log("afterMethod", true);
@BeforeClass
Reporter.log("beforeClass", true);
@AfterClass
Reporter.log("afterClass", true);
@BeforeTest
Reporter.log("beforeTest", true);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 155/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@AfterTest
Reporter.log("afterTest", true);
@BeforeSuite
Reporter.log("beforeSuite", true);
@AfterSuite
Reporter.log("afterSuite", true);
}}
_________________________________________________________________
_________________________________________________________________
By using groups attribute of Test annotation. what do we pass here is the name of the
module or a group to which we want a test method to tag to.
we can tag a particular test method to a group of modules as well by passing the list of
modules in a string array.
Syntax :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 156/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
-------------------
<groups>
<run>
</run>
</groups>
Summary :
which ever modules or groups are part of the include statement, those module specific or
group specific test cases will be executed. If anything is specified with exclude statement, will
not be executed.
-----------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 157/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
--------------------
Priority :
---------------------
eg:
@Test(priority=1)
System.out.println("createUser...");
--------------------
The least the value, the highest the priority, and the highest the value, the least the priority.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 158/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Multiple test methods with same priority takes default order of execution (alphabetical
order)
-----------------------------------
InvocationCount Attribute:
number of times.
syntax :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 159/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Test(invocationCount=4)
System.out.println("editUser...");
Note:
If we specify invocationCount as 0, the test method will not be consider for execution (It will
be skipped but will not be updated in the
report/console)
------------------------------------------------------
HOw do you skip a test method from execution without using invocationCOunt ?
syntax :
@Test(invocationCount=4, enabled=false)
System.out.println("editUser...");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 160/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Enabled=false will not execute the test method. It takes precedence over
invocationCount.
---------------------------------------------------------
Syntax :
@Test(priority=1)
System.out.println("createUser...user created
successufuly");
Assert.fail();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 161/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Test(priority=3, dependsOnMethods="createUser")
System.out.println("deleteUser...");
Here, createuser method will be failed and deleteUser method will be skipped. (In this case,
skipped count will be updated on the report or console)
---------------------------------------------
When two test methods are dependent on each other, it results in to Testng
Exception.
Eg:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 162/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Test(priority=1, dependsOnMethods="deleteUser")
@Test(priority=3, dependsOnMethods="createUser")
System.out.println("deleteUser...");
----------------------------------------------------------
--------------------
---------------------
It is an attribute of test annotation, using which, the order of execution of test methods can
be prioritized.
eg:
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 163/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Test(priority=1)
System.out.println("createUser...");
--------------------
The least the value, the highest the priority, and the highest the value, the least the priority.
Multiple test methods with same priority takes default order of execution (alphabetical
order)
-----------------------------------
InvocationCount is used to invoke or run a test method for the specified number of times.
syntax :
@Test(invocationCount=4)
System.out.println("editUser...");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 164/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Note:
If we specify invocationCount as 0, the test method will not be consider for execution (It will
be skipped but will not be updated in the report/console)
------------------------------------------------------
How do you skip a test method from execution without using invocationCount ?
syntax :
@Test(invocationCount=4, enabled=false)
System.out.println("editUser...");
Enabled=false will not execute the test method. It takes precedence over invocationCount.
---------------------------------------------------------
Syntax :
@Test(priority=1)
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 165/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Assert.fail();
@Test(priority=3, dependsOnMethods="createUser")
System.out.println("deleteUser...");
Here, createuser method will be failed and deleteUser method will be skipped. (In this case,
skipped count will be updated on the report or console)
---------------------------------------------
When two test methods are dependent on each other, it results in to Testng Exception.
Eg:
@Test(priority=1, dependsOnMethods="deleteUser")
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 166/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Test(priority=3, dependsOnMethods="createUser")
System.out.println("deleteUser...");
----------------------------------------------------------
package demotest;
import org.testng.Reporter;
import org.testng.annotations.Test;
Reporter.log("CreateUser", true);
Reporter.log("editUser", true);
@Test(priority=3, groups={"user"})
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 167/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Reporter.log("deleteUser", true);
Reporter.log("createProduct", true);
Reporter.log("editProduct", true);
@Test(priority=3, groups={"product"})
Reporter.log("deleteProduct", true);
_________________________________________________________________
Convert the above testng class to create testng.xml suite file as shown below
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 168/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
_________________________________________________________________
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include name="user"></include>
<exclude name="user"></exclude>
</run>
</groups>
<classes>
<class name="testngpackage.DemoA"/>
</classes>
_________________________________________________________________
We can create multiple test blocks to work on multiple browsers in automation project.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 169/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
_________________________________________________________________
package testngpackage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
static{
System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 170/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Test
@Parameters({"browser"})
//Reporter.log(browser, true);
if (browser.equals("firefox")) {
} else {
prop.load(configPath);
driver.get(url);
WebElement un = driver.findElement(By.id("username"));
un.sendKeys("admin" + i);
Thread.sleep(2000);
un.clear();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 171/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.close();
_________________________________________________________________
<test name="TestFirefox">
<classes>
<class name="testngpackage.LaunchFirefoxAndChromeTogether"/>
</classes>
<test name="TestChrome">
<classes>
<class name="testngpackage.LaunchFirefoxAndChromeTogether"/>
</classes>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 172/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
-----------------------------------------------------
Ans:
2. In order to create data, we create an object of 2 dimensional array wherein we specify the
row and column.
Here, row represents the number of times the test method should iterate and column
represents the number of parameters that we should pass as an argument to the test
method.
3. These databank can be utilised across any testng classes by using an attribute of TEST
annotation called dataProvider. Once we have the access to data bank, we can use the data in
our script.
Note: A test method can’t access data from multiple data banks at the same time.
code :
package scripts;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 173/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@DataProvider
data[0][0] = "admin1";
data[0][1] = "manager1";
data[1][0] = "admin2";
data[1][1] = "manager2";
return data;
@Test(dataProvider="dataBank")
------------------------------
-----------------------------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 174/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Executing the test scripts with multiple set of data by taking the data from external source is
known as data parameterization.
- In testing suite file, what we do is, we declare and initialize the parameters using parameter
tag.
-Once the parameters are declared and initialized, we utilize these parameters from any
testNG class by using @Parameters annotation.
-As an argument to this Parameters annotation, what we pass is the parameter name which is
declared in the suite file.
-And then by creating local variables to the test method, we access these parameters value
and utilize them in our scripts.
This is how ,we achieve data parameterization using testng suite file.
----------------------
In testng framework, listeners keep on listening to the execution status of suite, test blocks,
and testng classes. And based on a predefined event, it performs specific actions.
for eg :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 175/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
In our project, if we need to connect to databases and perform some database validations,
then before starting the suite execution, we can connect to database and once the entire
suite is executed, we can flush out the unwanted data from the database.
code :
@Override
@Override
---------------------------
· Data parameterisation from Excel file can be done by using Apache poi related jar file
which has a class called WorkbookFactory.
· We call static method of this WorkbookFactory called create( ) wherein we pass reference
of FileInputStream class as an argument ,this returns an instance of Workbook interface.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 176/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
· Using this Workbook reference we call getSheet( ) method , where in we pass sheet no. as
an argument which specifies from which sheet we are accessing the data.
· Then to get to the particular row we use get row( ) method and to get to the particular
column we use getCell( ) method and
-------------------------------------
GO to below url:
https://poi.apache.org/download.html#POI-4.0.0
--------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 177/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1) A file with . properties extension is called property file where data is stored in the form of
key-value pair.
2) Data parameterisation from property file can be done by using Properties class which has
a non static method called load( ).
3)We use load( ) method to a load property file which takes reference of FileInputStream
class as an argument and
4) To access data from property file we use get property( ) method which takes property
name(key) as an argument.
___________________________________________________________
SELENIUM GRID
To run the same scripts on multiple browsers and multiple systems parallely, we use
Selenium Grid.
1. HUB
2. NODE.
Node is the remote system on which you run the automation scripts.
In node system, JDK and Browser should be installed and we should also know the ip address.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 178/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
It Is used for Cross browser compatibility testing and cross platform testing on multiple
Operating systems.
1. Download selenium server jar file and browser specific driver executables files such as
chromedriver.exe and geckodriver.exe files in to a folder.
2. In the same folder, create a batch file with .bat extension and write the following
command.
java -Dwebdriver.gecko.driver=geckodriver.exe
-Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-standalone-server.jar
3. Double click on the Run.bat file and it should display the following message in the
command prompt window.
HUB :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 179/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
It is centralised system where the script is present. It is also used to control the execution.
We run the scripts from HUB and it will connect to remote system called NODE.
It will open the browser and perform action in the node and the result will be stored in the
HUB machine.
1. Hub will have all the softwares which is required for a typical selenium machine.
2. Update the selenium code to execute the scripts in remote system as shown below.
___________________________________________________________
package demotest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@Test
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 180/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Parameters({"node","browser"})
whichbrowser.setBrowserName(browser);
}}
---------------------------------------------------------------
Update the testng.xml suite file to run in multiple browsers and multiple systems.
__________________________________________
<test name="TestFirefox">
<classes>
<class name="testngpackage.SeleniumGridExample"/>
</classes>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 181/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
<test name="TestChrome">
<classes>
<class name="testngpackage.SeleniumGridExample"/>
</classes>
___________________________________________________
SELENIUM FRAMEWORK
Framework is set of instructions or guidelines which should be followed by all the automation
engineers in the team while automating an web application.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 182/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. In POM driven framework, we have created POM classes for all the page of our
application under test.
2. In our application, we had 20 pages in total and for all these 20 pages, we have
created 20 pom classes. All these pom classes, we have created in a single package
called pompages.
3. In each POM class, we declared the elements present on that particular page using
@FindBy annotation. As an argument to FindBy annotation, we can use any one of the
locator using which, element can be uniquely identified on the web page.
4. Once elements are declared, we initialise all the elements declared above using
PageFactory.initElements() method, which accepts 2 arguments - both are of type
object. First argument is WebDriver driver object and second argument is the current
class object which is referred by this keyword , we write this statement inside the
constructor, so that when the object of this pom class is created from another class, it
will invoke the constructor and initialise all the elements which are declared in the
pom class.
5. Once elements are declared and initialized, we utilise all the elements by creating
respective setter methods. This is what we have done on a high level inside a pom
class.
TESTNG FRAMEWORK :
1. Based on the number of test cases, we will create that many number of Testng classes.
In our project, we had close to 678 regression test cases and we have developed 678
testng class with one test method in each class.
2. In test method, we create object of respective POM class and using this reference
variable, we keep calling the relevant method of pom class based on the manual test
steps. This is how, we have automated our scripts using testng framework.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 183/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
1. Executing the same scripts with multiple set of data is called data parameterisation.
We used Excel file to get data from external source and utilised it in the scripts.
2. Using apache poi related jar, we implemented this data driven technique to achieve
data parameterisation in our framework. Hence, our framework is also a data driven
framework.
1. We created few generic methods to access data from external sources like Excel file,
config file, etc.
2. And furthermore, based on the manual regression test case steps, we call the relevant
method of pom class from the testng class, In this way , our framework is a method
driven framework as well.
---------------------------------------------------------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 184/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
_______________________
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 185/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
_____________________________________________
BaseTest.java Code
_________________________________
package generic;
import java.util.concurrent.TimeUnit;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 186/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
static{
System.setProperty(GECKO_KEY, GECKO_VALUE);
System.setProperty(CHROME_KEY, CHROME_VALUE);
@BeforeMethod
driver.get(url);
driver.manage().timeouts().implicitlyWait(timeoutPeriod, TimeUnit.SECONDS);
@AfterMethod
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 187/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
driver.close();
__________________________________________________
package generic;
___________________________________
package generic;
import java.io.FileInputStream;
import java.util.Properties;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 188/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
try {
prop.load(new FileInputStream(CONFIG_PATH));
property = prop.getProperty(key);
} catch (Exception e) {
return property;
int rowCount = 0;
try {
wb = WorkbookFactory.create(new FileInputStream(EXCEL_PATH));
rowCount = wb.getSheet(sheet).getLastRowNum();
} catch (Exception e) {
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 189/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
return rowCount;
public static String getCellValue(String EXCEL_PATH, String sheet, int row, int column){
try {
wb = WorkbookFactory.create(new FileInputStream(EXCEL_PATH));
value = wb.getSheet(sheet).getRow(row).getCell(column).toString();
} catch (Exception e) {
return value;
____________________________________________________
pompackage - LoginPage.java
package pompages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 190/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
//declaration
@FindBy(id="username")
@FindBy(name="pwd")
@FindBy(xpath="//div[.='Login ']")
//initialisation
PageFactory.initElements(driver, this);
//Utilisation
unTB.sendKeys(un);
pwTB.sendKeys(pw);
loginBtn.click();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 191/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
___________________________________________
package scripts;
import org.testng.annotations.Test;
import generic.BaseTest;
import generic.Lib;
import pompages.LoginPage;
@Test
l.setUsername(un);
l.setPassword(pw);
l.clickLogin();
}}
__________________________________________________
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 192/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
package generic;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
static{
System.setProperty(GECKO_KEY, GECKO_VALUE);
System.setProperty(CHROME_KEY, CHROME_VALUE);
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 193/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@BeforeMethod
driver.get(url);
driver.manage().timeouts().implicitlyWait(timeoutPeriod, TimeUnit.SECONDS);
@AfterMethod
driver.close();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 194/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
try {
FileUtils.copyFile(srcFile, destFile);
} catch (IOException e) {
e.printStackTrace();
------------------------
package generic;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
@Override
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 195/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Override
@Override
b.takeScreenshot("TestValidLogin");
@Override
@Override
@Override
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 196/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
@Override
-----------------------
<suite name="Suite">
<listeners>
<listener class-name="generic.TestngListeners"></listener>
</listeners>
<test name="Test">
<classes>
<class name="scripts.TestLogin"/>
</classes>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 197/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
-------------------------
package scripts;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import generic.BaseTest;
import generic.Lib;
import pompages.LoginPage;
@Test
l.setUsername(un);
l.setPassword(pw);
l.clickLogin();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 198/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
s.assertEquals(actualtitle, expectedTitle);
s.assertAll();
-------------------
MAVEN PROJECT :
Apache Maven is a so ware project management and build dependency management tool for
Selenium/java frameworks.
Why Maven ?
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 199/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
leave it as it is..
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 200/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 201/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Go to 2nd tab which is Dependencies tab and add dependency related informa on from below
url
h p://www.mvnrepository.com
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 202/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 203/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
copy the below dependency informa on and add it in the pom.xml file
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 204/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 205/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
now save the project -- control + S - in order to build the dependency jar files to the project
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 206/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Verify that the selenium server dependency is added to the pom.xml tab as shown below.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 207/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Now , add testng related dependency jar files to the pom.xml file.
How ?
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 208/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 209/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Now add testng related dependency informa on to the pom.xml file as shown below
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 210/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
____________________
<modelVersion>4.0.0</modelVersion>
<groupId>Automa onProjectWeekendBatch</groupId>
<version>0.0.1-SNAPSHOT</version>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 211/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<version>3.5.1</version>
<configura on>
<source>1.8</source>
<target>1.8</target>
</configura on>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.20.1</version>
<configura on>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configura on>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 212/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<version>6.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<version>3.17</version>
</dependency>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 213/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
<dependency>
<groupId>org.apache.poi</groupId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<version>3.17</version>
</dependency>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 214/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
<dependency>
<groupId>org.apache.poi</groupId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<version>1.1</version>
</dependency>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 215/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
<dependency>
<groupId>com.github.virtuald</groupId>
<version>1.05</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<version>2.9.1</version>
</dependency>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 216/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
</dependencies>
</project>
-----------------------------------------------
Right click on pom.xml file and run as maven clean for the first me.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 217/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Solu on : Windows - preference -- Java - Installed JREs → navigate to loca on wherer your
jdk1.8 is iintalled (c--program files --java - JDK)
2. MOJOExcep on
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 218/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Right click on the project → maven → update project → Select the project and select the
highlighted op ons:
- offline checkbox
- force update of snapshots
Note : S ll if it doesn’t work, delete the .m2 folder from the below loca on.
C://users/admin/.m2
-------------------------------------------------------------------------------------------------------
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 219/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Maven compiler plugin is responsible for building the maven project. This compiler plugin
utilises all the dependency files to build the project. How it works is : First, compiler checks
for the availability of the dependency file in maven local repository ( that is .m2 folder). If the
file is not present in .m2 folder, what it does is : it goes to the respective website specified in
the pom.xml file and downloads the files in .m2 folder for further use. And in case, if the
dependency file is already present in .m2 folder, it doesn’t go to the website, instead, it
utilises the resources from the maven local repo only to build the project. This is how maven
works.
By using a plugin called Maven-Surefire-plugin. This plugin is responsible for executing the
maven project. All we have to let the surefire plugin know is the path of the suite files which
we want to execute.
The command to execute the maven project is : clean install compile test
install - this command is used to install any resources if required to build the project.
-------------------------------------------------------------------------------------------------------
JENKINS
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 220/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Advantages of Jenkins :
URL :
https://jenkins.io/download/
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 221/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 222/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Username : admin
Password is below;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 223/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 224/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 225/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 226/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 227/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Copy the project that you want to execute from Jenkins as shown below
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 228/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 229/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Enter name and click on free style project and click on OK button
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 230/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 231/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 232/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 233/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 234/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 235/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Navigate to available tab and search Testng Result plugin and install ..
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 236/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 237/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 238/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 239/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 240/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Next..
Since the plugin is already installed, you can see the plugin under INSTALLED tab as shown
below.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 241/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 242/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 243/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Step 3:
Add the below details under Extended Email notification as shown below.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 244/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Step 4 :
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 245/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 246/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Check that the email is successfully sent out to the gmail inbox.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 247/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Step 5 : Now go the project related job, CLICK on CONFIGURE and add the mail recipients as
shown below.
Navigate to POst Build Actions and Select Editable Email Notification -- This is used to send
the testng execution status report in the mail body once the build is executed and finished
successfully.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 248/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 249/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 250/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
On this page, add the below script under Pre Send script text area.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 251/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 252/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Once the above configuration is done, then build the project by clicking on Build now
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 253/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 254/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
_______________________
GITHUB SETUP
Go to
www.github.com
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 255/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://desktop.github.com/
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 256/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Enter project name which we want to upload under NAME text box
And Local path is the actual workspace where in our project is located.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 257/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 258/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Once it is uploaded successfully, u will get something like this as shown below.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 259/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Now, go to the github , you will see that project is successfully uploaded to the central as
shown below
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 260/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 261/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
It will start downloading the project from GIthub to the local system in eclipse.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 262/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Once imported, we will do some changes and we will upload it back to github by using below
navigation.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 263/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 264/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
Now checkout the latest code from Github and get it in your local system.
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 265/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
_____________________________________
LOG4J :
Log4j
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.1</version>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 266/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
3. Create a log4j2.xml right under the src folder and do the following setting .
<Configuration status="WARN">
<Properties>
<Property name="basePath">./logs</Property>
</Properties>
<Appenders>
</RollingFile>
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 267/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
Important point : paste this file under the src folder in the project
Create a folder called logs under the projects and execute the script
----------------------------------------------
package scripts;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 268/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import generic.BaseTest;
import generic.Lib;
import pompages.LoginPage;
@Test
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "root");
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 269/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
while (rs.next()) {
l.setUsername(username);
l.setPassword(password);
l.clickLogin();
} catch (ClassNotFoundException e) {
Thread.sleep(10000);
s.assertAll();
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 270/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 271/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
****************************************************************************
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 272/273
12/26/2018 Selenium Notes_Weekend batch_10 to 2 PM_BTM.docx - Google Docs
List of Exceptions
****************************************************************************
--------------------------------------------------------------------------------------------------------
h ps://github.com/ajitbiswas/NewYearBatch_WeekendBatch10AM
https://docs.google.com/document/d/1xqPs6tJeVB5Z0f3s1hCkWRw7r_W_3CYL6AzjZPRCRCs/edit 273/273