Selenium Workflow
Selenium working principle
The client and server are actually connected through http Protocol to communicate , The interface document of the server can be referred to :
https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#sessionsessionidelement
The client passes in the request mode according to the requirements of the server interface 、 request url And request data , And then send http request , After receiving the request, the server drives the browser to execute the command .
selenium Actually, the request functions of various operation interfaces have been encapsulated , Users only need to pass in data through the specified method .
Such as get、click、find_element It actually encapsulates the whole http Method of request process .
That is, the tester only needs to call the corresponding method according to the functional requirements and pass the parameters , In the end by the selenium After assembling the request information, send the request .
so , You can skip it selenium, By myself requests The module build request information is sent to the driver , So as to drive the browser to perform relevant operations .
From the perspective of source code Selenium principle
explain
Following example , With Python、Chrome The driver 、Chrome Browser expansion instructions .
adopt Selenium Implement workflow
# start-up chrome browser
driver = webdriver.Chrome()
# Visit Baidu website
driver.get("http://www.baidu.com")
# Find the input box element
ele = driver.find_element_by_id("kw")
# Enter in the input box “selenium”
ele.send_keys("selenium")
# Find Baidu button
ele = driver.find_element_by_id("su")
# Click Baidu button
ele.click()
① driver = webdriver.Chrome()
start-up chromedriver The driver , After starting, it can pass http://ip:port Access driver .
adopt execute Method , structure “ Create a new session ” Interface request information , And send the http request , Return to session id.
Request mode :POST
Request address :http://localhost:post/session
Request data :
data = {"desiredCapabilities":{
"browserName": "chrome",
"version":"85.0.4183.83",
"platform":"WINDOWS"}
}
② driver.get(“http://www.baidu.com”)
adopt execute Method , Press get Command interface information construction http Request information , And send the http request .
Request mode :POST
Request address :http://localhost:post/session/$sessionId/url(sessionId For session id, from ① In order to get )
Request data :
{"url": "http://www.baidu.com"}
③ driver.find_element_by_id(“kw”)
adopt execute Method , Press find_element_by_id Command interface information construction http Request information , And send the http request .
Request mode :POST
Request address :http://localhost:post/session/$sessionId/element(sessionId For session id, from ① In order to get )
Request data :
{"using":"id","value":"kw"}
④ ele.send_keys(“selenium”)
adopt execute Method , Press send_keys Command interface information construction http Request information , And send the http request .
Request mode :POST
Request address :http://localhost:post/session/ s e s s i o n I d / e l e m e n t / sessionId/element/ sessionId/element/id/value(sessionId For session id, from ① In order to get ,id For the elements id, from ③ obtain )
Request data :
{"value": ["selenium"]}
⑤ ele.click()
adopt execute Method , Press click Command interface information construction http Request information , And send the http request .
Request mode :POST
Request address :http://localhost:post/session/ s e s s i o n I d / e l e m e n t / sessionId/element/ sessionId/element/id/click(sessionId For session id, from ① In order to get ,id For the elements id, from ③ obtain )
Request data : nothing
Code implementation
# TODO Access to the domain name
base_url = "http://localhost:9515"
# TODO Create a session
url = base_url + "/session"
data = {
"desiredCapabilities":{
"browserName": "chrome",
"version":"85.0.4183.83",
"platform":"WINDOWS"
}
}
res = request("POST",url,json=data)
session_id = res.json()["sessionId"] # Return to session id
sleep(2)
# TODO send out get command - Visit a website
url = base_url + "/session/{}/url".format(session_id)
data = {
"url": "http://www.baidu.com"
}
res = request("POST",url,json=data)
# TODO send out find_element command - Look for the element
url = base_url + "/session/{}/element".format(session_id)
data = {
"using":"id",
"value":"kw"
}
res = request("POST",url,json=data)
ele = jsonpath(res.json(),"$..ELEMENT")[0] # Returns the element id
# TODO send out send_keys command - Input content
url = base_url + "/session/{}/element/{}/value".format(session_id,ele)
data = {
"value": ["selenium"]
}
res = request("POST",url,json=data)
# TODO send out find_element command - Look for the element
url = base_url + "/session/{}/element".format(session_id)
data = {
"using":"id",
"value":"su"
}
res = request("POST",url,json=data)
ele = jsonpath(res.json(),"$..ELEMENT")[0] # Returns the element id
# TODO send out click command - Element click
url = base_url + "/session/{}/element/{}/click".format(session_id,ele)
res = request("POST",url)
Last : It can be in the official account : Sad spicy bar ! Get a free copy of 216 Page software testing engineer interview guide document information . And the corresponding video learning tutorial is free to share !, It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .
If my blog helps you 、 If you like my blog content , please “ give the thumbs-up ” “ Comment on ” “ Collection ” One button, three links ! Friends who like software testing , You can join our testing technology exchange group :914172719( There are various software testing resources and technical discussions )
Interview : First tier cities move bricks ! Another software testing post ,5000 That's enough …
What kind of person is suitable for software testing ?
The man who leaves work on time , Promoted before me …
The test post changes jobs repeatedly , Jump, jump and disappear …