码迷,mamicode.com
首页 > 编程语言 > 详细

python+selenium+unittest

时间:2020-07-10 09:28:52      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:over   net   setup   cio   wait   https   send   输入   acs   

1.login.py

# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

#拿到diver
driver = webdriver.Firefox()

#跳转网页
driver.get("https://old.xdclass.net")

print(driver.title)
#睡眠时间3秒
sleep(3)

#登录框
login_ele = driver.find_element_by_css_selector("#login")
#触发点击事件
ActionChains(driver).click(login_ele).perform()

sleep(1)
#查找输入框,输入账号,输入框要提前清理里面的数据
driver.find_element_by_id("phone").clear()
driver.find_element_by_id("phone").send_keys("17743125104")

#查找密码输入框,输入密码
driver.find_element_by_id("pwd").clear()
driver.find_element_by_id("pwd").send_keys("zxm3816489")

#拿到登录按钮
login_btn_ele = driver.find_element_by_css_selector("button.login")

#触发点击事件,登录
login_btn_ele.click()

#判断登陆是否成功,逻辑-》鼠标移到上面,判断弹窗字符

#获取鼠标上移的元素
user_info_ele = driver.find_element_by_css_selector(".user_head_portrait")

sleep(1)
#hover触发
ActionChains(driver).move_to_element(user_info_ele).perform()

sleep(1)
#获取用户名称元素
user_name_ele = driver.find_element_by_css_selector(".img_name > span:nth-child(2)")

print("===测试结果==")

print(user_name_ele.text)

name = user_name_ele.text

if name == u"xiaoming":
    print("login ok")
else:
    print("login fail")

2.cookie.py

# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

#拿到diver
driver = webdriver.Firefox()

#跳转网页
driver.get("https://xdclass.net")
print(driver.title)

#睡眠时间3秒
sleep(3)

driver.add_cookie({"name":"name","value":"jack"})
driver.add_cookie({"name":"token","value":"xdclasseyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4ZGNsYXNzIiwicm9sZXMiOiIxIiwiaW1nIjoiaHR0cDovL3RoaXJkd3gucWxvZ28uY24vbW1vcGVuL3ZpXzMyL1EwajRUd0dUZlRKb0M3aWN6Y3F2cDc4dTFmMEhySmljZHJzNUtYWDgxdWRKVTQzMzlYNVEyWUtKd2xXSlVpYXZPU2Vnd1Z6U3RZTUNSWGQ5Nm52eXB5UE13LzEzMiIsImlkIjo3MywibmFtZSI6IuS6jOW9k-WutuWwj0QiLCJpYXQiOjE1MzgwNjY1MDgsImV4cCI6MTUzODY3MTMwOH0.Clm7FGGylmb6Acs_nRDcLyYzb8yv64LS_hIk16Wwkkg"})

video_ele = driver.find_element_by_css_selector("div.hotcourses:nth-child(3) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1) > div:nth-child(1) > img:nth-child(1)")
video_ele.click()
sleep(2)

buy_btn_ele = driver.find_element_by_css_selector(".learn_btn > a:nth-child(1)")

buy_btn_ele.click()
print("进入下单页面")

 

 

 

 

3.

# -*- coding: UTF-8 -*-
import unittest
import time
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains


class LoginOrderTestCase(unittest.TestCase):
    def setUp(self):
        print("测试开始")
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(20)
        self.base_url = "https://xdclass.net"
        self.driver.get(self.base_url)


    def tearDown(self):
        print("单个测试用例结束")
        pass
        #单个测试用例结束
    
    def test_login_order(self):
        u"登录测试用例"
        driver = self.driver
        #登录框
        login_ele = driver.find_element_by_css_selector("#login")
        ActionChains(driver).click(login_ele).perform()

        sleep(2)
        #查找输入框,输入账号,输入框要提前清理里面的数据
        driver.find_element_by_id("phone").clear()
        driver.find_element_by_id("phone").send_keys("13113777338")
        #查找密码输入框,输入密码
        driver.find_element_by_id("pwd").clear()
        driver.find_element_by_id("pwd").send_keys("123456789")

        #拿到登录按钮
        login_btn_ele = driver.find_element_by_css_selector("button.login")
        #触发点击事件,登录
        login_btn_ele.click()
        #判断登陆是否成功,逻辑-》鼠标移到上面,判断弹窗字符
        #获取鼠标上移的元素
        user_info_ele = driver.find_element_by_css_selector(".user_head_portrait")
        sleep(1)
        #hover触发
        ActionChains(driver).move_to_element(user_info_ele).perform()
        sleep(1)
        #获取用户名称元素
        user_name_ele = driver.find_element_by_css_selector(".img_name > span:nth-child(2)")
        print("===测试结果==")
        print(user_name_ele.text)

        name = user_name_ele.text
        #self.assertEqual(name, u"二当家小D",msg="登录失败")

        video_ele = driver.find_element_by_css_selector("div.hotcourses:nth-child(3) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1) > div:nth-child(1) > img:nth-child(1)")
        video_ele.click()
        sleep(2)

        buy_btn_ele = driver.find_element_by_css_selector(".learn_btn > a:nth-child(1)")

        buy_btn_ele.click()
        print("进入下单页面")
       


if __name__ == __main__:
       unittest.main()

   

 

python+selenium+unittest

标签:over   net   setup   cio   wait   https   send   输入   acs   

原文地址:https://www.cnblogs.com/bashliuhe/p/13277465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!