51Testing软件测试论坛

标题: 【求助】用unittest 写的小框架,最后报错 [打印本页]

作者: tangtangcx    时间: 2016-11-3 16:01
标题: 【求助】用unittest 写的小框架,最后报错
用unittest 写的小框架,最后报错“AttributeError: 'ceshi' object has no attribute 'driver' ” ,但是检查了好多次,明明已经定义属性了,为什么还会报错

代码如下:
#encoding:utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.select import Select
import unittest,time,os

class ceshi(unittest.TestCase):
    def setUP(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://5.inuoer.com/admin")
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_ceshi(self):
        driver = self.driver
        time.sleep(2)
        driver.find_element_by_name("username").send_keys("admin")
        driver.find_element_by_name("password").send_keys("admin")
        time.sleep(2)
        driver.find_element_by_class_name("loginBtn").submit()
        time.sleep(3)
        driver.switch_to_frame("menu")
        driver.find_element_by_link_text(u"添加信息").click()
        time.sleep(2)
        driver.switch_to_default_content()
        driver.switch_to_frame("main")
        xz = driver.find_element_by_id("classid")
        Select(xz).select_by_value('15')  # 企业网站
        time.sleep(2)
        driver.find_element_by_id("title").send_keys(u"名字")
        driver.find_element_by_xpath("//input[@value='a']").click()
        driver.find_element_by_id("source").send_keys("moijkgoij")
        ff = driver.find_element_by_xpath("//div/iframe[@class='ke-edit-iframe']")
        driver.switch_to_frame(ff)
        time.sleep(2)
        driver.find_element_by_class_name("ke-content").send_keys(
         u"selenium是浏览器测试的大杀器,在研究ajax数据抓取时碰巧研究了一下")
        driver.switch_to.parent_frame()
        driver.find_element_by_class_name("submit").submit()
        driver.switch_to_default_content()

        time.sleep(2)


    def is_element_present(self, how, what):
        try:self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e : return False
        return True

    def is_alert_present(self):
        try:self.driver.switch_to_alert()
        except NoAlertPresentException as e : return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accppt()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)


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


报错截图:









作者: 若尘_51    时间: 2016-11-3 16:34
    def setUP(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://5.inuoer.com/admin")
        self.verificationErrors = []
        self.accept_next_alert = True
那个P是小写的,这个你太粗心了吧。

作者: tangtangcx    时间: 2016-11-3 16:46
若尘_51 发表于 2016-11-3 16:34
def setUP(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://5.i ...

还真是,太感谢了!! 改完立马好了,就说看了好久,想不通为啥定义了说没定义
作者: Miss_love    时间: 2016-11-4 09:20
编译器会自动填充,怎么会出现大写呢
作者: tangtangcx    时间: 2016-11-4 12:05
Miss_love 发表于 2016-11-4 09:20
编译器会自动填充,怎么会出现大写呢

我想锻炼自己写下,结果这一不小心大小写按钮没换过来,就成这样了
作者: Lee_zhang    时间: 2016-11-22 17:20
被人用的Python3.5.2,运行是总会报如下错误。
Traceback (most recent call last):
  File "C:\Users\lee\Desktop\unittest.py", line 6, in <module>
    import unittest
  File "C:\Users\lee\Desktop\unittest.py", line 20, in <module>
    class Baidu(unittest.TestCase):
AttributeError: module 'unittest' has no attribute 'TestCase'
作者: Lee_zhang    时间: 2016-11-22 17:25
本人小白,代码如下:
import os
import sys
import selenium
import unittest
import time
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicity_wait(30)
        self.base_url = "http://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
        
    def test_baidu(self):
        driver = self.driver
        driver.get(sself.base_url + "/")
        driver.find_element_by_id("kw").send_keys("selenium webdriver")
        driver.find_element_by_id("su").click()
        driver.close()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by = how, value = what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text =alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if _name_ == "_main_":
        unittest.main()
作者: Lee_zhang    时间: 2016-11-22 17:25
本人小白,代码如下:
import os
import sys
import selenium
import unittest
import time
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicity_wait(30)
        self.base_url = "http://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
        
    def test_baidu(self):
        driver = self.driver
        driver.get(sself.base_url + "/")
        driver.find_element_by_id("kw").send_keys("selenium webdriver")
        driver.find_element_by_id("su").click()
        driver.close()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by = how, value = what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text =alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if _name_ == "_main_":
        unittest.main()
作者: tangtangcx    时间: 2016-12-5 14:44
Lee_zhang 发表于 2016-11-22 17:20
被人用的Python3.5.2,运行是总会报如下错误。
Traceback (most recent call last):
  File "C:%users\le ...

看你这问题描述应该是unittest导入问题,你看下你Python有没有unittest




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