51Testing软件测试论坛

标题: unittest 怎么实现一个类中只启动一次浏览器,代码见下图 [打印本页]

作者: 小暖ljj    时间: 2016-9-22 15:50
标题: unittest 怎么实现一个类中只启动一次浏览器,代码见下图
一个class中有四个case,但是每个case都需要启动一遍浏览器,百度了一下unit test的执行顺序是    setUpclass   setUp   TearDown  Teardownclass,把启动的初始化操作写在setUPclass中后一个报错driver的问题。求解其他方法

作者: 若尘_51    时间: 2016-9-22 16:15
使用‘@classmethod’支持在一个类中只进行一次初始化和清理,下面是个例子:

#!/user/bin/env python
#encoding: utf-8

import unittest
from selenium import webdriver

class SearchTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # 基于类的setUp()和tearDown()方法
        # 这里的‘@classmethod’支持在一个类中只进行一次初始化和清理
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get("http://www.jd.com/")

    def test_search_by_category(self):
        # get the search textbox
        self.search_field = self.driver.find_element_by_id("key")
        self.search_field.clear()
        self.search_field.send_keys("phones")

        #submit
        self.search_btn = self.driver.find_element_by_xpath("//button[@clstag='h|keycount|2015|03c']")
        self.search_btn.click()

        # get all the anchor elements which have product names displayed
        # currently on result page using find_elements_by_xpath method
        products = self.driver.find_elements_by_xpath("//div[@class='p-img']/a")
        self.assertGreater(len(products), 3, "more than 3~~")

    def test_search_by_name(self):
        # get the search textbox
        self.search_field = self.driver.find_element_by_id("key")
        self.search_field.clear()
        self.search_field.send_keys("android")

        #submit
        self.search_btn = self.driver.find_element_by_xpath("//button[@clstag='shangpin|keycount|toplist1|b03']")
        self.search_btn.click()


        # get all the anchor elements which have product names displayed
        # currently on result page using find_elements_by_xpath method
        products = self.driver.find_elements_by_xpath("//div[@class='p-img']/a")
        self.assertGreater(len(products), 4, "more than 4~~")

    @classmethod
    def tearDownClass(cls):
        # close the browser window
        cls.driver.quit()

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

作者: 小暖ljj    时间: 2016-9-22 19:47
若尘_51 发表于 2016-9-22 16:15
使用‘@classmethod’支持在一个类中只进行一次初始化和清理,下面是个例子:

#!/user/bin/env python

#!/usr/bin/env python
#coding:utf-8
from lib2to3.pgen2 import driver
import unittest
from appium.webdriver.common.mobileby import MobileBy
from selenium import webdriver
from Pages import BasePages
from ddt import ddt,data,unpack
from Pages import basetestcase
@ddt
class LoginPage(unittest.TestCase):
    @classmethod
    def setUpclass(cls):
        cls.driver=webdriver.Firefox()
        cls.driver.maximize_window()
        cls.driver.get('http://bj.58.com')
        cls.driver.implicitly_wait(30)

    @data(('','',u'您还没有输入账户名'),('','123321',u''),('ljjtest12','',u'您还没有输入密码'))
    @unpack
    def test_FailLogin(self,value1,value2,expected):
       '''验证:错误登陆返回信息'''
       BasePages.Login(self.driver,value1,value2)
       self.assertEqual(BasePages.getErrorText(self.driver),expected)
    def test_successLogin(self,username='ljjtest12',password='123321',):
       '''验证:登陆成功'''
       BasePages.Login(self.driver,username,password,)
       self.assertTrue(MobileBy.ACCESSIBILITY_ID, '退出')

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
   # def tearDown(self):
   #     pass
   #     # self.driver.quit()
if __name__=='__main__':
    unittest.main(verbosity=2)


我这个还是报错driver的问题
Error
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/ddt.py", line 129, in wrapper
    return func(self, *args, **kwargs)
  File "/Users/lijingjing/PycharmProjects/testcase3/testcase/Test_login.py", line 29, in test_FailLogin
    driver=self.driver
AttributeError: 'LoginPage' object has no attribute 'driver'
作者: 若尘_51    时间: 2016-9-23 08:45
小暖ljj 发表于 2016-9-22 19:47
#!/usr/bin/env python
#coding:utf-8
from lib2to3.pgen2 import driver

可以给我看看你调用BasePages.Login这个脚本吗,看提示像是传参过程出错了
作者: 小暖ljj    时间: 2016-9-24 16:20
若尘_51 发表于 2016-9-23 08:45
可以给我看看你调用BasePages.Login这个脚本吗,看提示像是传参过程出错了

#coding:utf-8
from appium.webdriver.common.mobileby import MobileBy

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time as t
#等待方法
import self


def wait():
    t.sleep(2)
def Login(driver,username,password):
    wait()
    clickLogin(driver,username,password)

def clickLogin(driver,username,password):
    wait()
    driver.find_element_by_id('login').find_element_by_link_text(u'登录').click()
    driver.find_element_by_id('login_tab_orig').click()
    driver.find_element_by_id('username_new').send_keys(username)
    driver.find_element_by_id('password_new').send_keys(password)
    driver.find_element_by_id('btnSubmit_new').click()

def getErrorText(driver):
    wait()
    return driver.find_element_by_id("tipli_new").text

作者: 若尘_51    时间: 2016-9-26 09:10
小暖ljj 发表于 2016-9-24 16:20
#coding:utf-8
from appium.webdriver.common.mobileby import MobileBy

看了你的脚本,确实是传参的问题(driver这个参数),我也对传参不熟,试试下面的:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time as t
#等待方法
import self


def wait():
    t.sleep(2)
def Login(self,username,password):
    wait()
    clickLogin(self,username,password)

def clickLogin(self,username,password):
    wait()
    self.driver.find_element_by_id('login').find_element_by_link_text(u'登录').click()
    self.driver.find_element_by_id('login_tab_orig').click()
    self.driver.find_element_by_id('username_new').send_keys(username)
    self.driver.find_element_by_id('password_new').send_keys(password)
    self. driver.find_element_by_id('btnSubmit_new').click()

def getErrorText(self):
    wait()
    return self.driver.find_element_by_id("tipli_new").text




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2