Python selenium automated testing framework Getting real - Login Test Case

This article is Python automated test framework Basics articles, mainly to help write the basic selenium test code is not planning colleagues.
In this paper, the POM model, selenium, unittest framework, configparser profile, smtplib mailing, HTMLTestRunner test report cases of simple login module combines automated testing framework
project mainly includes the following sections

 conif.ini placed Profiles

E.g:

 myunit.py file browser placement operation code

import unittest
from selenium import webdriver

class MyTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.maximize_window()

def tearDown(self):
self.driver.quit()

if __name__=='__main__':
unittest.main()

placing base.py browser object operation code

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import os,configparser
class Page(object):
path = os.path.dirname(os.path.abspath("."))
cfpath = os.path.join(path, 'autoparker\config\conf.ini')
conf = configparser.ConfigParser()
conf.read(cfpath)
url=conf.get('base','url')
def __init__(self,driver,url=url):
self.driver=driver
self.url=url

def open(self):
self.driver.get(self.url)

def find_element(self,*loc):#传入参数为元组需要加*,本身就是元组的不需要*
#print(*loc)
try:
WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
return self.driver.find_element(*loc)
except:
print('页面中未找到 %s 元素'%(self,loc))

def find_elements(self,*loc):
return self.driver.find_elements(*loc)

def send_keys(self,loc,value):
self.find_element(*loc).send_keys(value)

def click(self,loc):
self.find_element(*loc).click()

def clear(self,loc):
self.find_element(*loc).clear()

loginpage.py placed Universal Login module code (to avoid duplication of code)

from selenium.webdriver.common.by import By
from time import sleep
from objpage.base import Page
class login(Page):

username_loc=(By.NAME,'accounts')
password_loc=(By.NAME,'pwd')
login_button_loc=(By.XPATH,'/html/body/div[5]/div/form/fieldset/p/button')
login_error_loc=(By.XPATH,'//*[@id="common-prompt"]/p')

def login_username(self,username):
self.find_element(*self.username_loc).clear()
self.find_element(*self.username_loc).send_keys(username)

def login_password(self,password):
self.find_element(*self.password_loc).clear()
self.find_element(*self.password_loc).send_keys(password)

def login_button(self):
self.find_element(*self.login_button_loc).click()

#统一登录入口
def user_login(self,username,password):
self.open()
self.login_username(username)
self.login_password(password)
self.login_button()
SLEEP (2) 

# Login prompt 
DEF login_error_text (Self):
 return self.find_element (* self.login_error_loc) .text

parker.py placing common elements operation code (parker I just named, do not tangle)

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select

class Parker(object):
def __init__(self,browser='chrome'):
if browser=='ie' or browser=='internet explorer':
driver=webdriver.Ie()
elif browser=='firefox' or browser==' FF ' : 
Driver = webdriver.Firefox ()
 elif Browser == ' Chrome ' : 
Driver = webdriver.Chrome ()
 the try : 
self.driver = Driver
 the except Exception:
 The raise NameError ( ' not found a browser, enter "ie" , "Chrome", "FF" ' ) 

DEF the wait (Self, = secs. 5): # implicit wait 
self.driver.implicitly_wait (secs) 

DEF to_element (Self, Key): # elements are positioned 
IF  ' -> '  Not  inkey: # If a key that does not contain the following statement is executed = 
The raise NameError ( ' parameter type input error ' ) 
by = key.split ( ' -> ' ) [0] # by obtaining the partition [0] corresponding to the value of 
Val key = .split ( ' -> ' ) [1] # through the partition access to [1] a value corresponding to 
IF by == ' ID ' : 
Element = self.driver.find_element_by_id (Val)
 elif by == ' name ' : 
Element = Self .driver.find_element_by_name (Val)
 elif by == ' class':
element=self.driver.find_element_by_class_name(val)
elif by=='link_text':
element=self.driver.find_element_by_link_text(val)
elif by=='xpath':
element=self.driver.find_element_by_xpath(val)
elif by=='css':
element=self.driver.find_element_by_css_selector(val)
else:
raise NameError('请输入正确的定位方式:id,name,class,link_text,xpath,css')
return Element 

DEF Open (Self, url): # Open a the URL of 
self.driver.get (url) 

DEF Max_Window (Self): # maximize the window (browser) 
self.driver.maximize_window () 

DEF set_windows (Self, Wide, High): # set window size 
self.driver.set_window_size (Wide, High) 

DEF iNPUT (Self, Key, text): # text input box 
EL = self.to_element (Key) 
el.send_keys (text) 

DEF the Click ( Self, Key): # click 
el = self.to_element (Key) 
el.click () 

DEF the Clear (Self, Key): # Clear the text box contents 
el =self.to_element(key)
el.clear()

def right_click(self,key):#右键操作
el=self.to_element(key)
ActionChains(self.driver).context_click(el).perform()

def move_to_element(self,key):#鼠标悬停
el=self.to_element(key)
ActionChains(self.driver).move_to_element(el).perform()

def drag_and_drop(self,el_key,ta_key):#拖拽 从一个元素拖到另外一个元素
el=self.to_element(el_key)
target=self.to_element(ta_key)
ActionChains(self.driver).drag_and_drop(el,target).perform()

def click_text(self,text):
self.driver.find_element_by_partial_link_text (text) .click ()

DEF use Close (Self): # close the current browser window 
self.driver.close () 

DEF quit (Self): # exit the browser 
self.driver.quit () 

DEF the Submit (Self, Key): # submit event 
EL = Self .to_element (Key) 
el.submit () 

DEF F5 (Self): # refresh 
self.driver.refresh () 

DEF JS (Self, Script): # perform JS 
self.driver.execute_script (Script) 

DEF get_attribute (Self, Key , attribute): # Get element attributes 
EL = self.to_element (Key)
 return get_text (Self, Key):el.get_attribute (attribute) 

DEF# Get text 
EL = self.to_element (Key)
 return el.text 

DEF get_title (Self): # get title 
return self.driver.title 

DEF GET_URL (Self): # get url 
return self.driver.current_url 

DEF to_frame (Self, Key): # window switching 
EL = self.to_element (Key) 
self.driver.switch_to.frame (EL) 

DEF alert_accept (Self): # box confirmation operation 
self.driver.switch_to.alert.accept () 

DEF alert_dismiss (Self ): # dialog box to cancel the operation 
self.driver.switch_to.alert.dismiss () 

DEF img(self,fp):#截图
self.driver.get_screenshot_as_file(fp)

def select_by_value(self,key,value):#下拉框操作
el=self.to_element(key)
Select(el).select_by_value(value)

mail transmission codes placed send_email.py 

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import configparser
import os

def sendEmail(file_path):
path=os.path.dirname(os.path.abspath("."))
cfpath=os.path.join(path,'autoparker\config\conf.ini')
conf = configparser.ConfigParser()
conf.read(cfpath)
smtpserver = conf.get('emailqq','smtpserver')
sender = conf.get('emailqq','sender')
pwd = conf.get('emailqq','pwd')

receiver=[]
email_to=conf.get('emailqq','receiver')
email_array=email_to.split(';')
for i in range(len(email_array)):
receiver.append(email_array[i])
Print (Receiver) 

with Open (file_path, ' RB ' ) AS FP: 
mail_boby = fp.read () 
MSG = MimeMultipart () 
MSG [ ' the From ' ] = SENDER 
MSG [ ' the To ' ] = " , " .join (Receiver ) 
MSG [ ' the Subject ' ] = ' I once complete mirror break ' 
body = MimeText (mail_boby, ' HTML ' , ' UTF-. 8 ' ) 
msg.attach (body) 
ATT=MIMEText(mail_boby,'html','utf-8')
att['Content-Type']='application/octet-stream'
att['Content-Disposition']='attachment;filename="test_reuslt.html"'
msg.attach(att)
try:
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender,pwd)
except:
smtp=smtplib.SMTP_SSL(smtpserver,465)
smtp.login(sender,pwd)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

sendEmail('D:\\report.html')

 Finally main.py file is placed to run code (run this file can then be tested)

import HTMLTestRunner
import unittest
from test_case.login import loginTest
from public.send_email import sendEmail

if __name__=='__main__':
testunit=unittest.TestLoader().loadTestsFromTestCase(loginTest)
suite=unittest.TestSuite(testunit)
file_path="D:\\html_report.html"
fp=open(file_path,'wb')
runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='Logon test ' , Description = ' test execution ' ) 
runner.run (Suite) 
fp.close () 
the sendEmail (file_path)

First here today, an entry-level automated testing framework, due to the non-professional self-development, out for everyone to share programming code may be a bit non-standard, named also very casual. Check it out first on everyone! Back slowly improving.
To learn even more bigwigs.
Note: There may want to self-study with a friend to see the profile code and exchange. Three lines must be my teacher

Guess you like

Origin www.cnblogs.com/ceshi-xiaoliu/p/11585556.html