-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText
More file actions
264 lines (202 loc) · 11.5 KB
/
Text
File metadata and controls
264 lines (202 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#XPATH:
To create any xpath format is //tagname[@attribute='value']
if the html code is <input class="btn btn-success" type="submit" value="Submit">
then we can create xpath "//input[@type='submit']"
Note: If there are multiple element with same xpath then
suppose 3 elements are there with same xpath and you want 3rd one then
(//tagname[@attribute='value'])[3] it will hit the 3rd one
__________________________________________________________________________________
#XPATH when no attribute is given - have to travel from parent to child:
format is
<form _ngcontent-kod-c46="" novalidate="" class="ng-pristine ng-invalid ng-touched">
<div _ngcontent-kod-c46="" class="form-group mt-2 mb-2"><label _ngcontent-kod-c46="" for="userEmail" class="mt-1">Email</label>
<input _ngcontent-kod-c46="" type="email" formcontrolname="userEmail" placeholder="Enter your email address" class="form-control form-control-sm ng-pristine ng-invalid ng-touched"><!----></div>
<div _ngcontent-kod-c46="" class="form-group"><label _ngcontent-kod-c46="" for="password">Password</label>
<input _ngcontent-kod-c46="" type="password" formcontrolname="userPassword" id="userPassword" placeholder="Passsword" class="form-control ng-untouched ng-pristine ng-invalid"><!----></div>
<div _ngcontent-kod-c46="" class="form-group"><label _ngcontent-kod-c46="" for="confirmPassword">Confirm Password</label>
<input _ngcontent-kod-c46="" type="password" formcontrolname="confirmPassword" id="confirmPassword" placeholder="Confirm Passsword" class="form-control ng-untouched ng-pristine ng-invalid"><!----></div>
<div _ngcontent-kod-c46="" class="d-flex justify-content-center"><button _ngcontent-kod-c46="" type="submit" class="btn btn-custom btn-block" style="background-color: #96161f; color: white;">Save New Password</button></div>
<div _ngcontent-kod-c46="" class="d-flex justify-content-center"><a _ngcontent-kod-c46="" class="text-reset" href="/client/auth/login">Login</a> <a _ngcontent-kod-c46="" class="text-reset" href="/client/auth/register">Register</a></div></form>
In above form form is parent and div's are child.
Then the xpath wil be for input for div 1 is
//form/div[1]/input (for enter mail id)
__________________________________________________________________________________
#XPATH based on text
//button[text()='PROCEED TO CHECKOUT']
from html
<button type="button" class=" " xpath="1">PROCEED TO CHECKOUT</button>
__________________________________________________________________________________
#XPATH when no attribute is given only text is present:
then we will create xpath by using
<button _ngcontent-kod-c46="" type="submit" class="btn btn-custom btn-block" style="background-color: #96161f;
color: white;">Save New Password</button>
//button[text()='Save New Password'] (xpath using text)
__________________________________________________________________________________
#CSS SELECTOR:
To create any css selector format is tagname[attribute='value']
if the html code is <input class="btn btn-success" type="submit" value="Submit">
then we can create css selector "input[type='submit']"
note: #id will turnout to be css selector ex- #radiobutton
.class name will turnout to be css selector ex- .alert-success
__________________________________________________________________________________
#CSS SELECTOR when no attribute is given - have to travel from parent to child:
format is
<form _ngcontent-kod-c46="" novalidate="" class="ng-pristine ng-invalid ng-touched">
<div _ngcontent-kod-c46="" class="form-group mt-2 mb-2"><label _ngcontent-kod-c46="" for="userEmail" class="mt-1">Email</label>
<input _ngcontent-kod-c46="" type="email" formcontrolname="userEmail" placeholder="Enter your email address" class="form-control form-control-sm ng-pristine ng-invalid ng-touched"><!----></div>
<div _ngcontent-kod-c46="" class="form-group"><label _ngcontent-kod-c46="" for="password">Password</label>
<input _ngcontent-kod-c46="" type="password" formcontrolname="userPassword" id="userPassword" placeholder="Passsword" class="form-control ng-untouched ng-pristine ng-invalid"><!----></div>
<div _ngcontent-kod-c46="" class="form-group"><label _ngcontent-kod-c46="" for="confirmPassword">Confirm Password</label>
<input _ngcontent-kod-c46="" type="password" formcontrolname="confirmPassword" id="confirmPassword" placeholder="Confirm Passsword" class="form-control ng-untouched ng-pristine ng-invalid"><!----></div>
<div _ngcontent-kod-c46="" class="d-flex justify-content-center"><button _ngcontent-kod-c46="" type="submit" class="btn btn-custom btn-block" style="background-color: #96161f; color: white;">Save New Password</button></div>
<div _ngcontent-kod-c46="" class="d-flex justify-content-center"><a _ngcontent-kod-c46="" class="text-reset" href="/client/auth/login">Login</a> <a _ngcontent-kod-c46="" class="text-reset" href="/client/auth/register">Register</a></div></form>
In above form form is parent and div's are child.
Then the xpath wil be for input for div 2 is
form div:nth-child(2) input (for enter password) (just like xpath only have to remove / and give space)
__________________________________________________________________________________
#DROPDOWN for Static dropdown
first we have to import Select class,
then make a variable for dropdown id
then dropdown.select_by_index(1) or value or visible text
dropdown = Select(driver.find_element(By.ID,"exampleFormControlSelect1"))
dropdown.select_by_index(1)
__________________________________________________________________________________
#DROPDOWN for Dynamic dropdown
If option are changing as per the prompt
After giving 'Ind' 3 option appears
Take attribute of all the option:
options are like below->
<li class="ui-menu-item" role="presentation" css="1" style=""><a id="ui-id-90" class="ui-corner-all" tabindex="-1">British Indian Ocean Territory</a></li>
create css selector for common elements
driver.find_elements(By.CSS_SELECTOR, "li[class='ui-menu-item']")
then extract the text and itterate from for loop for the desired prompt
__________________________________________________________________________________
#To get text of manual input
we can't use .text (it will work for inbuilt html)
we have to use .get_attribute("value")
__________________________________________________________________________________
#IMPLICIT WAIT
Implicit wait is global wait
We have to apply after declaring driver.
It will wait for every element to load for the specific time we gave.
driver.implicitly_wait(5)
Note: Implicit wait not works for find_elements
__________________________________________________________________________________
#MOUSE HOVER
For every method we have to use .perform at last.
__________________________________________________________________________________
#CHILD WINDOW
Switch to different window by
driver.window_handles (return all the window handles)
driver.switch_to.window(windowsopened[1]) (switch to particular window by index of handles)
__________________________________________________________________________________
#Frames
To switch any frame we have to use driver.switch_to.frame("mce_0_ifr") #passing id
To work outside of the frame we have to come out first from the current frame by using
driver.switch_to.default_content()
__________________________________________________________________________________
#MISCELLANOUS
There are soo many js which we can use in selenium to scroll i selenium
#Scroll
window.scrollBy(0,100) it will scroll lil bit
window.scrollBy(0,document.body.scrollHeight)
ex - driver.execute_script(window.scrollBy(0,document.body.scrollHeight))
#Screenshot
driver.get_screenshot_as_file("image.png")
__________________________________________________________________________________
#BROWSER_LESS TESTING
Here chrome will execute in backend --- headless
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("headless")
chrome_options.add_argument("--ignore-certificate-errors") # will ignore the certificates
__________________________________________________________________________________
#EXPLICIT WAIT
Explicit wait is a conditional wait
#wait untill India appears in suggestion
wait = WebDriverWait(driver,10)
wait.until(expected_conditions.presence_of_element_located((By.LINK_TEXT,"India")))
#select india from suggestion
driver.find_element(By.LINK_TEXT,"India").click
__________________________________________________________________________________
#LOGS
Debug
Info
Warning
Error
Critical
__________________________________________________________________________________
If working for pytest then :-
Creation of python file must be started with test_ or end with _test
Pytest method should start with test
Any code should be wrapped in method only
__________________________________________________________________________________
#To run pytest in terminal
cd your pytest folder path
py.test (will run all the test cases)
__________________________________________________________________________________
#Test case name == Method name
__________________________________________________________________________________
#from terminal execution -
-k stand for method name execution, -s logd in output, -v stands for more info metadata
__________________________________________________________________________________
# you can run specific file
py.test <filename>
__________________________________________________________________________________
#@pytest.mark.xfail
it will run without log
__________________________________________________________________________________
#you can mark tests @pytest.mark.smoke and then run with -m
__________________________________________________________________________________
#to skip any test case @pytest.mark.skip
__________________________________________________________________________________
#fixture
we can use fixture in every script by calling
@pytest.fixture()
annotation which will execute after and before test.
EX-
@pytest.fixture()
def setup():
print("I will execute first")
def test_fixtureDemo(setup):
print("I will execute steps")
above @pytest.fixture()
will execute first then 2nd method as we have passed setup in wnd method that setup is fixture
__________________________________________________________________________________
#YIELD
If we apply apply yield then whatever method is after YIELD
will execute at last.
EX-
@pytest.fixture()
def setup():
print("I will execute first") #will execute first
YIELD
print("I will execute last") #will execute at last
def test_fixtureDemo(setup):
print("I will execute steps") #will execute second
__________________________________________________________________________________
IF yoy are going to use fixture many times in your code then
put fixture in on file called "conftest.py"
and call that fixture in very code which is inside pytest folder.
__________________________________________________________________________________
Use fixture for soo many test cases:
@pytest.mark.usefixture(setup)
class Testexapmle:
def test_fixtureDemo(self):
print("I will execute steps")
def test_fixtureDemo1(self):
print("I will execute steps1")
def test_fixtureDemo2(self):
print("I will execute steps2")
it will give output like:
I will execute first
I will execute steps2
I will execute last
__________________________________________________________________________________
#EXCEL READ and WRITE
Get openpyxl package
terminal pip install openpyxl
-import openpyxl
-book = openpyxl.load_workbook("path")
-sheet = book.active
-cell = sheet.cell
-print(cell.value)