Write a python Google automated test library by yourself

Posted by scottybwoy on Wed, 18 Mar 2020 17:51:09 +0100

I've been exposed to selenium before. There are call interfaces in various languages and browser kernels But we need to build an automatic test library by ourselves for convenience. We can use it when we throw it under our program. We only support window s at present Each kernel browser has its own web debugging protocol Here I use Google's Chrome DevTools Used WebHttp WebSocket communication is specific. Please find the documents by yourself. We only give the library files written in c + + and Encapsulation of python Here is the package code of py

# -*- coding: UTF-8 -*-
import IChromeLink

def StartUp(host):
	IChromeLink.StartUp(host+" --remote-debugging-port=9222")
def GetTabs(index):
	return IChromeLink.GetTabs("localhost:9222")[index]["WebSocketDebuggerUrl"]
class ChromeTagLink:
    m_pChromeAccessor = 0
    def __init__(self,ws):
        ChromeTagLink.m_pChromeAccessor=IChromeLink.chrome_start(ws)
    def IsValid(self):
        return IChromeLink.chrome_IsValid(ChromeTagLink.m_pChromeAccessor)
    def Navigate(self,url,Referrer=""):
        return IChromeLink.chrome_Navigate(ChromeTagLink.m_pChromeAccessor,url,Referrer)
    def ReLoad(self,Cache):
        return IChromeLink.chrome_ReLoad(ChromeTagLink.m_pChromeAccessor,Cache)
    def UpdateFrame(self):
        return IChromeLink.chrome_UpdateFrame(ChromeTagLink.m_pChromeAccessor)
    def IsLoading(self):
        return IChromeLink.chrome_IsLoading(ChromeTagLink.m_pChromeAccessor)
    def GetChildFrameCount(self):
        return IChromeLink.chrome_GetChildFrameCount(ChromeTagLink.m_pChromeAccessor)
    def GetMainFrame(self):
        return IChromeLink.chrome_GetMainFrame(ChromeTagLink.m_pChromeAccessor)
    def contextid_count(self):
        return IChromeLink.chrome_contextid_count(ChromeTagLink.m_pChromeAccessor)
    def Eval(self,code):
		fid=self.GetMainFrame()["FrameId"]
		for x in self.contextid_count():
			if x["FrameId"]==fid:
				return IChromeLink.chrome_Eval(ChromeTagLink.m_pChromeAccessor,x["dwExecutionContextId"],code)
    def Close(self):
        IChromeLink.Close(ChromeTagLink.m_pChromeAccessor)
    def hmbb(self):
        return "666"
    def css_get_value(self,path):#Get element value
        return self.Eval("document.querySelector('{}').value".format(path))
    def css_set_value(self,path,value):#Set element value
        return self.Eval("document.querySelector('{}').value='{}'".format(path,value))
    def css_get_innerText(self,path):#Get element text text
        return self.Eval("document.querySelector('{}').innerText".format(path))	
    def css_set_innerText(self,path,innerText):#Set element text text
        return self.Eval("document.querySelector('{}').innerText='{}'".format(path,innerText))	
    def css_get_innerHTML(self,path):#Get element html text
        return self.Eval("document.querySelector('{}').innerHTML".format(path))	
    def css_set_innerHTML(self,path,innerHTML):#Set element html text
        return self.Eval("document.querySelector('{}').innerHTML='{}'".format(path,innerHTML))
    def css_get_getAttribute(self,path,name):#Element gets attribute
        return self.Eval("document.querySelector('{}').getAttribute('{}')".format(path,name))	
    def css_set_getAttribute(self,path,name,value):#Element set attribute
        return self.Eval("document.querySelector('{}').setAttribute('{}','{}')".format(path,name,value))
    def css_event(self,path):#Element event
        return self.Eval("document.querySelector('{}').event()".format(path))	
    def css_blur(self,path):#Remove element focus
        return self.Eval("document.querySelector('{}').blur()".format(path))		
    def css_focus(self,path):#Element get focus
        return self.Eval("document.querySelector('{}').focus()".format(path))		
    def css_click(self,path):#Element Click
        return self.Eval("document.querySelector('{}').click()".format(path))
    def css_checked(self,path,checked):#checked  false|true
        return self.Eval("document.querySelector('{}').checked={}".format(path,checked))
    def css_get_action(self,path):#Read form address
        return self.Eval("document.querySelector('{}').action".format(path))
    def css_set_action(self,path,action):#Set form address
        return self.Eval("document.querySelector('{}').action='{}'".format(path,action))	
    def css_reset_action(self,path):#Reset Form
        return self.Eval("document.querySelector('{}').reset()".format(path))		
    def css_submit_action(self,path):#Submit Form
        return self.Eval("document.querySelector('{}').submit()".format(path))		
    def css_set_scrollBy(self,xn,yn):#Set scroll bar distance
        return self.Eval("window.scrollBy({},{})".format(xn,yn))			
    def css_set_scrollTo(self,x,y):#Set scroll bar position
        return self.Eval("window.scrollTo({},{})".format(x,y))		
    def css_get_htmls(self,path):#Get element list
        atd="var htmls=[];[].forEach.call(document.querySelectorAll('"+path+"'),function(a){htmls.push(a.innerText)});JSON.stringify(htmls);"
        return self.Eval(atd)#innerText innerHTML

The above implements common script execution and filling operations and events Write a random example

# -*- coding: UTF-8 -*-
from chrome import *#Here we refer to the package above
import time
StartUp('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')#Prepare a Google browser
 time.sleep(3)#Wait three seconds for Google to start
chrome = ChromeTagLink(GetTabs(0))#Initialize page
chrome.Navigate("http://Www.bigauge.tv/xuanxiaoshuo/ ") (jump to page
for num in range(0,3):
    print chrome.css_get_htmls(".s3")#List with output class s3 
    #Note that CSS get HTMLs here can be changed to any element you want to get
    chrome.css_click(".ngroup") #Get the data and click to turn the page
    time.sleep(4) #Wait four seconds for page turning to continue
print "Climb over"
chrome.Close()#Destruction class

Note that the number of time.sleep(4) seconds here is not fixed according to the actual situation of your computer

Because of the limited space, it's so much for the time being Library and code download address Links: https://share.weiyun.com/5f7hfb0 Password: pe26pj

Topics: Google Attribute Selenium Python