selenium3⾃动化测试实战
<body>
<form action="{% url 'do_login' %}" method="post">
<span>⽤户名:</span><input type="text" name="uname" placeholder="请输⼊⽤户名"><br>
<span>密码:</span><input type="password" name="password" placeholder="请输⼊密码"><br>                {{ error }}
<button id="btn">登录</button>
{% csrf_token %}
</form>
</body>
# 设置cookie
def set_cookie(request):
res = HttpResponse("设置cookie")
res.set_cookie("name", "cuiheran")
return res
def get_cookie(request):
name = ("name")
print(name)
return HttpResponse(name)
def Login(request):
return render(request, "login.html")
def do_login(request):
print(200)
uname = ("uname")
print(uname)
response = HttpResponseRedirect(reverse("mine"))
response.set_cookie("uname", uname)
return response
def mine(request):
uname = ("uname")
return HttpResponse(uname)
从login到-----login.html-----do_login-----mine
def Login(request):
return render(request, "login.html")
def do_login(request):
print(200)
uname = ("uname")
print(uname)
response = HttpResponseRedirect(reverse("mine"))
# response.set_cookie("uname", uname)
response.set_signed_cookie("context", uname, "salt")  #加密
return response
def mine(request):
uname = ("context")
if uname:
selenium获取cookie
return HttpResponse(uname)
return redirect(reverse("login"))
登录页⾯详解
uname = _signed_cookie("context",salt="salt")  # 解密
单元测试
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome("D:/")  self.base_url = "www.baidu"
def test_search_key_selenium(self):
(self.base_url)
self.driver.find_element_by_id("kw").send_keys("selenium")
self.driver.find_element_by_id("su").click()
time.sleep(2)
title = self.driver.title
self.assertEqual(title, "selenium_百度搜索")  # title标签⾥⾯的内容
def test_search_key_unittest(self):
(self.base_url)
self.driver.find_element_by_id("kw").send_keys("unittest")
self.driver.find_element_by_id("su").click()
time.sleep(2)
title = self.driver.title
self.assertEqual(title, "unittest_百度搜索")
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
selenium
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome("D:/")
<("www.baidu")
# driver.set_window_size(480,800)
driver.find_element_by_id("kw").send_keys("selenium")
# driver.find_element_by_link_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input").send_keys("区块链") # driver.find_element_by_class("s_ipt").send_keys("selenium")
# above = driver.find_element_by_link_text("设置")
driver.find_element_by_id("su").click()
# driver.find_element_by_id("su").submit()
# ActionChains(driver).move_to_element(above).perform()
title = driver.title
print(title)
url = driver.current_url
print(url)
# text =
# print(text)
# search.submit()  # submit可以代替click 但是没有他功能强⼤
driver.back()
# driver.back()
# driver.quit()
缩⼩窗⼝
from selenium import webdriver
driver = webdriver.Chrome("D:/")
<("www.baidu")
driver.set_window_size(800,600)
driver.find_element_by_id("kw").send_keys("go")
driver.find_element_by_id("su").click()
js = "window.scrollTo(100,800);"
播放视频

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。