Skip to content

Commit 604949c

Browse files
authored
Add files via upload
1 parent 0ae1a93 commit 604949c

29 files changed

+1211
-0
lines changed

Day_1/main.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.chrome.service import Service
3+
from selenium.webdriver.common.by import By
4+
from webdriver_manager.chrome import ChromeDriverManager
5+
import os
6+
7+
# Service is the location of chrome driver
8+
9+
ser_obj = Service("C:\Drivers\chromedriver\chromedriver.exe")
10+
driver = webdriver.Chrome(service=ser_obj)
11+
12+
# Environment Setup
13+
# os.environ['PATH'] += r"C:\Drivers\chromedriver_win32"
14+
15+
# visiting website
16+
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
17+
18+
# used implicit wait so that the website can properly load and then the driver perform other actions
19+
driver.implicitly_wait(5)
20+
21+
# finding the username and password field using XPATH (this is Absolute/full XPATH)
22+
# Ab.XPATH uses nodes and tags only
23+
# (Ab.XPATH start from root node ie /Html) ((starts with /)
24+
25+
driver.find_element(By.XPATH, '/html/body/div/div[1]/div/div[1]/div/div[2]/div[2]/form/div[1]/div/div[2]/input').send_keys('Admin')
26+
driver.find_element(By.XPATH, '/html/body/div/div[1]/div/div[1]/div/div[2]/div[2]/form/div[2]/div/div[2]/input').send_keys('admin123')
27+
28+
# find element using class name
29+
driver.find_element(By.CLASS_NAME, "orangehrm-login-button").click()
30+
31+
# this is Relative/Partial XPATH (starts with //)
32+
# Rel.XPATH must contain an Attribute
33+
driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div[1]/aside/nav/div[2]/ul/li[10]/a/span').click()
34+
35+
# find element using LinkText
36+
driver.implicitly_wait(2)
37+
38+
driver.find_element(By.LINK_TEXT, "OrangeHRM, Inc").click()
39+
40+
# driver.find_element(By.PARTIAL_LINK_TEXT, "OrangeHRM").click()
41+
42+
# after login matching the title of the website
43+
exp_title = driver.title
44+
act_title = 'OrangeHRM'
45+
if exp_title == act_title:
46+
print("test case passed!")
47+
else:
48+
print("test case failed!")
49+
driver.close()
50+
# my_element = driver.find_element(By.CLASS_NAME, "orangehrm-login-button")
51+

Day_10/broken_links_handling.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import requests as requests
2+
from selenium import webdriver
3+
from selenium.webdriver.chrome.service import Service
4+
from selenium.webdriver.common.by import By
5+
6+
options = webdriver.ChromeOptions()
7+
options.add_experimental_option("detach", True)
8+
9+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
10+
driver = webdriver.Chrome(options=options, service=ser_obj)
11+
driver.get('http://www.deadlinkcity.com/')
12+
13+
# for handling broken links we need to install 'requests' package/module.
14+
# broken links does not have any target page
15+
# we need to identify which one is broken link and which one is normal
16+
# broken links response is always equal to or greater than 400
17+
18+
# first capture all links
19+
all_links = driver.find_elements(By.TAG_NAME, 'a')
20+
count = 0 # counter for broken links
21+
count1 = 0 # counter for normal links
22+
for link in all_links:
23+
url = link.get_attribute('href') # get all the URL of the links
24+
try: # during the request-response process network error may occur. good practice to use try-catch
25+
response = requests.head(url) # hit the server with the url and store the response
26+
except:
27+
None # means ignore all exception
28+
29+
# one of the things which comes with response is the status code.
30+
if response.status_code >= 400:
31+
print(url, 'broken url')
32+
count += 1
33+
else:
34+
print(url, 'valid link')
35+
count1 += 1
36+
37+
print('total no of broken links: ', count)
38+
print('total no of normal links: ', count1)

Day_10/dropdown.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import time
2+
3+
from selenium import webdriver
4+
from selenium.webdriver.chrome.service import Service
5+
from selenium.webdriver.common.by import By
6+
from selenium.webdriver.support.select import Select
7+
8+
options = webdriver.ChromeOptions()
9+
options.add_experimental_option("detach", True)
10+
11+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
12+
driver = webdriver.Chrome(options=options, service=ser_obj)
13+
driver.get('https://testautomationpractice.blogspot.com/')
14+
15+
# dropdown has tag name 'select'. and it is a single web element
16+
# inside the select tag we have multiple tag names called 'options' which hold text or values
17+
18+
# first identify the dropdown and store it
19+
dropdown_element = driver.find_element(By.XPATH, "//select[@id='country']")
20+
# now we can't access the option directly in a dropdown so, we have to use the built_in select module in a package
21+
# we will pass dropdown element to the select module in order to access its options.
22+
23+
# option_element = Select(driver.find_element(By.XPATH, "//select[@id='country']")) # we can do this as well
24+
# Note: select method is only for the 'select' tag.sometimes we have 'div' or some other tag for that we will use XPATH
25+
26+
opt_element = Select(dropdown_element) # object of select class (use it for accessing option elements)
27+
opt_element.select_by_visible_text('Canada') # case Sensitive: provide exactly the same option text otherwise error
28+
opt_element.select_by_value('usa') # extra the value attribute value from HTML DOM and select the option via it
29+
opt_element.select_by_index(3) # select the value using index.you have to find the index manually.don't write it in db.commas as it is index number
30+
31+
# capture all the options and catch them. we have 2 approaches for that
32+
# 1: write a common XPATH and capture option
33+
# 2: use built in methode 'option' and capture all the option inside the dropdown
34+
# you can use child::option , or you can just use /option and, it will select all the option
35+
# capture_opt_xp = driver.find_elements(By.XPATH, "//select[@id='country']/child::option")
36+
capture_opt_xp = driver.find_elements(By.XPATH, "//select[@id='country']/option")
37+
38+
for cop in capture_opt_xp:
39+
print(cop.text)
40+
41+
builtin_op = opt_element.options # this will return all the option elements inside the dropdown
42+
for cop in builtin_op:
43+
print(cop.text)
44+
45+
# time.sleep(3)
46+
# if I want to print a specific option without using builtin function ie 'select_by_value' or select_by_index etc ...
47+
for cop in capture_opt_xp:
48+
if cop.text == 'France':
49+
cop.click()
50+
break # once the element is found then there is no need to look for other elements so 'break' it
51+
52+
# suppose instead of select tag there is div or button or some other tag.how to find option inside the dropdown
53+
# we will write a Xpath for that and then we can store all the element and do whatever we want
54+
55+
opt_len = driver.find_elements(By.XPATH, "//*[@id='country']/option")
56+
print('total number of option are :', len(opt_len))

Day_10/links.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# links are web elements. there are 3 types of links
2+
# 1: internal -- will navigate to the same page
3+
# 2: external -- opens a new tab and will navigate to some other page
4+
# 3: broken -- does not have any target page
5+
6+
from selenium import webdriver
7+
from selenium.webdriver.chrome.service import Service
8+
from selenium.webdriver.common.by import By
9+
10+
options = webdriver.ChromeOptions()
11+
options.add_experimental_option("detach", True)
12+
13+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
14+
driver = webdriver.Chrome(options=options, service=ser_obj)
15+
driver.get('https://testautomationpractice.blogspot.com/')
16+
17+
# find the total no of links. we will find something which is common in all the links and, it is the tag name ie 'a'
18+
links = driver.find_elements(By.TAG_NAME, 'a')
19+
# links = driver.find_elements(By.XPATH, '//a')
20+
print('total no of links: ', len(links))
21+
22+
# print all the link names
23+
for link in links:
24+
print(link.text)

Day_11/alert_popups.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# alert and popups are not web elements, and we can't identify any element on the alert window
2+
3+
import time
4+
from selenium import webdriver
5+
from selenium.webdriver.chrome.service import Service
6+
from selenium.webdriver.common.by import By
7+
8+
options = webdriver.ChromeOptions()
9+
options.add_experimental_option("detach", True)
10+
11+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
12+
driver = webdriver.Chrome(options=options, service=ser_obj)
13+
driver.get('https://the-internet.herokuapp.com/javascript_alerts')
14+
15+
# opens alert window
16+
driver.find_element(By.XPATH, "//button[normalize-space()='Click for JS Prompt']").click()
17+
# you can use text() instead of normalize-space(). they are the same
18+
# the only difference is that if there are additional spaces in the text it will be ignored
19+
time.sleep(3)
20+
21+
alert_window = driver.switch_to.alert # switch to alert window
22+
# as we can't identify the alert window using locators so, we will use builtin methods
23+
# print(alert_window.text)
24+
alert_window.send_keys('my name is fazle yazdan')
25+
alert_window.accept() # close the alert window by clicking the 'ok' button. and we use it mostly
26+
# alert_window.dismiss() # close the alert window by clicking the cancel button
27+

Day_11/authentication_popups.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# a.popups are not common and are only used inside the company or corporation
2+
# they are not part of the app and is imposed on top of the app to authorize you before accessing it.
3+
# there when you want to access there utilities on their app. most likely you will have to Log in via auth.popup
4+
# the only you can automate this is to bypass this.
5+
# we can bypass it by providing the username and password inside the url. this is also called injecting
6+
7+
import time
8+
from selenium import webdriver
9+
from selenium.webdriver.chrome.service import Service
10+
from selenium.webdriver.common.by import By
11+
12+
options = webdriver.ChromeOptions()
13+
options.add_experimental_option("detach", True)
14+
15+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
16+
driver = webdriver.Chrome(options=options, service=ser_obj)
17+
# driver.get('https://the-internet.herokuapp.com/digest_auth')
18+
19+
# syntax : http:// username:password @ test.com
20+
driver.get('https://admin:[email protected]/digest_auth') # injecting
21+

Day_11/browser_window.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# switching between different browser windows
2+
# to switch bw windows we will use driver.switch_to.window(). in the parenthesis we will pass the browser window id.
3+
# now we can't see the window id in HTML dom. it is dynamic and is created when the browser window opens
4+
# driver.current_window_handle : return windowID of single browser window
5+
# driver.window_handles : return window ID's of multiple browsers windows
6+
import time
7+
from selenium import webdriver
8+
from selenium.webdriver.chrome.service import Service
9+
from selenium.webdriver.common.by import By
10+
11+
options = webdriver.ChromeOptions()
12+
options.add_experimental_option("detach", True)
13+
14+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
15+
driver = webdriver.Chrome(options=options, service=ser_obj)
16+
driver.get('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login')
17+
18+
currentWindowId = driver.current_window_handle # return id of the current window id
19+
print(currentWindowId)
20+
driver.implicitly_wait(3)
21+
driver.find_element(By.LINK_TEXT, 'OrangeHRM, Inc').click() # opens another browser window
22+
23+
allWindowIDs = driver.window_handles # return IDs of all browser window in a list collection
24+
25+
# Approach 1:
26+
# parentWindow = allWindowIDs[0] # browser ID of the parent/main window
27+
# childWindow = allWindowIDs[1] # browser ID of the child window
28+
# print(parentWindow, childWindow)
29+
#
30+
# driver.switch_to.window(childWindow)
31+
# print('title of the child window: ', driver.title)
32+
#
33+
# driver.switch_to.window(parentWindow)
34+
# print('title of the parent window: ', driver.title)
35+
36+
# Approach 2:
37+
# the above approach is suitable for 2,3 browser windows. suppose there are 10 browser windows then we can't store title of every window
38+
# for that we will use for loop to get the title of different windows
39+
# print the title after switching to the other window
40+
41+
# print('printing title of windows using approach 2')
42+
# for winId in allWindowIDs:
43+
# driver.switch_to.window(winId)
44+
# print(driver.title)
45+
46+
# closing specific browser window (all you need to do is to provide the title of that browser window)
47+
time.sleep(3)
48+
for winId in allWindowIDs:
49+
driver.switch_to.window(winId)
50+
if driver.title == 'OrangeHRM':
51+
driver.close()
52+
53+

Day_11/frames_iframes.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frames: when we want to embed something from the 3rd party into our website they are called Iframes.
2+
# for example : if i want to embed google map into my website then I will use Iframes for that.
3+
# when working with windows we call it frames and when working with web we call it Iframes
4+
# tag name for frame: 'frame' , 'Iframe' , 'form': they all represents frame
5+
6+
from selenium import webdriver
7+
from selenium.webdriver.chrome.service import Service
8+
from selenium.webdriver.common.by import By
9+
10+
options = webdriver.ChromeOptions()
11+
options.add_experimental_option("detach", True)
12+
13+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
14+
driver = webdriver.Chrome(options=options, service=ser_obj)
15+
driver.get('https://www.selenium.dev/selenium/docs/api/java/index.html?org/openqa/selenium/WebDriver.html')
16+
17+
# webdriver cannot directly identify elements on Iframes. to tackle this issue we have to switch to Iframes first.
18+
# inside the parenthesis we have to pass either name or id of the frame or, we can pass a frame as a web element
19+
# if there is only 1 frame inside the web then good approach is to pass the index of the frame 0. ie: driver.switch_to.frame(0)
20+
# webdriver cannot directly switch from one frame to another and it will give an exception...
21+
# when you switch from main page to frame the driver is focused on that frame.now to switch to another frame...
22+
# we have to go back to the main page and then from the main page switch to another frame.
23+
# for that we will use driver.switch_to.default_content()
24+
25+
driver.switch_to.frame('packageListFrame')
26+
driver.find_element(By.LINK_TEXT, 'org.openqa.selenium').click()
27+
driver.switch_to.default_content() # go back to the main page
28+
29+
driver.switch_to.frame('packageFrame')
30+
driver.find_element(By.LINK_TEXT, 'WebDriver').click()
31+
driver.switch_to.default_content()
32+
33+
34+
driver.switch_to.frame('classFrame')
35+
driver.find_element(By.XPATH, '/html/body/header/nav/div[1]/div[1]/ul/li[8]/a').click()

Day_11/inner_frame.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frame inside another frame is called inner frame.
2+
# now to access elements inside the inner frame we have to switch to frame.
3+
# there is no need to use default_content() because the inner frame is inside the outer frame which we have accessed.
4+
5+
6+
from selenium import webdriver
7+
from selenium.webdriver.chrome.service import Service
8+
from selenium.webdriver.common.by import By
9+
10+
options = webdriver.ChromeOptions()
11+
options.add_experimental_option("detach", True)
12+
13+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
14+
driver = webdriver.Chrome(options=options, service=ser_obj)
15+
driver.get('https://demo.automationtesting.in/Frames.html')
16+
17+
driver.find_element(By.XPATH, "//a[normalize-space()='Iframe with in an Iframe']").click()
18+
outerFrame = driver.find_element(By.XPATH, '//*[@id="Multiple"]/iframe')
19+
driver.switch_to.frame(outerFrame) # passed frame as a web element
20+
21+
innerFrame = driver.find_element(By.XPATH, "/html/body/section/div/div/iframe")
22+
driver.switch_to.frame(innerFrame)
23+
driver.find_element(By.XPATH, "//input[@type='text']").send_keys('i found you!')
24+
25+
# if you want to switch back to the parent Iframe there is a special command for it.
26+
driver.switch_to.parent_frame()

Day_11/task_browser_window.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.chrome.service import Service
3+
from selenium.webdriver.common.by import By
4+
5+
options = webdriver.ChromeOptions()
6+
options.add_experimental_option("detach", True)
7+
8+
ser_obj = Service("C:\Drivers\chromedriver_win32\chromedriver.exe")
9+
driver = webdriver.Chrome(options=options, service=ser_obj)
10+
driver.get('https://testautomationpractice.blogspot.com/')
11+
12+
inputData = driver.find_element(By.XPATH, "//input[@id='Wikipedia1_wikipedia-search-input']")
13+
inputData.send_keys('selenium')
14+
15+
driver.find_element(By.XPATH, "//input[@type='submit']").click()
16+
driver.implicitly_wait(5) # for handling synchronization errors
17+
18+
# capture all links and loop through it so that we can click on each link and capture the browser window IDs
19+
allChild = driver.find_elements(By.XPATH, "//div[@id='Wikipedia1_wikipedia-search-results']//child::*//a")
20+
for child in allChild:
21+
child.click()
22+
23+
allWindowIDs = driver.window_handles # capture all browser windows IDs
24+
for winIDs in allWindowIDs:
25+
driver.switch_to.window(winIDs)
26+
print(driver.title)
27+
28+
driver.quit() # quit all browser windows at once

0 commit comments

Comments
 (0)