selenium+python 新浪微博测试自动化实例(源码)

selenium+python 新浪微博测试自动化实例(源码)_第1张图片

因为涉及登陆用户名密码,所以将登陆单独写了一个py文件,其他case进行引用login函数即可(login文件可自行进行编写 比较简单)

l  test_01脚本

判断用户执行结果是否和实际结果相符,有些case写了两种方法,实际中使用一种方法即可,推荐断言方法。

#coding:utf-8
import unittest
import login
from selenium.common.exceptions import TimeoutException
from selenium  import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time

class Test(unittest.TestCase):
    def setup(self):
        self.driver = webdriver.Firefox()
        login.login_os(self.driver)

    def test01(self):
        u'''登陆'''
        self.assertEqual(self.driver.title, u"我的首页 微博-随时随地发现新鲜事", msg = "登陆失败")

    def test02(self):
        u'''退出'''
        try:
            element = WebDriverWait(self.driver, 20, 0.5).until(
                    lambda self:self.find_element_by_xpath("/html/body/div/div/div/div/div/div[3]/div[2]/div[2]/a/em"))
        except TimeoutException:
            print(u"查找设置项超时")
            raise
        else:
            time.sleep(1)
            element.click()

        try:
            element = WebDriverWait(self.driver, 20, 0.5).until(
                    lambda self:self.find_element_by_xpath(
                        "/html/body/div/div/div/div/div/div[3]/div[2]/div[2]/div/ul/li[10]/a"))
        except TimeoutException:
            print(u"查找退出项超时")
            raise
        else:
            time.sleep(1)
            element.click()

        self.driver.get_screenshot_as_file("E:\\study\\xxtest2\\02.jpg")
        self.assertEqual(self.driver.title, u"微博-随时随地发现新鲜事", msg="退出失败")

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

if __name__=='__main__':
    unittest.main()

 l  test_02脚本

#coding:utf-8
import unittest
import login
from selenium.common.exceptions import TimeoutException
from selenium  import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time

class Test(unittest.TestCase):
    def setup(self):
        self.driver = webdriver.Firefox()
        login.login_os(self.driver)

    def test03(self):
        u'''发布微博'''
        try:
            element = WebDriverWait(self.driver, 20, 0.5).until(
                    lambda self: self.find_element_by_xpath(
                        ".//*[@id='v6_pl_content_publishertop']/div/div[2]/textarea"))
        except TimeoutException:
            print(u"查找发布框超时")
            raise
        else:
            element.send_keys("test1")
            time.sleep(1)
            self.driver.find_element_by_xpath(".//*[@id='v6_pl_content_publishertop']/div/div[3]/div[1]/a").click()

        self.assertEqual(self.driver.find_element_by_xpath(
                     "/html/body/div/div/div[2]/div[3]/div[2]/div/div/div/div[2]/div/span[2]").text, u"发布成功", msg="发布微博失败")
        self.driver.get_screenshot_as_file("E:\\study\\xxtest2\\03.jpg")

    def test04(self):
        u'''删除微博'''
        try:
            element = WebDriverWait(self.driver, 20, 0.5).until(
                lambda self: self.find_element_by_xpath(
                    ".//*[@id='v6_pl_rightmod_myinfo']/div/div/div[2]/ul/li[3]/a/span"))
        except TimeoutException:
            print(u"查找主页微博按钮超时")
            raise
        else:
            time.sleep(1)
            element.click()

        try:
            element = WebDriverWait(self.driver, 20, 0.5).until(
                lambda self: self.find_element_by_xpath(
                    ".//*[@id='Pl_Official_ProfileFeedNav__19']/div[2]/div[1]/div/ul/li[1]/a"))
        except TimeoutException:
            print(u"查找我的首页标签超时")
            raise
        else:
            time.sleep(1)
            element.click()

        time.sleep(5)
        self.driver.find_element_by_xpath(
                    "/html/body/div/div/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div/div/a/i").click()
        time.sleep(2)
        self.driver.find_element_by_xpath(
                    "/html/body/div/div/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div/div/div/ul/li/a").click()
        time.sleep(2)
        self.driver.find_element_by_xpath(
                    "/html/body/div/div/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div/div/div[2]/div/p[2]/a").click()
        time.sleep(2)
        self.driver.get_screenshot_as_file("E:\\study\\xxtest2\\04.jpg")
        self.assertNotEqual(self.driver.find_element_by_xpath(
            "/html/body/div/div/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div[3]/div[4]"), "test1", msg = "删除微博失败")

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

if __name__=='__main__':
    unittest.main()

 

l  run_all_case脚本编写

# coding:utf-8
import unittest
import HTMLTestRunner
import time


case_path = 'E:\\study\\xxtest2\\test_case'
discover=unittest.defaultTestLoader.discover(case_path,pattern='test_*.py',top_level_dir=None)
print discover

if __name__=='__main__':
    now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
    report_path = 'E:\\study\\xxtest2\\report\\'
    filename = report_path + now + '_Rport.html'
    with open(filename, 'wb') as fp:
        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'微博基本功能自动化测试执行结果:')
        runner.run(discover)

 

l  在工程文件report下既可以看到报告

selenium+python 新浪微博测试自动化实例(源码)_第2张图片

l  报告结果如图

selenium+python 新浪微博测试自动化实例(源码)_第3张图片

l  测试用例截图

selenium+python 新浪微博测试自动化实例(源码)_第4张图片

 

你可能感兴趣的:(selenium+python 新浪微博测试自动化实例(源码))