selenium3自动化测试实战

https://www.cnblogs.com/wusir66/p/9838960.html

https://blog.csdn.net/weixin_42134789/article/details/81505963

https://blog.csdn.net/qq_34045989/article/details/88977104

https://www.jianshu.com/p/3d99ddd5148f


    
用户名:
密码:
{{ error }} {% csrf_token %}
# 设置cookie
def set_cookie(request):
    res = HttpResponse("设置cookie")
    res.set_cookie("name", "cuiheran")
    return res


def get_cookie(request):
    name = request.COOKIES.get("name")
    print(name)
    return HttpResponse(name)



def Login(request):
    return render(request, "login.html")

def do_login(request):
    print(200)
    uname = request.POST.get("uname")
    print(uname)
    response = HttpResponseRedirect(reverse("mine"))
    response.set_cookie("uname", uname)
    return response


def mine(request):
    uname = request.COOKIES.get("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 = request.POST.get("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 = request.COOKIES.get("context")
    if uname:

        return HttpResponse(uname)
    return redirect(reverse("login"))

登录页面详解

    uname = request.get_signed_cookie("context",salt="salt")  # 解密

selenium3自动化测试实战_第1张图片
selenium3自动化测试实战_第2张图片
selenium3自动化测试实战_第3张图片

  • 单元测试
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:/chromedriver/chromedriver.exe")

		self.base_url = "https://www.baidu.com"

	def test_search_key_selenium(self):
		self.driver.get(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.driver.get(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:/chromedriver/chromedriver.exe")
driver.get("https://www.baidu.com")
# 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 = driver.text
# print(text)
# search.submit()  # submit可以代替click 但是没有他功能强大
driver.refresh()
driver.back()
# driver.back()

# driver.quit()
  • 缩小窗口
from selenium import webdriver

driver = webdriver.Chrome("D:/chromedriver/chromedriver.exe")
driver.get("https://www.baidu.com")

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);"
driver.execute_script(js)
  • 播放视频
from time import sleep

from selenium import webdriver

driver = webdriver.Chrome("D:/chromedriver/chromedriver.exe")
driver.get("http://videojs.com")

video = driver.find_element_by_id("preview-player_html5_api")

url = driver.execute_script("return arguments[0].currentSrc;", video)

print(url)

print("start")
driver.execute_script("arguments[0].play();", video)

sleep(15)
print("stop")

driver.execute_script("arguments[0].pause();", video)

driver.quit()

你可能感兴趣的:(selenium3自动化测试实战)