Skip to content

Commit 511068b

Browse files
committed
automating screenshots, cookies , tabsandwindows
1 parent 3b058e7 commit 511068b

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

Day_16/handling_cookies-4.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# when you visit an app or site, there are some things which it remembers
2+
#* like password , keywords etc. it is because of the cookies
3+
#! now through automation we can find wether a site is using cookies or not and what kind of info it stores
4+
#* there are cookie attributes and values we can also extract that. e.g cookies name, expiry date etc.
5+
#* we can also check how many cookies are created after visiting the app
6+
#* we can also delete specific cookies or all cookies as well
7+
#! cookies are dynamic and they are not always the same number, every time you visit the site there is chance that a cookie is changed
8+
#! remember a cookie is not a web element. it is an object having attributes with key value pairs
9+
#* so when capturing a cookie you have to store it in dictionary.one dictionary obj represents one cookie
10+
11+
import time
12+
from selenium import webdriver
13+
from selenium.webdriver import ActionChains
14+
from selenium.webdriver.chrome.service import Service
15+
from selenium.webdriver.common.by import By
16+
17+
18+
options = webdriver.ChromeOptions()
19+
options.add_experimental_option("detach", True)
20+
21+
ser_obj = Service("C:\Drivers\chromedriver\chromedriver.exe")
22+
driver = webdriver.Chrome(options=options, service=ser_obj)
23+
24+
driver.implicitly_wait(3)
25+
driver.get("https://demo.nopcommerce.com/")
26+
driver.maximize_window()
27+
28+
#* capturing default cookies created by the browser
29+
cookies = driver.get_cookies()
30+
print ("size of cookies : ", len(cookies)) #5
31+
32+
33+
# for c in cookies:
34+
# print(c) #* print all cookies
35+
# print(c.get('name')) #* print names of cookies
36+
# print(c.get('name'), ":", c.get('value')) #* print names and value of cookies
37+
38+
#! adding new cookie or user defined cookie to the browser.
39+
driver.add_cookie({"name" : "MyCookie", "value" : "FY777"}) #* you can add as many attributes you want
40+
cookies = driver.get_cookies()
41+
print ("size of cookies after adding a cookie : ", len(cookies)) #* sometimes after creating a cookie size remain the same. that means your app or site is not allowing you to create a cookie
42+
43+
#! deleting a cookie
44+
driver.delete_cookie("MyCookie")
45+
cookies = driver.get_cookies()
46+
print ("size of cookies after deleting a cookie : ", len(cookies))
47+
48+
driver.delete_all_cookies()
49+
cookies = driver.get_cookies()
50+
print ("size of cookies after deleting all cookies : ", len(cookies))
51+
52+
53+
time.sleep(3)
54+
driver.close()

Day_16/headless_testing-5.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# headless testing means testing without seeing the UI
2+
#* in other words testing is executed in the backend and you will see results on terminal
3+
#* normally what happens is, when executing test cases we see the ui
4+
#! PROS: in headless mode the script is running in backend and the screen is not occupied and you can do other activities as well
5+
#* script execution is faster in headless mode (performance is increased)
6+
#! CONS: suppose you newly joined a company and you want to know app funvtionality....
7+
#* now we know that there is no UI actions involved and everything is running in backend...
8+
#* so in essence you are not learning anything specially the functionality of an app.....
9+
#! headless testing is different in different browser
10+
11+
from selenium import webdriver
12+
13+
def headless_chrome(): #! for chrome browser
14+
from selenium.webdriver.chrome.service import Service
15+
ser_obj= Service("C:\Drivers\chromedriver\chromedriver.exe")
16+
options = webdriver.ChromeOptions()
17+
options.add_argument('--headless') #! for headless mode
18+
# options.add_experimental_option("detach", True)
19+
driver = webdriver.Chrome(service = ser_obj, options = options)
20+
return driver
21+
22+
# def headless_edge(): #! for Edge browser
23+
# from selenium.webdriver.edge.service import Service
24+
# ser_obj= Service("C:\Drivers\edgedriver\chromedriver.exe")
25+
# options = webdriver.EdgeOptions()
26+
# options.add_argument('--headless') #! for headless mode
27+
# # options.add_experimental_option("detach", True)
28+
# driver = webdriver.Edge(service = ser_obj, options = options)
29+
# return driver
30+
31+
32+
# driver = headless_edge()
33+
driver = headless_chrome()
34+
driver.get("https://demo.nopcommerce.com/")
35+
print("Title :", driver.title)
36+
print("URL :", driver.current_url)
37+
38+
driver.close()
39+
40+
41+

Day_16/tabsand_windows-3.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# scenario: i visited a page and click on the register...
2+
# normally what happens is that it open the register in the same page....
3+
#! i want to open the registeration in a different page and then switch to it without losing the homepage
4+
#* we have a shortchut in chrome browser ctrl+enter to open a link in a new tab
5+
#! now this shortcut can be different for different browsers
6+
7+
8+
import time
9+
from selenium import webdriver
10+
from selenium.webdriver import ActionChains
11+
from selenium.webdriver.chrome.service import Service
12+
from selenium.webdriver.common.keys import Keys
13+
from selenium.webdriver.common.by import By
14+
15+
16+
options = webdriver.ChromeOptions()
17+
options.add_experimental_option("detach", True)
18+
19+
ser_obj = Service("C:\Drivers\chromedriver\chromedriver.exe")
20+
driver = webdriver.Chrome(options=options, service=ser_obj)
21+
22+
driver.implicitly_wait(3)
23+
# driver.get("https://demo.nopcommerce.com/")
24+
# driver.maximize_window()
25+
26+
# newtab = Keys.CONTROL+Keys.RETURN #* Return Refers to Enter
27+
# driver.find_element(By.LINK_TEXT, "Register").send_keys(newtab) #! so we are passing the keys to open this in newtab
28+
29+
30+
#* Method 2: in this method we can open a new tab through a command avail in selenium 4
31+
# driver.get("https://demo.nopcommerce.com/")
32+
# driver.switch_to.new_window('tab')
33+
# driver.get("https://www.dummyticket.com/")
34+
35+
#* now to open a tab in in new window specify window in place of tab
36+
driver.get("https://demo.nopcommerce.com/")
37+
driver.switch_to.new_window('window')
38+
driver.get("https://www.dummyticket.com/")
39+
40+
41+
time.sleep(3)
42+
driver.close()

0 commit comments

Comments
 (0)