Selenium Untit
Selenium Untit
------------------------------------------------
• By the end of this course you can automate any Web Appication
Ra
verification
• You will be familiar with the frameworks applied in the industry
• You will be able to write efficient codes applying Code reusability
Ki
• You will be able to integrate various Third party tools with Selenium
Tool
r
This course helps you gain knowledge theoretically and practically that
can be applied on real time Softwares. In this course, we use Seleniu
WebDriver as an Automation testing tool.
CS
Automation Testing
A4
Selenium
• What is Selenium ?
• Why Selenium (Advantages) ?
• What are its versions ?
• What all OS, Browsers and Programming Languages it support ?
001
• Java-Selenium Architecture
• WebDriver Architecture
• Basic Selenium Program to open and close browser
• Rntime Polymorphism Program in Selenium
• WebDriver abstract methods
• Locatos
• Xpath, its types and cases
AUTOMATION FRAMEWORK
=> Stages and Types of Framework
r
• Data-Driven
• Keyword-Driven
• Method-Driven
_K
- Assertion
- Grouping Execution
- Data Parameterisation
- Data driven through DataProvider
- Parallel Execution
- Distibuted parallel Execution
- Cross Browser Parallel Execution
• Modular Frameworks
• Hybrid Framework
=> Hybrid Framework Architecture
=> Introduction to Maven, GitHub and Jenkins
002
-----------
25-09-2020
-----------
G.
Ra
vi
Ki
• MT - Manual Testing
• AT - Automation Testing
• GUI - Graphical User Interface
_K
WebComponents
003
Example to create a link :
--------------------------
<html>
<head>
<title>My First Web Page</title>
</head>
<body>
<a href='Error! Hyperlink reference not valid. </a>
</body>
</html>
G.
• a means anchor
-----------
Ra
26-09-2020
-----------
vi
• Inside the <head> tag we use <title> tag to provide title of the
A4
webpage
• To create link we use anchor tag <a> and compulsory attribute called
'href' (href means hyper reference) where we provide address of
landing page
• To break the line and write element in next linewe use break rule <br>
004
• To give tooltip (info about the element) we use 'title' attribute
DropDown <select> NA
table <table> NA
Paragraph <p> NA
005
<a href='https://www.google.com' id='i2' name='n2' class='c2'
value='v2'>Google </a> <br>
<a href='https://www.facebook.com' title='click here to go to facebook
page'>Facebook </a>
<select name='month'>
<option> </option>
<option>JAN </option>
<option>FEB </option>
<option>MAR </option>
<option>APR </option>
<option>MAY </option>
<option>JUN </option>
G.
<option>JUL </option>
</select>
</body>
Ra
</html>
<html>
<head>
<title>My Registration Page</title>
</head>
Ki
<body>
<marquee> Ravi kiran </marquee>
r
Password<input type='password'></input><br>
<input type='radio' name='gender'>MALE</input><br>
<input type='radio'name='gender'>FEMALE</input><br>
A4
006
-----------
28-09-2020
-----------
G.
Ra
vi
Automation Testing:
• Process of tesing the functionality of an application, by the help
of automation tools is known as Automation Testing.
Ki
(or)
• Converting manual testcases into automaton scripts, executing them
r
007
-----------
29-09-2020
-----------
• We cannot automate progression testing, because we don't know the flow
of the functionality testng.
• We sart automating from the second release.
• Anything which is completely dynamic cannot be automated.
Ex: Pc-games, OTPs
6.Game Testing
• We need skilled resources to do automation testing.
• Cost involved in Automation testing is more compared to manual
r
008
-----------
30-09-2020
-----------
What is Selenium ?
Selenium is a free, Open-Source, Web-based Application automation tool.
/openqa/selenium
Chicago, USA
Earlier name --> 'JavaScriptTestRunner'
vi
• Selenium Core
• Selenium IDE - Record and play back tool
r
Note:
-----
A4
• Selenium supports all the OS like windows, Mac, Linux but except for
unix
009
RC Drawback :
------------
1. We need a server without which automation is not possible
2. Same Origin Policy
www.google.com //Automation possible
www.google.com/search.html //Automation possible
www.google.com/goToLink.com //Automation possible
www.google.com/maps //Automation possible
www.youtube.com //Automation not possible
Tools
------
G.
--------------------------
LoadRunner, Jmeter, NeoLoad, Web Load
CS
Java-Selenium Architecture
---------------------------
• Selenium WebDriver API communicates with programming language like
java by using JASON(Javascript Object Notation) Wire Protocol.
• These 2 combined communicates with the browsers by using respective
driver exceutible files.
010
Java-Selenium Architecture
---------------------------
G.
Ra
vi
Ki
r an
_K
CS
A4
011
-----------
01-10-2020
-----------
package qsp;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchBrowserTest {
Ra
//Firefox --->FirefoxDriver
System.setProperty(key, value);
_K
}}
A4
012
-----------
02-10-2020
-----------
package qsp;
import org.openqa.selenium.firefox.FirefoxDriver;
A4
013
G.
import org.openqa.selenium.firefox.FirefoxDriver;
public class LaunchFirefoxTest {
vi
System.setProperty("webdriver.gecko.driver",".\\drivers\\geckodri
ver.exe");
Ki
new FirefoxDriver();
}}
r an
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
CS
String key="webdriver.chrome.driver";
String value="E:<Error! Hyperlink reference not valid.>";
System.setProperty(key, value);
new ChromeDriver().close();
System.setProperty("webdriver.gecko.driver",".\\drivers\\geckodri
ver.exe");
FirefoxDriver firefox = new FirefoxDriver();
firefox.close();
}}
014
-----------
06-10-2020
-----------
WebDriver Architecture
-----------------------
G.
Ra
vi
Ki
015
package qsp; //Ex
import java.util.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class RuntimepolymorphismInSelenium {
public static void main(String[] args) {
WebDriver driver;
System.out.println("Enter Browser Name :");
Scanner sc=new Scanner(System.in);
String browserName = sc.next();
G.
if(browserName.equalsIgnoreCase("chrome"))
System.setProperty("webdriver.chrome.driver","./drivers/chromedri
{
ver.exe");
Ra
driver=new ChromeDriver();
}
vi
else if(browserName.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver","./drivers/geckodrive
r.exe");
driver=new FirefoxDriver();
Ki
}
}}
r an
_K
CS
A4
016
-----------
07-10-2020
-----------
017
7. manage() : used to manage browser window
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverMethods {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
Ki
"./drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
r
System.out.println(url);
System.out.println(pgSrc);
System.out.println(title);
if(title.equals("Google"))
{
System.out.println("Google Page is Displayed, Test Step PASSED");
}
else
{
System.out.println("Google Page is not Displayed, Test Step
FAILED");
}
}}
018
-----------
08-10-2020
-----------
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
Ra
Thread.sleep(3000);
Ki
//
// Thread.sleep(3000);
an
//
// Thread.sleep(3000);
// //navigate to next page
CS
// driver.navigate().forward();
//
// Thread.sleep(3000);
A4
//method Chaining
//Return type of current method should be same as parent of
next
019
//To change the position of the browser window
Point p=new Point(500,600);
driver.manage().window().setPosition(p);
3. get() will wait until complete page navigate() will not wait until
is loaded complete page is loaded
r an
_K
CS
A4
020
-----------
09-10-2020
-----------
Types of Locators :-
-------------------
r
1. tagName(String arg)
2. id(String arg)
an
3. name(String arg)
4. className(String arg)
_K
5. linkText(String arg)
6. partialLinkText(String arg)
7. cssSelector(String arg)
CS
8. xpath(String arg)
package qsp;
A4
//Ex
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Locators {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
021
WebDriver driver=new ChromeDriver();
driver.get("file:///E:/Qspiders%20Selenium/.html/demo.html");
Thread.sleep(2000);
// driver.findElement(By.id("i2")).click();
G.
// driver.findElement(By.name("n2")).click();
// driver.findElement(By.className("c2")).click();
Ra
driver.findElement(By.linkText("Google")).click();
vi
complete linktext
// wherein we use static part and ignore dynamic part
_K
driver.findElement(By.partialLinkText("Inbox"));
CS
// Bhanuprakasha - linkText
//
// partialLinkText
A4
// Bhanu
// prakash //uses of 'partialLinkText' locator
// akash 1.To handle partially dynamic links
// asha 2.To avoid writing lengthy linktexts
// anu
// hanu
// anup
// driver.findElement(By.partialLinkText("akash"));
}
}
022
-----------
10-10-2020
-----------
• In the above code, to create a link we use <a> tag and mandatory
attribute called 'href' but other attributes like id, name, class,
Ra
• So, in selenium we can use features like tag, attributes like id, name
vi
023
• Shortcuts of Css Selector :-
• Shortcut for id is #
div[id='i1']
div#i1
#i1
button
Limitation of CssSelector :
_K
8. xpath -
A4
024
Absolute xpath means complete path we travel from start(html) element to
--> it is achieved by '/' (Single Forward Slash) --> immediate
child/immediate descendant
<div>
A<input type='text'></input>
B<input type='text'></input>
</div>
G.
<div>
V<input type='text'></input>
Ra
D<input type='text'></input>
</div>
vi
</body>
</html>
--------------------
r
html [1]
body [1]
an
div [1]
input A [1]
_K
input B [2]
div [1]
input V [1]
CS
input D [2]
ABVD html/body/div/input
AB html/body/div[1]/input
A html/body/div[1]/input[1]
AV html/body/div/input[1]
BV html/body/div[1]/input[2] | html/body/div[2]/input[1]
025
-----------
12-10-2020
-----------
Relative xpath :
• Shortest path of element in HTML Tree
• // means any child or any descendant
html [1]
body [1]
G.
div [1]
input A [1]
input B [2]
Ra
div [1]
input V [1]
input D [2]
vi
ABVD //input
AB //div[1]/input
r an
A //div[1]/input[1]
AV //input[1]
_K
BV //div[1]/input[2] | //div[2]/input[1]
CS
AD //div[1]/input[1] | //div[2]/input[2]
A4
026
Case 2: xpath by text() function
Syntax: //tagName[text()='textValue']
//div[@fn='Deepika'] ----> 2
//div[@ln='Rai'] ----> 2
//div[@fn='Deepika' and @ln='Rai'] ----> 1
Ra
Ex-1://input[@class='RNmpXc' or @type='submit']
r
---> https://www.google.com/
Ex-2://input[@id='groupcode' and @type='text']
an
---> https://www.skillrary.com/user/login
Ex-3://span[@class='a-size-base' and text()='4GB RAM+ 64GB Storage']
_K
---> https://www.amazon.in/
CS
A4
027
-----------
13-10-2020
-----------
<div id='ui-id-1'>
<div id='ui-id-2'>
<div id='ui-id-3'>
<div id='ui-id-4'>
<div id='ui-id-5'>
element/following-sibling::next element
Travel from one element ---> previous element of same parent
element/preceding-sibling::previous element
CS
<html>
<body>
A4
<table border='1'>
<tbody>
<tr id='t1'>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
</tr>
<tr id='t2'>
<td>E</td>
028
<td>F</td>
<td>G</td>
<td>H</td>
</tr>
</tbody>
</table>
</body>
</html>
body
table
tbody
Ra
tr 't1'
td A
td B
vi
td C
td D
tr 't2'
td E
Ki
td F
td G
r
td H
an
6. E to t2 ---> //td[text()='E']/ancestor::tr[@id='t2']
7. t1 to body ---> //tr[@id='t1']/ancestor::body
A4
9. A to B ---> //td[text()='A']/following-sibling::td[text()='B']
10. A to B,C,D ---> //td[text()='A']/following-sibling::td
11. F to H ---> //td[text()='F']/following-sibling::td[text()='H']
12. A to C,D ---> //td[text()='A']/following-sibling::td[text()='C' or
text()='D']
029
-----------
14-10-2020
-----------
Cousin
B to F --->
• //td[text()='B']/ancestor::tr/following-sibling::tr[@id='t2']/des
cendant::td[text()='F']
• //td[text()='B']/ancestor::tbody/descendant::td[text()='F']
Uncle
G to t1 --->
G.
• //td[text()='G']/ancestor::tr/preceding-sibling::tr
• //td[text()='G']/ancestor::tbody/descendant::tr[@id='t1']
Ra
vi
Python Download
an
C# Download
JS Download
• //td[text()='Java']/following-sibling::td[@data-label='Links']/de
_K
scendant::a[text()='Download']
• //td[text()='Java']/ancestor::tr/descendant::a[text()='Download']
CS
• //td[text()='Java']/..//a[text()='Download']
https://www.flipkart.com/mobiles/pr?sid=tyy%2C4io&p%5B%5D=facets.brand
%255B%255D%3DRealme&otracker=nmenu_sub_Electronics_0_Realme
• //div[text()='Motorola']/preceding-sibling::div[@class='_1p7h2j']
030
-----------
15-10-2020
-----------
html [1]
body [1]
div [1]
a [1]
a [2]
a [3]
G.
div [2]
a [1]
a [2]
Ra
a [3]
div [3]
a [1]
vi
a [2]
a [3]
//a --> 9
Ki
//a[1] --> 3
a[1]
r
a[2]
a[3]
an
a[4]
a[5]
_K
a[6]
a[7]
a[8]
CS
a[9]
(//a)[6] --> 1
A4
(//div)[1] --> 1
031
• //input - matches every input on webpage
• //inpur[1] - matches all the first inputs of their parent
• (//input)[1] - matches very first input of the webpage
• (//input)[last()] - matches very last input
• (//input)[last()-1] - match last but one
• (//input)[position() mod 2 =0] - match all even inputs
• (//input)[position() mod 2 =1] - match all odd inputs
2. name
3. linktext
Ra
4. xpath
cssSelector xpath
cssSelector does not support text, it xpath supports text and also
CS
cssSelector is xpath is
uni-Directional(forward) Bi-Direcional(forward+backward)
032
-----------
16-10-2020
-----------
com/login.d
o
Ra
PW-
password and Page)should be
click on login displayed
button
Ki
package qsp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
_K
import org.openqa.selenium.chrome.ChromeDriver;
CS
033
driver.get("https://demo.actitime.com/login.do");
Thread.sleep(2000);
//Enter Valid Username in username textbox
driver.findElement(By.id("username")).sendKeys("admin");
vi
Thread.sleep(2000);
//Enter Valid Password in password textbox
driver.findElement(By.name("pwd")).sendKeys("manager");
Ki
Thread.sleep(2000);
r
Thread.sleep(5000);
_K
034
Handling Multiple Elements
---------------------------
1. We Handle multiple Elements by using findElements() method of
WebDriver interface.
2. Return type of findElements() is List<WebElement>
3. If findElements() method is not able to find the elements on the
webpage, then we get emptyList.
<html>
G.
<body>
<a href='https://demo.actitime.com/login.do'>actitime</a><br>
<a href='https://www.google.com/'>Google</a><br>
Ra
<a href='http://www.yuvadhwaja.in/'>Yuvadhwaja</a><br>
</body>
</html>
vi
package qsp;
import java.util.List;
import org.openqa.selenium.By;
Ki
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
r
import org.openqa.selenium.chrome.ChromeDriver;
an
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
CS
");
035
// System.out.println(text);
// }
}
}
Ra
vi
Ki
r an
_K
CS
A4
036
-----------
17-10-2020
-----------
WebDriver Interface
--------------------
S. findElement() findElements()
No
Synchronisation:
-----------------
Ki
2. Dynamic wait
037
Implicitlywait:
• Here no need to give any condition.
• It will wait for findElement() method and findElements() method
Syntax: driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
• here 20 means maximum time until control should wait before giving
NOSuchElementException
Note: implicitlywait statemet can be declared once at the top of the code
which can work for all the findElement() method / findElements() methods
----------------------------
Ra
vi
Ki
r an
_K
038
package qsp;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FlipkartSynchronisation {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
G.
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
Ra
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(2000);
vi
driver.findElement(By.xpath("//button[text()='✕']")).click();
Ki
driver.findElement(By.xpath("//span[text()='Cart']")).click();
r
// Thread.sleep(3000);
an
driver.findElement(By.xpath("//span[text()='Login']")).click();
}}
CS
A4
039
Explicitlywait:
• Here we need to give condition
• It will work for any method
• create an object of 'WebDriverWait' Class
condition...
• WebDriverWait class and ExpectedConditions Class is imported from
'org.openqa.selenium.support.ui' package
Ra
----------------------------
Ki
r an
_K
CS
A4
040
package qsp;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
driver.manage().window().maximize();
//Enter the test URL
driver.get("https://demo.actitime.com/login.do");
Ki
{
System.out.println("Login Page is Displayed, Test Step PASSED");
_K
}
else
{
CS
Thread.sleep(2000);
//Enter Valid Username in username textbox
driver.findElement(By.id("username")).sendKeys("admin");
Thread.sleep(2000);
//Enter Valid Password in password textbox
driver.findElement(By.name("pwd")).sendKeys("manager");
Thread.sleep(2000);
//Click on Login Button
driver.findElement(By.xpath("//div[text()='Login
']")).click();
041
// Thread.sleep(5000);
}
else
{
vi
042
----------- ------------
19-10-2020 && 20-10-2020
----------- ------------
ImplicitlyWait ExplicitlyWait
findElement()/findElements()
throughout the code
A4
043
package qsp;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
Ra
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
vi
// driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(2000);
driver.findElement(By.xpath("//button[text()='✕']")).click();
Ki
r
driver.findElement(By.xpath("//span[text()='Cart']")).click();
an
// Thread.sleep(3000);
_K
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath
CS
("//span[text()='Login']")));
driver.findElement(By.xpath("//span[text()='Login']")).click();
}}
044
1. Static Dropdown: Options of the dropdown are fixed
Two types of Static Dropdown
1. Single-Select Dropdown
2. Multi-Select Dropdown
Created by using
<select>, <option>
• Selection Methods
1. selectByVisibleText(String arg) : void
2. selectByValue(String arg) : void
3. selectByIndex(int index) : void
Ki
• DeSelection Methods
4. deselectByVisibleText(String arg) : void
r
• Operational Methods
8. isMultiple() : boolean
CS
9. getOptions() : List<WebElement>
10. getAllSelectedOptions() : List<WebElement>
11. getFirstSelectedOption() : WebElement
A4
Note:
1. selectByIndex() can handle duplicate options
2. When we try to use deselection methods on a single select dropdown,
then we get UnsupportedOperationException (Java)- (Runtime)
isMultiple() :
• Used to verify whether it is a Single Selectdropdown or multi-Select
Dropdown.
• ReturnType is boolean.
• Returns true if it is multi-Select dropdown.
045
getOptions() :
• Used to get the address of all the options present in the dropdown.
• ReturnType is List<WebElement>
• If no option present in dropdown then we get emptyList
getAllSelectedOptions() :
• Used to get the address of all the selected options
• ReturnType is List<WebElement>
• If no option is Selected in dropdown then we get emptyList
getFirstSelectedOption() :
G.
getWrappedElement() :
vi
• Used to get the address of all the options wrapped into a single
address
• ReturnType is WebElement Interface
• If no option present in dropdown then we get NoSuchElementException
Ki
Note:
r
<html>
_K
<body>
<select id='novotel' multiple>
<option value='a'>Idly</option>
CS
<option value='b'>Biryani</option>
<option value='c'>Vada</option>
<option value='d'>Dosa</option>
A4
package qsp;
046
import java.util.List;
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.support.ui.Select;
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
Ra
20Selenium/.html/MultiSelectDropdown.html");
Thread.sleep(3000);
Ki
// for(int i=0;i<allOptions.size();i++)
_K
// {
// System.out.println(allOptions.get(i).getText());
// sel.selectByIndex(i);
CS
// }
// sel.deselectAll();
A4
// for(int j=0;j<allOptions.size();j++)
// {
// sel.deselectByIndex(j);
// }
// for(int j=allOptions.size()-1;j>=0;j--)
// {
// sel.deselectByIndex(j);
// Thread.sleep(500);
// }
047
//for(start;end;incr/decr)
// for(int i=2;i<=5;i++)
// {
// sel.selectByIndex(i);
// }
// }
//sel.selectByVisibleText("Biryani");
Ki
// if(sel.isMultiple())
r
// {
// System.out.println("It is Multi-Select Dropdown");
an
// }
// else
_K
// {
// System.out.println("It is Single-Select Dropdown");
// }
CS
// sel.selectByValue("d");
A4
// sel.selectByVisibleText("Biryani");
// sel.selectByIndex(0);
// sel.selectByVisibleText("Vada");
// Thread.sleep(3000);
// sel.deselectByVisibleText("Vada");
// sel.deselectAll();
}
}
048
G.
Ra
vi
Ki
Exceptions
r
-----------
an
(or)
It is set incorrectly
CS
049
-----------
21-10-2020
-----------
G.
Ra
vi
package qsp;
import java.util.HashSet;
Ki
import java.util.LinkedHashSet;
import java.util.List;
r
import java.util.Set;
import java.util.TreeSet;
an
import org.openqa.selenium.By;
_K
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
CS
import org.openqa.selenium.support.ui.Select;
050
//print all the options as it is....
// WebElement ddAddr = driver.findElement(By.id("novotel"));
// Select s =new Select(ddAddr);
// List<WebElement> allOpts = s.getOptions();
//
// for(WebElement oneOpt:allOpts)
// System.out.println(oneOpt.getText());
//
// for(WebElement oneOpt:allOpts)
// {
vi
// for(String oneText:st)
// {
r
// System.out.println(oneText);
// }
an
_K
//
// for(WebElement oneOpt:allOpts)
// {
// String text = oneOpt.getText();
// st.add(text);
// }
//
// for(String oneText:st)
// {
// System.out.println(oneText);
// }
051
//Print options without Duplicate + Sorted Order
WebElement ddAddr = driver.findElement(By.id("novotel"));
Select s =new Select(ddAddr);
List<WebElement> allOpts = s.getOptions();
for(WebElement oneOpt:allOpts)
{
String text = oneOpt.getText();
st.add(text);
}
G.
for(String oneText:st)
{
Ra
System.out.println(oneText);
}
vi
}
}
Output:
Ki
Chai
052
-----------
22-10-2020
-----------
• Since it is not created by select tag, hence we can't use Select class
and its methods. If we use, we get UnexpectedTagNameException.
Ra
package qsp;
import java.util.List;
vi
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
Ki
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
r
import org.openqa.selenium.chrome.ChromeDriver;
org.openqa.selenium.chrome.ChromeOptions;
an
import
import org.openqa.selenium.support.ui.Select;
_K
System.setProperty("webdriver.chrome.driver",
A4
"./drivers/chromedriver.exe");
ChromeOptions co = new ChromeOptions();
co.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(co);
driver.manage().window().maximize();
driver.get("https://www.cleartrip.com/");
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
// Thread.sleep(3000);
WebElement ddAddr = driver.findElement(By.id("FromTag"));
ddAddr.sendKeys("del");
053
Thread.sleep(6000);
// ddAddr.sendKeys(Keys.DOWN);
// Thread.sleep(3000);
// ddAddr.sendKeys(Keys.DOWN);
// Thread.sleep(3000);
// ddAddr.sendKeys(Keys.ENTER);
List<WebElement> allOptions =
driver.findElements(By.xpath("//a[contains(@id,'ui-id')]"));
G.
System.out.println(allOptions.get(0).getText());
Thread.sleep(3000);
allOptions.get(allOptions.size()-1).click();
Ra
//System.out.println(allOptions.size());
vi
// System.out.println(allOptions.get(0).getText());
// for(WebElement op:allOptions)
// {
Ki
// System.out.println(op.getText());
// }
r
// for(int i = 0;i<allOptions.size();i++)
an
// {
// WebElement oneOpt = allOptions.get(i);
_K
}
}
A4
Assignment-1:
-------------
1. Go to Flipkart.com and type laptops in search textbox
2. Count and print the options of the dropdown and select 2nd option
3. In Search Results page, Apply filter
Max Price - 50000
Processor - Core i3
Brand - Dell
OS - Windows 10
4. Print the RAM and Price Details of the First laptop
054
package qsp;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
"./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
vi
driver.get("https://www.flipkart.com/");
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
Ki
driver.findElement(By.xpath("//button[text()='✕']")).click();
r
WebElement ddAddr1 =
an
driver.findElement(By.xpath("//input[@class='LM6RPg']"));
ddAddr1.sendKeys("laptops");
_K
List<WebElement> allOptions =
driver.findElements(By.xpath("//li[@class='_1va75j']"));
CS
System.out.println(allOptions.size());
for(WebElement oneOpt:allOptions)
A4
{
System.out.println(oneOpt.getText());
}
Thread.sleep(3000);
allOptions.get(1).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@class='_1YoBfV']/descendant::
option[@class='OMc8Rd' and @value='50000']")).click();
055
Thread.sleep(3000);
driver.findElement(By.xpath("//div[text()='Core
i3']/preceding-sibling::div[@class='_1p7h2j']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//div[text()='Dell']/preceding-sibli
ng::div[@class='_1p7h2j']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//div[text()='Operating
G.
System']")).click();
driver.findElement(By.xpath("//div[text()='Windows
Ra
10']/preceding-sibling::div[@class='_1p7h2j']")).click();
vi
List<WebElement> allPrizes =
driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
//System.out.println(allPrizes.size());
System.out.println(allPrizes.get(0).getText());
Ki
r
List<WebElement> allRams =
an
driver.findElements(By.xpath("//li[contains(text(),'RAM')]"));
//System.out.println(allRams.size());
_K
System.out.println(allRams.get(0).getText());
}
}
CS
Assignment-2:
-------------
A4
package qsp;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
056
public class Assignment2 {
WebElement Addr =
G.
driver.findElement(By.xpath("//input[@title='Search']"));
Addr.sendKeys("Qspiders");
Ra
Thread.sleep(3000);
List<WebElement> allOptions =
driver.findElements(By.xpath("//ul[@class='erkvQe']/descendant::li[con
vi
tains(@class,'sbct')]"));
System.out.println(allOptions.size());
Ki
Thread.sleep(3000);
r
for(WebElement everyOp:allOptions)
{
an
System.out.println(everyOp.getText());
}
_K
for(WebElement everyOp:allOptions)
CS
{
if(everyOp.getText().equals("qspiders reviews"))
{
A4
Thread.sleep(3000);
WebElement ReqOpt =
driver.findElement(By.xpath("//b[contains(text(),'reviews')]"));
ReqOpt.click();
}
}
}
}
057
-----------
26-10-2020
-----------
• In KeyEvent Class all the keyboard keys are stored as virtual keys
and since they are static final variables , we can access directly
ra
package qsp;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
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.interactions.Actions;
058
public class HandlingMouseAndKeyboardAction {
// System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
// WebDriver driver = new ChromeDriver();
// driver.manage().window().maximize();
// driver.get("https://www.flipkart.com/");
//
// Thread.sleep(5000);
G.
//
driver.findElement(By.xpath("//button[text()='✕']")).click();
Ra
//
// WebElement elec =
driver.findElement(By.xpath("//span[text()='Electronics']"));
vi
// Thread.sleep(5000);
// Actions a = new Actions(driver);
// a.moveToElement(elec).perform();
Ki
//
// Thread.sleep(3000);
ra
//
// driver.findElement(By.xpath("//a[text()='Power
n_
Banks']")).click();
KC
// System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
4
059
//Performing Drag and Drop Actions
// System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
// WebDriver driver = new ChromeDriver();
// driver.manage().window().maximize();
//
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/
demo-drag-drop-3.html");
//
// Thread.sleep(5000);
//
// WebElement src = driver.findElement(By.id("box7"));
G.
// ac.dragAndDrop(src, dest).perform();
vi
System.setProperty("webdriver.chrome.driver",
ra
"./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
n_
driver.manage().window().maximize();
driver.get("file:///C:/Users/Ravi%20Kiran/Downloads/Programs/.htm
KC
l/demo.html");
SA
Thread.sleep(3000);
WebElement google = driver.findElement(By.id("i2"));
4
}
}
060
-----------
27-10-2020
-----------
061
JavascriptExecutor Diagram
----------------------------
G.
Ra
vi
• window.scrollBy+location of Element
A4
Example:
WebElement ele =driver.findElement(By.xpath("//h2[text()='Selenium Level
Sponsors']"));
Point loc = ele.getLocation();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy"+loc);
062
package qsp;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
"./drivers/chromedriver.exe");
//upcasting from ChromeDriver class to WebDriver Interface
WebDriver driver = new ChromeDriver();
vi
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/downloads/");
Thread.sleep(3000);
Ki
WebElement ele =
r
System.out.println(loc);
_K
//Through typeCasting
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy"+loc);
CS
//Through Downcasting
// RemoteWebDriver rwd = (RemoteWebDriver)driver;
A4
// rwd.executeScript("window.scrollBy"+loc);
}
}
063
-----------
28-10-2020
-----------
First we need to handle it manually and then we pass the Javascript into
executeScript(script) method of JavascriptExecutor Interface
G.
Handling it Manually :-
1. Go to the required webpage and press F12 key(in some laptops Fn + F12),
Ra
064
j.executeScript("document.getElementById('i2').value='manager'");
(or)
2) Downcast from
WebDriver Interface to RemoteWebDriver Class
WebDriver driver = new ChromeDriver();
RemoteWebDriver rwd = (RemoteWebDriver)driver;
rwd.executeScript("document.getElementById('i2').value='manager'"
);
package qsp;
import org.openqa.selenium.By;
G.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Ra
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver","./drivers/chromedri
ver.exe");
r
driver.get("file:///C:/Users/Ravi%20Kiran/Downloads/Programs/.htm
_K
l/DisabledElement.html");
Thread.sleep(2000);
CS
untb.sendKeys("admin");
}
else {
System.out.println("Untb is not Enabled");
JavascriptExecutor j = (JavascriptExecutor)driver;
j.executeScript("document.getElementById('i1').value='admin'");
}
Thread.sleep(3000);
WebElement pwtb = driver.findElement(By.id("i2"));
if(pwtb.isEnabled()) {
System.out.println("Pwtb is Enabled");
065
pwtb.sendKeys("manager");
}
else {
System.out.println("Pwtb is not Enabled");
JavascriptExecutor j = (JavascriptExecutor)driver;
j.executeScript("document.getElementById('i2').value='manager'");
}
}}
1 click() void
2 clear() void
Ra
3 sendKeys(charSequence) void
vi
4 getTagname() String
6 getText() String
7 getLocation() Point
ra
8 getRect() Rectangle
n_
10 getSize() Dimesnion
CS
12 isDisplayed() boolean
A4
13 isEnabled() boolean
14 isSelected() boolean
17 submit() void
066
Method Name Method Use
pixel, Y pixel)
webpage
CS
067
package qsp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebElementMethods {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
G.
driver.manage().window().maximize();
driver.get("https://demo.actitime.com/login.do");
Thread.sleep(3000);
Ra
if(chkbox.isDisplayed())
{
System.out.println("Checkbox is Displayed");
if(chkbox.isEnabled())
Ki
{
System.out.println("Checkbox is Enabled");
ra
if(chkbox.isSelected())
{
System.out.println("It is already selected,
n_
dont touch");
}
else
K
{
CS
}
}
else
{
System.out.println("Checkbox is not Enabled");
}
}
else
{
System.out.println("Checkbox is not Displayed");
}
}}
068
-----------
29-10-2020
-----------
package qsp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebElementMethods {
public static void main(String[] args) throws InterruptedException
{
G.
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
Ra
Thread.sleep(2000);
untb.sendKeys("admin");
Thread.sleep(2000);
r
untb.clear();
System.out.println("Tag: "+untb.getTagName());
an
System.out.println("Attribute:
"+untb.getAttribute("class"));
_K
System.out.println("Size: "+untb.getSize());
System.out.println("Width: "+untb.getRect().width);
A4
System.out.println("Height: "+untb.getRect().height);
System.out.println("Location: "+untb.getLocation());
System.out.println("Font-Size: "+untb.getCssValue("font-size"));
}
}
069
Output:
Tag: input
Attribute: textField
Text: Please identify yourself
Size: (214, 32)
Width: 214
Height: 32
Location: (644, 294)
X axis: 644
Y axis: 294
Font-Size: 14px
G.
TakesScreenshot
----------------
Ra
vi
Ki
r an
_K
CS
A4
070
Hence, to access it,
• we either typecast from WebDriver Interface to TakesScreenshot
Interface
TakesScreenshot ts = (TakesScreenshot)driver;
(or)
• We Downcast from WebDriver Interface to RemoteWebDriver Class
RemoteWebDriver rwd = (RemoteWebDriver)driver;
(or)
• Since, EventFiringWebDriver Class is also Implementing it, we can
Create object and access it
EventFiringWebDriver e = new EventFiringWebDriver(driver);
G.
package qsp;
import java.io.File;
CS
import java.io.IOException;
A4
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.google.common.io.Files;
071
public class TakingScreenshots {
// File ss = ts.getScreenshotAs(OutputType.FILE);
// //path+name+extension
// File f = new File("C:<\\Users\\Ravi Kiran\\Downloads>
Ra
\\\\screenshots\\Google.png");
// Files.copy(ss, f);
vi
// Files.copy(src, dest);
an
\\\\screenshots\\GImage.jpg");
// Files.copy(src, dest);
A4
\\\\screenshots\\GLogo.jpg");
Files.copy(src, dest);
}
}
072
-----------
30-10-2020
-----------
POPUPS
--------
Popups usually appear on the webpage to give some warning or get some data
from users or get confirmation from users.....
WEB-BASED
1. Javascript popup
Ra
2. Hidden-Division popup
3. Browser Notification popup
vi
1. Is it colorful ? NO
2. Is it movable ? NO
_K
3. Is it Inspectable? NO
4. Until we handle it, we can't do futher action on the webpage
CS
Note: If there is no alert popup on the webpage, but still we are trying
to switch to it and handle it, then we get NoAlertPresentException
073
• for Alert Popup
<html>
<body>
<h2>JavaScript Alert</h2>
Username<input type='text'><br>
Password<input type='password'><br>
<button onclick='myFunction()'>Login</button>
<script>
function myFunction() {
alert('I am an alert box!');
}
G.
</script>
</body>
</html>
Ra
<body>
<p>Click the button to display your Love.....</p>
<button onclick="myFunction()">I Love You</button>
<p id="demo">You Pressed OK...Now you have to Love..haha..!</p>
Ki
<script>
function myFunction() {
r
var txt;
var r = confirm("Will You Marry me ???");
an
if(r == true) {
txt = "You Pressed Ok, so you should marry...." ;
_K
} else {
txt = "You Pressed Cancel, Now you are safe...." ;
}
CS
document.getElementById("demo").innerHTML = txt;
}
</script>
A4
</body>
</html>
package qsp;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
074
public static void main(String[] args) throws InterruptedException
{
// Thread.sleep(3000);
//
driver.findElement(By.xpath("//button[text()='Login']")).click();
Ra
// Thread.sleep(3000);
// Alert al = driver.switchTo().alert();
// System.out.println(al.getText());
vi
// al.accept();
// //al.dismiss();
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
r
driver.get("file:///C:/Users/Ravi%20Kiran/Downloads/Programs/.htm
_K
l/Confirmation%20Popup.html");
Thread.sleep(3000);
driver.findElement(By.xpath("//button[text()='I Love You']")).click();
CS
Alert al = driver.switchTo().alert();
Thread.sleep(3000);
A4
System.out.println(al.getText());
// //OK button
// al.accept();
//CANCEL button
al.dismiss();
}
}
075
-----------
31-10-2020
-----------
2. Hidden Division Popup
• Characteristics:
1. Is it Colorful? Yes
2. Is it Movable? No
3. Is it Inspectable? Yes
How to handle it ?
• Since it is inspectable, we use fndElement(), findElements(),
click(), sendKeys() etc to handle it.
G.
called addArguments("settings")
FirefoxOptions fo = new FirefoxOptions();
fo.addArguments("--disable-notifications");
n_
fo.addArguments("start-maximized");
K
package qsp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
076
//Handling Hidden Division Popup
//Handling Browser Notifications Popup
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
ChromeOptions co = new ChromeOptions();
co.addArguments("--disable-notifications");
co.addArguments("start-maximized");
//Open Chrome browser with the settings
WebDriver driver = new ChromeDriver(co);
//driver.manage().window().maximize();
driver.get("https://www.cleartrip.com/");
G.
Thread.sleep(3000);
driver.findElement(By.id("DepartDate")).click();
Ra
Thread.sleep(3000);
driver.findElement(By.xpath("//td[@datamonth='10']//a[text()='4']")).c
lick();
vi
}
}
WINDOW-BASED POPUP
Ki
• Characteristics:
1. It is Colorful
2. It is movable
n_
3. It is not Inspectable
• Examples:
K
4. FileUpload
5. FileDownload
CS
6. Print
A4
077
Note: No need to handle download popup in chrome browser, because it will
not show popup but it auto-downloads any file.
package qsp;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
G.
System.setProperty("webdriver.gecko.driver","./drivers/geckodriver.exe
");
WebDriver driver = new FirefoxDriver();
Ki
driver.manage().window().maximize();
driver.get("https://www.selenium.dev/downloads/");
r
Thread.sleep(3000);
an
driver.findElement(By.xpath("//td[text()='Java']/..//a[text()='Do
_K
wnload']")).click();
Thread.sleep(3000);
Robot r = new Robot();
CS
r.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(3000);
r.keyPress(KeyEvent.VK_ENTER);
A4
r.keyRelease(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_ENTER);
}
}
078
-----------
02-11-2020
-----------
AutoIT
-------
• AutoIT is a free third party tool, used to automate window based
applications.
• It can be downloaded from
https://www.autoitscript.com/site/autoit/downloads/
Download AutoIT Zip file
• In AutoIT we have two tools :-
G.
drop on the required control. We will get all the attributes details
related of that control in Window Info tool.
vi
Note:
Control ID = combination of Class and Instance
Class='Button'
_K
ID='10'
Control Id = 'Button10'
CS
• All the automation of window popup will be done through autoIT and
we get an executable file. But we just need to execute that. Hence
we use 'exec("full path of exe file")' of Runtime Class to
execute(run) it.
079
G.
Ra
package qsp;
import java.awt.AWTException;
import java.awt.Robot;
vi
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
Ki
System.setProperty("webdriver.gecko.driver",
an
"./drivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
_K
driver.get("https://www.selenium.dev/downloads/");
CS
Thread.sleep(3000);
//Getting Window Popup(Print popup)
Robot r = new Robot();
A4
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_P);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_P);
Thread.sleep(3000);
//Executing the autoIT Script
//Integrating AutoIT Script with our selenium script
Runtime.getRuntime().exec("E:\\Qspiders
Selenium\\autoIT\\Print.exe");
}}
080