<file_sep>import unittest
from common import HTMLTestRunner_cn

pathCase = 'D:\\all_test_dir\\my_test_for_web_1\\case'
rule = 'test*.py'
discover = unittest.defaultTestLoader.discover(start_dir=pathCase,pattern=rule)
print(discover)

report = "D:\\all_test_dir\\my_test_for_web_1\\report\\"+'report.html'
fp = open(report,'wb')
runner = HTMLTestRunner_cn.HTMLTestRunner(stream=fp,title='我日报告',description='好了吧秒速')
runner.run(discover)



<file_sep># coding:utf-8
from time import sleep

from pykeyboard import PyKeyboard
from selenium import webdriver
from pages.login_page import LoginPage

driver = webdriver.Firefox()
a = LoginPage(driver)
a.login()
driver.get('https://leisongwei.5upm.com/bug-create-1-0-moduleID=0.html')
sleep(2)
# 点击上传图片文件
driver.find_element_by_css_selector('.ke-toolbar-icon.ke-toolbar-icon-url.ke-icon-image').click()
sleep(2)
# 点击本地上传
driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[2]/div/div[1]/ul/li[2]').click()
# 点击浏览
driver.find_element_by_css_selector('.ke-inline-block.ke-upload-button').click()


k = PyKeyboard()
s = r"‪C:\users\administrator\desktop\11.txt"
for i in s:
    k.tap_key(i)
sleep(1)
k.tap_key(k.enter_key)

<file_sep># coding: utf-8
from time import sleep

import time

from selenium import webdriver
from case.login_zentao import Login
from base.base import Base


class AddBug(Base):

    loc_test = ('css selector', '#navbar>.nav.nav-default>li:nth-child(4)>a')
    loc_bug = ('css selector', '[data-id="bug"]>a')
    loc_add_bug = ('css selector', '.btn.btn-primary>.icon.icon-plus')

    loc_version = ('css selector', '#openedBuild_chosen>.chosen-choices')
    loc_version_add = ('css selector', '.active-result')
    loc_title = ('id', 'title')
    # 需要切换iframe
    loc_body = ('class name', 'article-content')
    loc_submit = ('id', 'submit')

    # loc_bug_name = ('xpath', "/html/body/main/div/div[3]/div[2]/form/div[2]/table/tbody/tr[1]/td[5]/span")
    loc_bug_name1 = ('xpath', "/html/body/main/div/div[3]/div[2]/form/div[2]/table/tbody/tr[1]/td[3]/a")

    def add_bug(self, title='title'):

        self.click(self.loc_test)
        self.click(self.loc_bug)
        self.click(self.loc_add_bug)
        self.click(self.loc_version)
        self.click(self.loc_version_add)
        self.sendText(self.loc_title, title)
        sleep(2)
        self.driver.switch_to.frame(1)
        self.sendText(self.loc_body, '一二三呢女')
        self.driver.switch_to.default_content()
        self.click(self.loc_submit)

    def is_add_success(self, title):

            # a = self.findElement(self.loc_bug_name).get_attribute('class')
            # b = self.findElement(self.loc_bug_name).text
            # a1 = self.findElement(self.loc_bug_name1).get_attribute('class')
            b1 = self.findElement(self.loc_bug_name1).text
            print(b1)
            # print(a, b)
            # print(a1,b1)
            if title == b1:
                return True
            else:
                return False




if __name__ == '__main__':
    profile_directory=r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\s6qtifzn.default'
    profile = webdriver.FirefoxProfile(profile_directory)
    driver = webdriver.Firefox()

    driver.get('https://leisongwei.5upm.com/user-login-Lw==.html')
    LOGIN = Login(driver)
    LOGIN.login()
    a = AddBug(driver)
    strTime = time.strftime('%Y_%m_%d_%H_%M_%S')
    title = '测试bug' + strTime
    a.add_bug(title)
    sleep(2)
    b =a.is_add_success(title)
    print(b)


<file_sep># coding:utf-8
from time import sleep
from selenium import webdriver
profile_directory = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\s6qtifzn.default'
profile = webdriver.FirefoxProfile(profile_directory)
driver = webdriver.Firefox(profile)
# driver.get('https://mail.126.com/')
# myframe = driver.find_element_by_css_selector(3)
# driver.switch_to.frame(2)
# driver.find_element_by_css_selector('.j-inputtext.dlemail').send_keys('123456')
# driver.switch_to.default_content()
# print(driver.title)
# try:
#     sleep(2)
#     driver.find_element_by_partial_link_text('你的专业电子邮局')
#     print(driver.title)
# except:
#     driver.quit()

driver.get('http://bj.ganji.com/')
driver.find_element_by_css_selector('.tab-category:nth-child(7) .dd>a:nth-child(1)').click()
all_handle = driver.window_handles
print(all_handle)
driver.close()
driver.switch_to.window(all_handle[1])
a = driver.title
print(a)
<file_sep>from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

# driver = webdriver.Firefox()
# driver.get('http://www.baidu.com')
# driver.find_element_by_xpath('//*[@class="s_ipt"]').send_keys('百度一下。你就知道')

# driver.find_element_by_id('su').click()
# mouse = driver.find_element_by_link_text('设置')
# print(mouse)
# ActionChains(driver).move_to_element(mouse).perform()
# sleep(2)
# driver.find_element_by_css_selector('.setpref')
# sleep(2)
# driver.find_element_by_css_selector('#kw').send_keys('我爱你M')
# sleep(2)
# driver.find_element_by_css_selector('#kw').send_keys(Keys.BACK_SPACE)
# sleep(2)
# driver.find_element_by_css_selector('#kw').send_keys('琉璃神社')
# sleep(2)
# driver.find_element_by_css_selector('#kw').send_keys(Keys.CONTROL, 'a')
# driver.find_element_by_css_selector('#kw').send_keys(Keys.CONTROL, 'c')
# sleep(2)
# driver.find_element_by_css_selector('#kw').clear()
# sleep(2)
# driver.find_element_by_css_selector('#kw').send_keys(Keys.CONTROL, 'v')
# sleep(1)
# driver.find_element_by_css_selector('#kw').send_keys(Keys.ENTER)
# sleep(2)
# driver.quit()


# 乘法口诀
for i in range(1,10):
    for j in range(1,10):
        if j>i :
            pass
        else:
            print("%d*%d=%d" % (i,j,i*j), end='   ')
    print('')

for i in range(1,10):
    for j in range(1,i+1):
        print("%d*%d=%2d" % (i,j,i*j),end=" ")
    print(" ")

# 水仙数字
for i in range(100,1000):
    a = i//100
    b = i % 100//10
    c = i % 10
    if i == pow(a, 3)+pow(b, 3)+pow(c, 3):
        print(i)

<file_sep>from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://kyfw.12306.cn/otn/index/init')
js = '''
    document.getElementById('train_date').value='2137-12';
    document.getElementById('fromStationText').value='01-17-12';
    document.getElementById('search_one').click()
    '''
driver.execute_script(js)<file_sep>#-*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait



class Base():
    def __init__(self, driver:webdriver.Firefox):
        self.driver = driver
        self.timeout = 10
        self.t = 0.5

    def findElement(self, loctor):
        ele = WebDriverWait(self.driver, self.timeout, self.t).until(lambda x: x.find_element(*loctor))
        return ele

    def click(self, locator):
        self.findElement(locator).click()

    def sendText(self, locator, text):
        self.findElement(locator).send_keys(text)

    def is_alert(self):
        try:
            return self.driver.switch_to.alert
        except:
            return False

    def is_alert_new(self):
        try:
            return WebDriverWait(self.driver, self.timeout, self.t).until(EC.alert_is_present)
        except:
            return False

    def get_text(self, locator):
        try:
            a = self.findElement(locator).text
            print('获取文本成功，返回文本：%s' % a)
            return a
        except:
            print('获取文本失败，返回空')
            return ''


if __name__ == '__main__':
    driver = webdriver.Firefox()
    driver.get('https://leisongwei.5upm.com/my/')
    a = Base(driver)
    loc1 = (By.ID, 'account')
    a.sendText(loc1,'admin')
    loc2=('id', 'submit')
    a.click(loc2)
<file_sep># coding: utf-8
from time import sleep

import time

from selenium import webdriver
from case.add_bug_zentao import AddBug
import unittest
from case.login_zentao import Login


class TestAddBug(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get('https://leisongwei.5upm.com/my/')
        self.a = Login(self.driver)
        self.a.login()
        self.b = AddBug(self.driver)

    def test1(self):
        str = time.strftime('%Y_%m_%d_%H_%M_%S')
        title = '标题'+str
        self.b.add_bug(title)
        sleep(2)
        a = self.b.is_add_success(title)
        self.assertTrue(a)

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()<file_sep>
def login(driver,user='admin123',password='<PASSWORD>'):
    driver.find_element_by_id('account').send_keys(user)
    driver.find_element_by_name('password').send_keys(<PASSWORD>)
    driver.find_element_by_id('submit').click()<file_sep>import unittest


class CeShiEqual(unittest.TestCase):
    def test01(self):
        self.a = 3
        self.b = 3
        self.assertTrue(self.a == self.b)


if __name__ == '__main__':
    unittest.main()<file_sep>from time import sleep


from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
driver.get('http://www.baidu.com')

mouse = driver.find_element_by_css_selector("#u1 [name = 'tj_settingicon']")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_css_selector('.setpref').click()
sleep(1)
driver.find_element_by_css_selector('#nr').find_element_by_css_selector("option[value='50']").click()
driver.find_element_by_id('nr').click()
sleep(2)
driver.find_element_by_css_selector('#nr').find_element_by_css_selector("option[value='20']").click()
driver.find_element_by_id('nr').click()
sleep(2)
driver.find_element_by_css_selector('.prefpanelgo').click()
a = driver.switch_to.alert
a.accept()
sleep(3)
driver.quit()

<file_sep>#-*- coding: utf-8 -*-
from time import sleep
from selenium import webdriver


class Login():
    def __init__(self, driver):
        self.driver = driver

    def login(self, user='admin123', password='<PASSWORD>'):
        self.driver.find_element_by_id('account').send_keys(user)
        self.driver.find_element_by_name('password').send_keys(<PASSWORD>)
        self.driver.find_element_by_id('submit').click()

    def is_alert(self):
        try:
            sleep(2)
            self.driver.switch_to.alert.accept()
        except:
            return ''

    def get_user_name(self):
        try:
            a = self.driver.find_element_by_css_selector('.user-name').text
            return a
        except:
            return ''

    def is_login_success(self):
        try:
            a = self.driver.find_element_by_css_selector('.user-name').text
            print("登录成功，账号：%s" % a)
            return a
        except:
            print('没有登录成功')
            return ''


if __name__ == '__main__':
    driver = webdriver.Firefox()
    driver.get('https://leisongwei.5upm.com/user-login-Lw==.html')
    a = Login(driver)
    a.login()
    a.is_login_success()
    a.is_alert()<file_sep>import ddt
from time import sleep
import xlrd
from selenium import webdriver
import unittest
from pages.login_page import LoginPage
from common.read_excel import ExcelUtil


path = xlrd .open_workbook('D:\\all_test_dir\\my_test_for_web_1\\common\\databases.xls')
datas = ExcelUtil(path)
testdatas = datas.dict_data()


@ddt.ddt
class Test_login(unittest.TestCase):
    def login_case(self, user, password, expect):
        self.login.send_user(user)
        self.login.send_password(<PASSWORD>)
        self.login.click_submit()
        result = self.login.get_user_name()
        print(result)
        self.assertTrue(result == expect)

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        cls.login = LoginPage(cls.driver)

    def setUp(self):
        self.driver.get('https://leisongwei.5upm.com/my/')

    @ddt.data(*testdatas)
    def test1(self,data):
        self.login_case(data['user'],data['password'], data['expect'])

    def tearDown(self):
        sleep(1)
        self.login.is_alert()
        self.driver.delete_all_cookies()
        sleep(1)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()


if __name__ == '__main__':
    unittest.main()<file_sep>#-*- coding: utf-8 -*-
from selenium import webdriver
from base.base import Base


class LoginPage(Base):
    loc_user = ('id', 'account')
    loc_password = ('<PASSWORD>', '<PASSWORD>')
    loc_submit = ('id', 'submit')
    loc_login_name = ('css selector', '.user-name')

    def send_user(self, user):
        self.sendText(self.loc_user, user)

    def send_password(self, password):
        self.sendText(self.loc_password, password)

    def click_submit(self):
        self.click(self.loc_submit)

    def confirm_alert(self):
        a = self.is_alert_new()
        if a:
            a.accept()

    def get_user_name(self):
        return self.get_text(self.loc_login_name)

    def login(self, user='admin123', password='<PASSWORD>'):
        self.driver.get('https://leisongwei.5upm.com/user-login.html')
        self.send_user(user)
        self.send_password(password)
        self.click_submit()


if __name__ == '__main__':
    driver = webdriver.Firefox()
    driver.get('https://leisongwei.5upm.com/user-login.html')
    login = LoginPage(driver)
    login.send_user('admin123')
    login.send_password('<PASSWORD>')
    login.click_submit()
    login.get_user_name()<file_sep>from time import sleep
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
a = driver.find_element_by_xpath('html/body/div[1]/div[1]/div/div[3]/a[8]').text
b = driver.find_element_by_xpath('html/body/div[1]/div[1]/div/div[3]/a[8]').get_attribute('name')
print(a, b)<file_sep>
from time import sleep

from selenium import webdriver
import unittest


class Test_login(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()

    def setUp(self):
        self.driver.get('https://leisongwei.5upm.com/my/')

    def is_login_success(self):
        try:
            a = self.driver.find_element_by_css_selector('.user-name').text
            print("登录成功，账号：%s"% a)
            return a
        except:
            print('没有登录成功')
            return ''

    def test1(self):
        self.driver.find_element_by_id('account').send_keys('admin123')
        self.driver.find_element_by_name('password').send_keys('<PASSWORD>')
        self.driver.find_element_by_id('submit').click()
        sleep(3)
        a = self.is_login_success()
        self.assertTrue(a == 'admin123')

    def test2(self):
        self.driver.find_element_by_id('account').send_keys('<PASSWORD>')
        self.driver.find_element_by_name('password').send_keys('a')
        self.driver.find_element_by_id('submit').click()
        sleep(3)
        a = self.is_login_success()
        self.assertTrue(a == '')

    def tearDown(self):
        sleep(1)
        self.driver.delete_all_cookies()
        self.driver.refresh()
        sleep(1)


    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()


if __name__ == '__main__':
    unittest.main()<file_sep># coding:utf-8
from time import sleep
from selenium import webdriver
# 加载插件
profile_directory = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\s6qtifzn.default'
profile = webdriver.FirefoxProfile(profile_directory)
# 打开浏览器
driver = webdriver.Firefox(profile)
driver.get("http://gz.ganji.com/")
sleep(1)

# 滚动到底部
js = 'window.scrollTo(0,document.body.scrollHeight)'
driver.execute_script(js)

# 滚动到顶部
sleep(1)
driver.execute_script("window.scrollTo(0, 0)")
sleep(1)

# 滚动到指定位置
ele = driver.find_element_by_link_text('新车')
js = 'arguments[0].scrollIntoView();'
driver.execute_script(js, ele)
sleep(1)
driver.quit()
