程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Take you to learn Python automated testing (VII) -- warning box processing bit

編輯:Python

Warning box handling

stay web in , In addition to the elements and operations mentioned above , There is also the processing of the prompt box of the page , The warning boxes on a page are usually divided into these categories js alert 、 confirm as well as prompt, These warning boxes , We can all go through switch_to_alert() To deal with it . The warning box can be handled in the following ways :

 text

The text in the secure warning box

 accept

Click OK... In the warning box 32

 dismiss

If the warning box is completely cleared , You can click "cancel all" through this method .

Save the following code as .html file , Click the clear data button above , Will produce a confirm The police

Notice frame , Click ok , Ok... Is displayed in the input box , Click cancel all , Then the input box displays "all clear..." , Next to this

A warning box is processed . The code is as follows :

<script> function clear1()
{

if(confirm(" Are you sure you want to clear the data ?"))
{

document.main.text1.value = " determine ";
}
else
{

document.main.text1.value = " Complete elimination ";
}
}
</script>
<body>
<form name="main">
<input type="text" name="text1" />
<input type="button" name="Submit" value=" Empty data " onClick="return
clear1();">
</form>
</body>
from selenium import webdriver
Import webdriver
from time import sleep
Need to use sleep Come and pause , So import here time Of sleep modular
driver = webdriver.Firefox()
open firefox browser
driver.get('file:///D:/test/test1.html')
open html file
driver.find_element_by_css_selector('body > form:nth
child(1) > input:nth-child(2)').click()
Click button
text=driver.switch_to_alert().text
Get the text of the warning box , Assign to text Variable
print text Output text
driver.switch_to_alert().dismiss()
Click Cancel... In the warning box
sleep(3)
wait for 3 second
driver.find_element_by_css_selector('body > form:nth
child(1) > input:nth-child(2)').click()
Click the button again
driver.switch_to_alert().accept()
Click OK... In the warning box

Cookie Handle

adopt webdriver Can be used for cookie Processing , Common treatment methods include full

cookie、 add to cookie、 Delete the specified cookie、 Delete all cookie.

obtain cookie Information

from selenium import webdriver
from time import sleep
drvier=webdriver.Firefox()
drvier.get('http://www.chuangyijia.com/login')

Open the front landing page

drvier.implicitly_wait(3)
drvier.find_element_by_id('email').send_keys('8101550
[email protected]')
enter one user name
drvier.find_element_by_id('pwd').send_keys('a654321')
Input password
drvier.find_element_by_css_selector('#submit').click()
Click login
cookie = drvier.get_cookies()
Get the information after login cookie Information
print cookie
Print what you get cookie Information
3334

towards cookie Add information to

from selenium import webdriver
from time import sleep
drvier=webdriver.Firefox()
drvier.get('http://www.chuangyijia.com/login')

Open the front landing page

cookie = drvier.add_cookie({
‘name’:’key
test’,’value’:’key-test’})
add to cookie Information
add to cookie, have access to add_cookie Way to add .

Delete cookie Information in

drvier.delete_cookie('ci_session')
Delete cookie
drvier.delete_all_cookies()
Delete all cookie

expected_conditions

In the process of self-test , It is usually necessary to judge the test results , Here you can

expected_conditions To achieve the desired results , To assert execution status .

expected_conditions Provides multiple methods , The common methods are as follows :

 title_is: Judge the of the front page title Whether it is the expected result

 title_contains: Judge the of the front page title Contains the expected character

 presence_of_element_located: Determine whether an element exists , However, it does not mean that the element is visible ,

If the element exists , Returns the element , Otherwise, throw an exception .

 visibility_of_element_located: Determine whether there are elements on the page , And the element is visible , If saved

In and visible , Returns the element , Otherwise, throw it out of order . presence_of_all_elements_located: Determine that at least one page exists , There should be a , be

Returns a list of all elements , Otherwise return an empty list .

 text_to_be_present_in_element: Determine whether the text of an element contains the expected string ,

Match returns True, Otherwise return to False.

from selenium import webdriver
Import webdriver
from selenium.webdriver.support import expected_conditions
Import expected_conditions modular
from selenium.webdriver.common.by import By
stay expected_conditions Positioning... Is required in ,by Provide unified use
find_element() Method , Simplifies positioning operations
from time import sleep
Import sleep modular
drvier=webdriver.Firefox()
Open the browser
drvier.get('http://www.chuangyijia.com/login')
Open the landing page
drvier.implicitly_wait(3)
wait for 3 second
drvier.find_element_by_id('email').send_keys('[email protected]
.com')
enter one user name
drvier.find_element_by_id('pwd').send_keys('a654321')
Input password
drvier.find_element_by_css_selector('#submit').click()
land
sleep(2)
Now wait 2 second , It's mainly about getting title, Too soon , Acquired title
It was before the successful login title
35is_title=expected_conditions.title_is(u' home page - Creationist ')
Determine the of the page title Whether it is the expected string
is_title(drvier)
If it is equal to the expected string , The result returned here is True, Otherwise False.
Title_is It's a Class, The class Implemented in the __call__ Method , So this
A class object can be called like a function .
is_in_title=expected_conditions.title_contains(u' Creationist ')
Judge title Contains the expected string
is_in_title(drvier)
It can be used print Print his return result as True also False
is_exist=expected_conditions.presence_of_element_located((
By.CSS_SELECTOR,'.sq_menu > a:nth-child(3)'))
Determine whether the element exists on the page , Not necessarily on the page
print is_exist(drvier)
If there is , The element information is returned here , Otherwise there will be
NoSuchElementException It's abnormal .
in_ele=expected_conditions.presence_of_all_elements_locate
d((By.TAG_NAME,'li'))
Whether there is at least one specified element on the page
print in_ele(drvier)
If there is , This returns a list , Otherwise, the returned list is empty
visibility_exist=expected_conditions.visibility_of_element
_located((By.TAG_NAME,'li'))
Check whether the element is visible
print visibility_exist(drvier)
If the element exists and is visible , Element information is returned , Elements are not visible , Then return an exception ,
NoSuchElementException It's abnormal , If the element exists , But it's not visible ,
Then return to False.
3637
is_text_in_ele=expected_conditions.text_to_be_present_in_e
lement((By.CSS_SELECTOR,'.menu > ul:nth-child(1) > li:nth
child(1) > a:nth-child(1)'),u' It means ')
Check whether the element contains a string
print is_text_in_ele(drvier)
If the check contains a string , Then return to True, Otherwise return to False.

Finally, thank everyone who reads my article carefully , The following online link is also a very comprehensive one that I spent a few days sorting out , I hope it can also help you in need !

These materials , For those who want to change careers 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful ……

If you don't want to grow up alone , Unable to find the information of the system , The problem is not helped , If you insist on giving up after a few days , You can click the small card below to join our group , We can discuss and exchange , There will be various software testing materials and technical exchanges .

Click the small card at the end of the document to receive it

Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .

Self study recommendation B Stop video :

Zero basis transition software testing :25 Days from zero basis to software testing post , I finished today , Employment tomorrow .【 Include features / Interface / automation /python automated testing / performance / Test Development 】

Advanced automation testing :2022B The first station is super detailed python Practical course of automated software testing , Prepare for the golden, silver and four job hopping season , After advanced learning, it soared 20K


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved