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

Take you through Python automated testing (VIII) -- cookie handling

編輯:Python

adopt webdriver Can be used for cookie To deal with , Common processing methods include obtaining 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

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